Recycler View Inside Recycler View not Scrolling

浪子不回头ぞ 提交于 2019-12-23 11:55:04

问题


There is a Recycler View inside the other Recycler View.Both needs to scroll vertically. Outer Recycler view is scrolling properly but inner recycler view is not.

Here is the code:

LinearLayoutManager mLayoutManager = new LinearLayoutManager(ViewActivity.this);
outerRecyclerView.setLayoutManager(mLayoutManager);
ViewAdapter adapter = new ViewAdapter(ViewActivity.this);
outerRecyclerView.setAdapter(adapter);

ViewAdapter is as follows:

public void onBindViewHolder(ViewAdapter.ViewViewHolder holder, int position)
{
  //RECYCLER VIEW
  //TODO: Inner Recycler view scroll movement
  LinearLayoutManager mLayoutManager = new LinearLayoutManager(context);
  holder.protocolRecyclerView.setLayoutManager(mLayoutManager);
  ViewProtocolAdapter adapter = new ViewProtocolAdapter(context);
  holder.protocolRecyclerView.setAdapter(adapter);
}

I have tried the following on both recycler views but could not solve the problem

recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
       @Override
       public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
           if(rv.getChildCount() > 0) {
               View childView = rv.findChildViewUnder(e.getX(), e.getY());
               if(childView ==listView) {
                   int action = e.getAction();
                   switch (action) {
                       case MotionEvent.ACTION_DOWN:
                           rv.requestDisallowInterceptTouchEvent(true);
                   }
               }
           }

           return false;
       }

       @Override
       public void onTouchEvent(RecyclerView rv, MotionEvent e) {

       }

       @Override
       public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

       }
   });

Also tried this one:

outerRecyclerView.setNestedScrollingEnabled(true);//Does not make any difference
innerRecyclerView.setNestedScrollingEnabled(true);//Recycler View start scrolling but very slowly and sometimes scrolls the outer one.

回答1:


Instead of using ScrollView use android.support.v4.widget.NestedScrollView

Here for me it worked perfectly.

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="4dp"
            />

</android.support.v4.widget.NestedScrollView>



回答2:


I had the same problem while implementing a recycler view some time ago. Both the recycler views would start scrolling, right. Though it's a bad idea going for a nested recycler view but if you want it to scroll properly, you have to disable the scroll on the inner one. I am not sure but i think it was this. Let me know if it works. If not I'll dig up my code and try to find the solution.

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(parent.getContext()) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };



回答3:


Apply this below ontouch listener to inner recycler which is may be in adapter of parent recycler.

RecyclerView.OnItemTouchListener mScrollTouchListener = 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) {

}
}; 

innerrecyclerView.addOnItemTouchListener(mScrollTouchListener);



回答4:


You don't need to put recyclerView inside another recyclerView. This does not make sense. If you want to put together in a single Layout XML just follow the steps below:

  • Make NestedScrollView parent
  • Create LinerLayout inside your NestedScrollView
  • Put RecyclerViews inside NestedScrollView

    <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/firstRecycler"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
    
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/secondRecycler"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
    
            </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
    



回答5:


What you can do is, you can add a scroll view as root layout for the view holder encapsulating second recycler view in it.

  <ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/subContainer"
    >
    <android.support.v7.widget.RecyclerView
        android:id="@+id/subList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />
</ScrollView>

After doing that, subList recycler view can fully utilize wrap_content property. Now, for this to work you need to disable vertical scroll of sublist recycler view.

LinearLayoutManager layoutManager = new LinearLayoutManager(context) {
        @Override
        public boolean canScrollVertically() {
            return false;
        }
    }; 

Doing so, all the vertical scroll events will be handled by parent recycler view.




回答6:


Now, for this to work you need to disable vertical scroll of sublist recycler view.

LinearLayoutManager layoutManager = new LinearLayoutManager(context) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
    }; 



回答7:


Don't keep a recycler view inside a recycler view. Instead you should inflate the inner recycler view. You can have a Linear layout and inflate that layout in onBindViewHolder of the recycler view. By doing this, you will never face scrolling problem.




回答8:


parentScroll.setOnTouchListener(new View.OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {

        findViewById(R.id.childScroll).getParent()
           .requestDisallowInterceptTouchEvent(false);

        return false;
     }
});

childScroll.setOnTouchListener(new View.OnTouchListener() {

      public boolean onTouch(View v, MotionEvent event) {  

           v.getParent().requestDisallowInterceptTouchEvent(true);
           return false;
      }
});


来源:https://stackoverflow.com/questions/37423763/recycler-view-inside-recycler-view-not-scrolling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!