编程知识 cdmana.com

Android nestedscrollview nesting viewpager and viewpager embedding recyclerview

NestedScrollView nesting viewpager There are two problems

1.viewpager Unable to display , The height is 0

2. After nesting NestedScrollView Can't slide

Let's start with the first question :
viewpager Can't display normally , Only need NestedScrollView In addition
android:fillViewport="true" That's all right.

  <android.support.v4.widget.NestedScrollView
            android:id="@+id/novice_live_scroll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:overScrollMode="never"
            android:fillViewport="true"
            >

The second question is , After nesting NestedScrollView Can't slide , This is a question that we just rewrite viewpager, And then in xml We use our viewpager That's all right.

public class WrapContentHeightViewPager extends ViewPager {
    public WrapContentHeightViewPager(Context context) {
        super(context);  
    }  
  
    public WrapContentHeightViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);  
    }  
  
    @Override  
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  
        int height = 0;  
        for (int i = 0; i < getChildCount(); i++) {  
            View child = getChildAt(i);
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));  
            int h = child.getMeasuredHeight();  
            if (h > height) height = h;  
        }  
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);  
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
    }  
}  

If viewpager There are also nested recycleview, Just want to let nestedScrollView slide ,recyclerView Just to show , Then it's necessary to recyclerview To deal with , as follows :

   LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), 		   LinearLayoutManager.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
    recycler.setLayoutManager(linearLayoutManager);

Record a small pit found , There are friends with these problems , I hope this post can help you .

版权声明
本文为[osc_ j9bz63ep]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224102147120i.html

Scroll to Top