Use RecyclerView inside ScrollView with flexible Recycler item height

后端 未结 8 1655
误落风尘
误落风尘 2020-11-29 04:57

I want to know if there is any possible way to use RecyclerView?

Before this, I used RecyclerView with fixed height inside a

相关标签:
8条回答
  • 2020-11-29 05:11

    To fix fling in RecyclerView you must override canScrollVertically in LinearLayoutManager:

    linearLayoutManager = new LinearLayoutManager(context) {
     @Override
     public boolean canScrollVertically() {
      return false;
     }
    };
    
    0 讨论(0)
  • 2020-11-29 05:11

    In case setting fixed height for the RecyclerView didn't work for someone (like me), here is what I've added to the fixed height solution:

       mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
            int action = e.getAction();
            switch (action) {
            case MotionEvent.ACTION_MOVE:
                rv.getParent().requestDisallowInterceptTouchEvent(true);
                break;
        }
        return false;
    }
    
    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    
    }
    
    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
    
        }
    });
    

    I

    0 讨论(0)
  • 2020-11-29 05:12

    I solved this problem by using NestedScrollView instead of ScrollView. And for RecyclerView

        <android.support.v7.widget.RecyclerView 
    android:layout_width="match_parent"
      android:layout_height="match_parent" />
    

    And in Code

    recyclerView.setHasFixedSize(true);
    recyclerView.setNestedScrollingEnabled(false);
    
    0 讨论(0)
  • 2020-11-29 05:14

    Use This line to your recyclerview :

     android:nestedScrollingEnabled="false"
    

    try it ,recyclerview will be smoothly scrolled with flexible height

    0 讨论(0)
  • 2020-11-29 05:21

    If you want to just scrolling then you can use to NestedScrollView instead of ScrollView So you can modify your code with following :

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
                //design your content here with RecyclerView 
    
        </LinearLayout>
    
    </android.support.v4.widget.NestedScrollView>
    
    0 讨论(0)
  • 2020-11-29 05:23

    I fully agree with Ashkan solution, big thanks (voted +1!), but there is one thing...

    If RecyclerView does scroll correctly inside ScrollView/NestedScrollView BUT it doesn't fling (intercept it) then the solution is to extend RecyclerView and override OnInterceptTouchEvent and OnTouchEvent. If you don't need any click actions but just want to present items with working fling then simply return false in both like below:

    public class InterceptingRecyclerView extends RecyclerView {
    
        public InterceptingRecyclerView(Context context) {
            super(context);
        }
    
        public InterceptingRecyclerView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public InterceptingRecyclerView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent e) {
            return false;
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent e) {
            return false;
        }
    }
    

    Or actually it should be called NonIntercepting... ;) Simple as that.

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