【问题标题】:Listview/Recycler View is not expanding in Scrollview/NestedViewListview/Recycler View 未在 Scrollview/NestedView 中展开
【发布时间】:2015-11-13 10:34:00
【问题描述】:

我在滚动/嵌套滚动视图中尝试了列表和回收器视图,列表的高度没有正确扩展。如果我硬编码列表的高度而不是它的工作原理,否则包装内容不起作用。 由于滚动视图中的列表视图和两个对象都具有滚动能力,我在互联网上进行了很多搜索,这就是为什么一个对象不让另一个对象滚动的原因。这就是为什么列表视图的高度不起作用的原因。我知道这是不好的做法,但我没有选择我的布局包含多个对象,如复选框和编辑文本。我也需要滚动其他对象。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
        xmlns-android="http-//schemas.android.com/apk/res/android"
        xmlns-app="http-//schemas.android.com/apk/res-auto"
        android-isScrollContainer="false"
        android-layout_width="match_parent"
        android-layout_height="match_parent"
        android-layout_gravity="fill_vertical"
        android-clipToPadding="false"
        app-layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android-layout_width="match_parent"
            android-layout_height="match_parent"
            android-orientation="vertical"
            android-paddingTop="1dp">

     <ListView
        android-id="@+id/list"
        android-layout_width="fill_parent"
        android-layout_height="match_parent"
        android-divider="@null" />
         </LinearLayout>
</android.support.v4.widget.NestedScrollView>

【问题讨论】:

  • 你需要给listView设置一个固定的高度,有什么问题?
  • 我相信 NestedScrollView 中的 RecyclerView 应该可以工作,但您需要致电 recyclerView.setNestedScrollingEnabled(true)
  • 我可以完成“m vai”的陈述。 RecyclerView 与 CollapsingToolbar 结合使用似乎更加可靠。
  • 列表中的项目是动态的,项目在运行时生成,这就是我无法修复高度的原因
  • 我也试过recyclerview同样的结果

标签: android listview android-recyclerview android-scrollview


【解决方案1】:

尝试在 ScrollView 上设置属性 fillViewport="true"

【讨论】:

    【解决方案2】:

    你可以用这个:

    <android.support.v4.widget.NestedScrollView
            android:id="@+id/nested_scrollview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false">
    
            <com.example.NestedListView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
        </android.support.v4.widget.NestedScrollView>
    

    和:

    public class NestedListView extends ListView implements View.OnTouchListener, AbsListView.OnScrollListener {
    
        private int listViewTouchAction;
        private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;
    
        public NestedListView(Context context, AttributeSet attrs) {
            super(context, attrs);
            listViewTouchAction = -1;
            setOnScrollListener(this);
            setOnTouchListener(this);
        }
    
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                             int visibleItemCount, int totalItemCount) {
            if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
                if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                    scrollBy(0, -1);
                }
            }
        }
    
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
            int newHeight = 0;
            final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
            int heightSize = MeasureSpec.getSize(heightMeasureSpec);
            if (heightMode != MeasureSpec.EXACTLY) {
                ListAdapter listAdapter = getAdapter();
                if (listAdapter != null && !listAdapter.isEmpty()) {
                    int listPosition = 0;
                    for (listPosition = 0; listPosition < listAdapter.getCount()
                            && listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
                        View listItem = listAdapter.getView(listPosition, null, this);
                        //now it will not throw a NPE if listItem is a ViewGroup instance
                        if (listItem instanceof ViewGroup) {
                            listItem.setLayoutParams(new LayoutParams(
                                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                        }
                        listItem.measure(widthMeasureSpec, heightMeasureSpec);
                        newHeight += listItem.getMeasuredHeight();
                    }
                    newHeight += getDividerHeight() * listPosition;
                }
                if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
                    if (newHeight > heightSize) {
                        newHeight = heightSize;
                    }
                }
            } else {
                newHeight = getMeasuredHeight();
            }
            setMeasuredDimension(getMeasuredWidth(), newHeight);
        }
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
                if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                    scrollBy(0, 1);
                }
            }
            return false;
        }
    }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2015-11-14
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 2019-11-11
      • 2018-06-23
      • 1970-01-01
      相关资源
      最近更新 更多