0

ListView is not scrolling inside LinearLayout. Then I have added a ScrollView and put the LinearLayout that contains ListView. But still my code is not working. Here is my full xml code...

bottom_content_geofence_show.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/des_cardview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        app:cardBackgroundColor="@color/cardview_dark_background">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:paddingBottom="12dp"
            android:paddingStart="20dp"
            android:paddingTop="12dp">


            <TextView
                android:id="@+id/textView16"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Type of Geofence is Polygon"
                android:textAllCaps="true" />

            <TextView
                android:id="@+id/created_by_textview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Created By Anuradha"
                android:textAllCaps="true" />

            <TextView
                android:id="@+id/created_on_textview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="CReated On 4 April, 2017"
                android:textAllCaps="true" />
        </LinearLayout>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:id="@+id/enforce_detail_cardview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:paddingStart="20dp"
        android:paddingTop="20dp"
        app:cardBackgroundColor="@color/cardview_dark_background">

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

            <TextView
                android:id="@+id/enforce_textview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Enforced Assets"
                android:textAlignment="viewStart"
                android:textAllCaps="true"
                android:textColor="@color/colorPrimary"
                android:textSize="14sp" />

            <ListView
                android:id="@+id/asset_list"
                style="@style/Widget.AppCompat.ListView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:divider="@color/White"
                android:groupIndicator="@null"
                android:overScrollMode="always"
                android:smoothScrollbar="true" />
        </LinearLayout>

    </android.support.v7.widget.CardView>
</LinearLayout>

This layout will be shown inside a NestedScrollView

activity_show.xml

<android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">


        <include layout="@layout/bottom_content_geofence_show" />
</android.support.v4.widget.NestedScrollView>

Please let me know possible solutions. Thanks in advance.

anuradha
  • 612
  • 1
  • 8
  • 20
  • If you'r using `NestedScrollView` then don't use `ScrollView` again. – Piyush May 02 '17 at 07:29
  • ok I will remove the ScrollView. But both scenarios are same for me. Listview scrolling is not working wit/without the scrollview – anuradha May 02 '17 at 07:32
  • Its has fix height so it will show data only in that portion. Check does listview populate with your data? – Piyush May 02 '17 at 07:32
  • @Piyush I have updated my code.....removed scroll view and fixed height of ListView and added fixed height to LinearLayout that contains ListView.....you can look into it. But still the listview is not scrlling. The listview populates data but is not scrolling. – anuradha May 02 '17 at 07:40
  • 2
    My advice to use `RecylerView` instead of `ListView`. – Piyush May 02 '17 at 07:42

2 Answers2

0

Try to use this, might be you gotta solution for it :-

           Utility.setListViewHeightBasedOnChildren(yourlistview);

add this method :-

public static class Utility {
        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);
        }
    }

It will not scrolling but it is going to fix your height of a listView and some changes in your show.xml

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        <include layout="@layout/bottom_content_geofence_show" />
</LinearLayout>
Andie
  • 86
  • 3
  • 19
0

Use below library, it really works in Linear layout.

 compile 'com.github.paolorotolo:expandableheightlistview:1.0.0'

Usage:

<ScrollView 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">

    <com.github.paolorotolo.expandableheightlistview.ExpandableHeightListView
        android:id="@+id/expandable_listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="8dp">
    </com.github.paolorotolo.expandableheightlistview.ExpandableHeightListView></ScrollView>

Inside Activity:

 ExpandableHeightListView expandableListView = (ExpandableHeightListView) findViewById(R.id.expandable_listview);

expandableListView.setAdapter(adapterName);

// This actually does the magic
expandableListView.setExpanded(true);
Sumit Kumar
  • 482
  • 1
  • 5
  • 16