본문 바로가기
Study/Android

[Android | JAVA] ListView 2개 / scrollView 사용하기

by M개발자 2021. 8. 30.
반응형

https://wefu.tistory.com/64

 

이분 글 보고 따라하니깐 잘됨 ^^b 짱짱천재 ~~! 

 

 

list.xml

처음 | androidx.core.widget.NestedScrollView

중간 | LinearLaout

중간 안 | ListView

 

ScrollView는 한개의 자식만을 취급하므로 LinearLaout으로 ListView들을 묶어줘야한다. 

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".CallFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <TextView
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="     성범죄 관련 신고"
        android:textSize="14sp"
        android:background="#F2F2F2"/>

    <ListView
        android:id="@+id/reportListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="     성범죄 관련 법무법인"
        android:textSize="14sp"
        android:background="#F2F2F2"/>

    <ListView
        android:id="@+id/lawfirmListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    </LinearLayout>
</androidx.core.widget.NestedScrollView>

MainActivity.java

앱 기능 상 Fragment에서 구현

public class CallFragment extends Fragment {

    //report list view
    private ListView reportListView;
    private CallReportListViewAdapter reportAdpater;

    //lawfirm list view
    private ListView lawfirmListView;
    private CallReportListViewAdapter lawfirmAdpater;

	//스크롤 가능하게 만들기
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = (ListAdapter) listView.getAdapter();
        if (listAdapter == null) return;
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_call_list, container, false);

        //report list view
        reportAdpater = new CallReportListViewAdapter();
        reportListView = (ListView) v.findViewById(R.id.reportListView);
        reportListView.setAdapter(reportAdpater);

        reportAdpater.addItem("사이버 경찰청", "112");

        reportListView.setAdapter(reportAdpater);
        reportAdpater.notifyDataSetChanged();

        //report list view
        lawfirmAdpater = new CallReportListViewAdapter();
        lawfirmListView = (ListView) v.findViewById(R.id.lawfirmListView);
        lawfirmListView.setAdapter(lawfirmAdpater);

        lawfirmAdpater.addItem("법무법인 굳센 (24시)", "02-598-8284");

        lawfirmListView.setAdapter(lawfirmAdpater);
        lawfirmAdpater.notifyDataSetChanged();
        setListViewHeightBasedOnChildren(reportListView);
        setListViewHeightBasedOnChildren(lawfirmListView);

        return v;
    }
}

 


참고자료

https://wefu.tistory.com/64

반응형

댓글