ListView not expanding inside NestedScrollView

前端 未结 10 1051
忘了有多久
忘了有多久 2020-12-23 19:57

I am using CoordinatorLayout in my activity page. In that there is ListView below the app bar. But its not working when I use ListView

相关标签:
10条回答
  • 2020-12-23 20:51

    on Lollipop onwards you can use

    setNestedScrollingEnabled(true);
    

    on your ListView/GridView/ScrollableView. From the documentation

    Enable or disable nested scrolling for this view

    if you need backwards compatibility with older version of the OS you'll have to use the RecyclerView. You can read more here

    Edit. ViewCompat has the static method setNestedScrollingEnabled(View, boolean). Eg.

    ViewCompat.setNestedScrollingEnabled(listView, true)
    

    thanks to @Dogcat for pointing it out

    0 讨论(0)
  • 2020-12-23 20:52

    Replace your ListView with RecyclerView if possible else create your customListView and set onMeasure of ListView to this:

     public NonScrollListView(Context context) {
            super(context);
        }
    
        public NonScrollListView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        }
    

    This ListView won't be able to scroll anymore and can be used inside NestedScrollView.

    0 讨论(0)
  • 2020-12-23 20:55

    Below code worked for me:

    ViewCompat.setNestedScrollingEnabled(listView, true);
    

    Your ListView should be inside NestedScrollView

    0 讨论(0)
  • 2020-12-23 21:01

    this is what worked for me.

    set android:fillViewport="true" on the NestedScrollView

    add One Layout Element as Child to NestedScrollView. In my case LinearLayout and then set android:nestedScrollingEnabled="true" on ListView Make ListView a child of LinearLayout

    Good to go

    0 讨论(0)
提交回复
热议问题