【问题标题】:ListView inside LinearLayout is not scrolling in AndroidLinearLayout 内的 ListView 在 Android 中不滚动
【发布时间】:2017-05-02 07:24:54
【问题描述】:

ListView 不在 LinearLayout 内滚动。然后我添加了一个 ScrollView 并放置了包含 ListView 的 LinearLayout。但是我的代码仍然无法正常工作。这是我的完整 xml 代码...

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>

此布局将显示在 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>

请告诉我可能的解决方案。提前致谢。

【问题讨论】:

  • 如果您正在使用NestedScrollView,请不要再使用ScrollView
  • 好的,我将删除 ScrollView。但是这两种情况对我来说都是一样的。 Listview 滚动在有/没有滚动视图的情况下不起作用
  • 它有固定的高度,所以它只会显示那部分的数据。检查 listview 是否填充了您的数据?
  • @Piyush 我已经更新了我的代码.....删除了 ListView 的滚动视图和固定高度,并为包含 ListView 的 LinearLayout 添加了固定高度.....你可以查看它。但是列表视图仍然没有滚动。列表视图填充数据但不滚动。
  • 我的建议是使用RecylerView 而不是ListView

标签: android xml android-layout listview


【解决方案1】:

尝试使用它,也许你需要解决它:-

           Utility.setListViewHeightBasedOnChildren(yourlistview);

添加这个方法:-

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);
        }
    }

它不会滚动,但会修复 listView 的高度以及 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>

【讨论】:

    【解决方案2】:

    使用下面的库,它确实适用于线性布局。

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

    用法:

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

    内部活动:

     ExpandableHeightListView expandableListView = (ExpandableHeightListView) findViewById(R.id.expandable_listview);
    
    expandableListView.setAdapter(adapterName);
    
    // This actually does the magic
    expandableListView.setExpanded(true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多