Ghimire
Ghimire

Reputation: 958

Scroll ParentLayout which includes two ListView

I am using two listView within LinearLayout.The List Data is populated through JSON and I am able to populate data in the ListView.

I got one problem that the whole LinearLayout is not scrollable i.e Meaning that only the first ListData is shown when scrollview is not included in the xml file.

fragment_dashboard

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context="com.example.user.mis.fragment.Dashboard">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#2E353D"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="30dp"
                android:layout_height="match_parent"
                android:padding="3dp"
                android:src="@mipmap/dashboard" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="8dp"
                android:text="Dashboard"
                android:textColor="#fff"
                android:textSize="17dp"
                android:textStyle="bold" />

        </LinearLayout>
    </LinearLayout>

    <!--  <ScrollView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:scrollbars="vertical">   -->

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="10dp"
            android:background="#F3F1F2"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="30dp"
                android:layout_height="20dp"
                android:layout_gravity="left|center"

                android:src="@mipmap/progress" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="20dp"
                android:layout_gravity="center"
                android:gravity="center"
                android:paddingLeft="5dp"
                android:text="Progress"
                android:textColor="#008B8B"
                android:textStyle="bold" />

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#EEECED" />

        <LinearLayout

            android:layout_width="match_parent"
            android:layout_height="215dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@mipmap/progressdiagram" />


        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginLeft="15dp"
            android:background="#F3F1F2"
            android:orientation="horizontal">

            <ImageView

                android:layout_width="30dp"
                android:layout_height="20dp"
                android:layout_gravity="left|center"
                android:src="@mipmap/notice" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="20dp"
                android:layout_gravity="center"
                android:gravity="center"
                android:paddingLeft="5dp"
                android:text="Notice"
                android:textColor="#008B8B"
                android:textStyle="bold" />

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:background="#EEECED" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="10dp"
            android:background="#EEECED" />


        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:background="#EEECED" />


        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:background="#EEECED" />


        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:background="#EEECED" />

        <!--     <LinearLayout
                 android:layout_width="match_parent"
                 android:layout_height="30dp"
                 android:layout_marginLeft="15dp"
                 android:layout_marginTop="5dp"
                 android:orientation="horizontal">

                 <ImageView

                     android:layout_width="30dp"
                     android:layout_height="15dp"
                     android:layout_gravity="left|center"
                     android:src="@mipmap/subnotice" />

                 <TextView
                     android:layout_width="wrap_content"
                     android:layout_height="20dp"
                     android:layout_gravity="center"
                     android:gravity="center"
                     android:paddingLeft="5dp"
                     android:text="Review of student support officers"
                     android:textSize="13sp"
                     android:textStyle="normal" />

             </LinearLayout>    -->


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


            />


        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:background="#EEECED" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginLeft="15dp"
            android:background="#F3F1F2"
            android:orientation="horizontal">

            <ImageView

                android:layout_width="30dp"
                android:layout_height="20dp"
                android:layout_gravity="left|center"
                android:src="@mipmap/pendingbill" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="20dp"
                android:layout_gravity="center"
                android:gravity="center"
                android:paddingLeft="5dp"
                android:text="Pending Bills"
                android:textColor="#008B8B"
                android:textStyle="bold" />

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#EEECED" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="5dp"
            android:orientation="horizontal">


            <TextView
                android:layout_width="0dp"
                android:layout_height="20dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="left"
                android:paddingLeft="5dp"
                android:text="Bill No."
                android:textSize="13sp"
                android:textStyle="normal" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="20dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="left"
                android:paddingLeft="5dp"
                android:text="Date"
                android:textSize="13sp"
                android:textStyle="normal" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="20dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="left"
                android:paddingLeft="5dp"
                android:text="Amount"
                android:textSize="13sp"
                android:textStyle="normal" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="20dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="left"
                android:paddingLeft="5dp"
                android:text="Status"
                android:textSize="13sp"
                android:textStyle="normal" />

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:background="#EEECED" />


        <!--            <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="30dp"
                        android:layout_marginLeft="15dp"
                        android:layout_marginTop="5dp"
                        android:orientation="horizontal"></LinearLayout>  -->
        <ListView
            android:id="@+id/student_dashboard_pendingbills"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"


            />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:background="#EEECED" />

    </LinearLayout>
    <!-- </ScrollView>   -->

</LinearLayout>

It is not recommended to use scrollview including ListView.But when i use scrollview outside ListView .Both list items are showing only one item i.e are scrollable.my output seems like this

Dashboard

public class Dashboard extends Fragment {

    ListView listViewNotice, listviewpendingbills;
    String Navigation_URL = "http://192.168.100.5:84/api/academics/getNotices";
    String Navigation_URL_BIlls = "http://192.168.100.5:84/Api/financeApi/getAllFees";

    String access_token;
    String Notice, BillNumber, Status, AmountPaid, Name;
    String master_id;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
        // getActivity().setTitle("St. Xavier's High School");
        setHasOptionsMenu(true);

        listViewNotice = (ListView) view.findViewById(R.id.list_student_notice_dashboard);
        listviewpendingbills = (ListView) view.findViewById(R.id.student_dashboard_pendingbills);

        SessionManagement session = new SessionManagement(getContext());
        session.checkLogin();
        access_token = session.getAccesstToken();
        master_id = session.getMasterId();
        makeJsonObjectRequest();
        makeJsonObjectRequestBills();
        return view;
    }

    private void makeJsonObjectRequest() {

        RequestQueue requestQueue = Volley.newRequestQueue(getContext());
        String URL = Navigation_URL;
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {

                            ArrayList<DashboardStudentNotice> student_list_notice_dashboard = new ArrayList<>();
                            JSONArray jArray = new JSONArray(response);
                            for (int i = 0; i < jArray.length(); i++) {
                                JSONObject jsonObject = jArray.getJSONObject(i);
                                Notice = jsonObject.getString("notice");
                                student_list_notice_dashboard.add(new DashboardStudentNotice(Notice));

                            }


                            DashboardStudentNoticeAdapter dashboardStudentNoticeAdapter = new DashboardStudentNoticeAdapter(getActivity(), student_list_notice_dashboard);

                            listViewNotice.setAdapter(dashboardStudentNoticeAdapter);
                            setListViewHeightBasedOnChildren(listViewNotice);



                        } catch (JSONException e) {
                            Toast.makeText(getContext(), "Fetch failed!", Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        }
                    }

                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getContext(), error.toString(), Toast.LENGTH_LONG).show();
            }
        }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<String, String>();
                headers.put("Authorization", "Bearer " + access_token);
                headers.put("Content-Type", "application/x-www-form-urlencoded");
                return headers;
            }
/*
            @Override
            protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();

                map.put("id", master_id);
                map.put("accessID", accessID);
                map.put("currentUser", master_id);
                return map;

            } */
        };


        requestQueue.add(stringRequest);

    }


    private void makeJsonObjectRequestBills() {

        RequestQueue requestQueue = Volley.newRequestQueue(getContext());
        String URL1 = Navigation_URL_BIlls + "?id=" + master_id;
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL1,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {

                            ArrayList<DashboardStudent_PendingBills> student_list_pendingbills_dashboard = new ArrayList<>();
                            JSONArray jArray = new JSONArray(response);
                            for (int i = 0; i < jArray.length(); i++) {
                                JSONObject jsonObject = jArray.getJSONObject(i);
                                BillNumber = jsonObject.getString("RecieptNo");
                                Status = jsonObject.getString("Status");
                                AmountPaid = jsonObject.getString("AmountPaid");
                                Name = jsonObject.getString("NAME");
                                student_list_pendingbills_dashboard.add(new DashboardStudent_PendingBills(BillNumber, Name, AmountPaid, Status));

                            }


                            DashboardStudentPendingBillsAdapter dashboardStudentPendingBillsAdapter = new DashboardStudentPendingBillsAdapter(getActivity(), student_list_pendingbills_dashboard);

                            listviewpendingbills.setAdapter(dashboardStudentPendingBillsAdapter);
                            setListViewHeightBasedOnChildren(listviewpendingbills);


                        } catch (JSONException e) {
                            Toast.makeText(getContext(), "Fetch failed!", Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        }
                    }

                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getContext(), error.toString(), Toast.LENGTH_LONG).show();
            }
        }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<String, String>();
                headers.put("Authorization", "Bearer " + access_token);
                headers.put("Content-Type", "application/x-www-form-urlencoded");
                return headers;
            }
/*
            @Override
            protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();

                map.put("id", master_id);
                map.put("accessID", accessID);
                map.put("currentUser", master_id);
                return map;

            } */
        };


        requestQueue.add(stringRequest);

    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.dashboard, menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle item selection
        switch (item.getItemId()) {
            case R.id.action_settings:
                // do s.th.
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            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();
    }


}

How can the Whole Layout be made scrollable with listitems not being scrollable i.e all are shown?

Upvotes: 1

Views: 127

Answers (2)

Suraj Makhija
Suraj Makhija

Reputation: 1396

First of all, uncomment ScrollView in your layout file. Thereafter, call the following method after calling setAdapter() for both the listview's :

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = 0;
    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += listItem.getMeasuredHeight();
    }

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

Upvotes: 1

Abhishek Kumar
Abhishek Kumar

Reputation: 1265

Try to this code to get what you want

<LinearLayout>
    <ScrollView>
         <LinearLayout>.......</LinearLayout>
                                    </ScrollView>
                                           </LinearLayout>

Put within Nested ScrollView to your ListView.

  <LinearLayout
            android:layout_margin="10dp"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">`

            <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">
            <Listview>
                 android:id="@+id/recycler_pracstice_loaction"
                 android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scrollbars="none" />

            </android.support.v4.widget.NestedScrollView>



        </LinearLayout>

and Put This code in your Activity class.

listView.setOnTouchListener(new OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_MOVE) {
        return true; // Indicates that this has been handled by you and will not be forwarded further.
    }
    return false;
}

});

Upvotes: 0

Related Questions