RecyclerView scroll scrolling in Android SDK 23

I implement a RecyclerView inside a ScrollView . To have only one scroll behavior throughout the page, I implement a version of NonScrollRecyclerView . The implementation is as follows:

 public class NonScrollRecyclerView extends RecyclerView { public NonScrollRecyclerView(Context context) { super(context); } public NonScrollRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); } public NonScrollRecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean dispatchTouchEvent(MotionEvent ev){ if(ev.getAction() == MotionEvent.ACTION_MOVE) return true; return super.dispatchTouchEvent(ev); } } 

As soon as I update my build settings and goals to SDK 23, I had problems scrolling the page containing NonScrollRecyclerView . The specific problem is that the page scrolls until I get to the recycler view part, and as soon as I get to this view, I can no longer scroll up or down.

I donot face this problem with SDK 22 and below

My xml is as follows:

XML @layout/rv contains recycler view

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/background_gray"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/background_gray" android:orientation="vertical"> <include layout="@layout/row_mall_header" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/cv_mall_header" android:layout_marginTop="8dp"/> <include layout="@layout/row_mall_shops" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/cv_mall_shops" android:layout_marginTop="8dp"/> <include layout="@layout/row_mall_coupons" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/cv_mall_coupons" android:layout_marginTop="8dp"/> <include layout="@layout/rv" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/cv_mall_feeds" android:layout_marginTop="8dp"/> </LinearLayout> </ScrollView> 

XML - @ layout / rv

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/background_gray" android:id="@+id/ll_mall_feeds"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:paddingTop="6dp" android:paddingBottom="6dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_feedcount" android:textColor="@color/semi_theme_blue" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="12dp" android:layout_centerVertical="true" /> </RelativeLayout> <com.project.ui.NonScrollRecyclerView android:id="@+id/nrv" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="@android:color/transparent" android:listSelector="@android:color/transparent" /> </LinearLayout> 
+5
source share
2 answers

RecyclerView and ListView are not recommended inside ScrollView, because when rendering ScrollView elements values ​​are not calculated. This means that your adapter cannot be full when ScrollView is displayed, and later, when the RecyclerView is notified of data changes (for example, when you initialize your adapter), there is no way to recalculate the heights of the elements.

This is really a pain in the neck, because you should try to calculate the heights of the elements, and it is never accurate, so you will have discrepancies when displaying a ListView or RecyclerView inside a ScrollView. Below are examples of how to calculate element heights here and here .

My advice is to turn the RecyclerView into a LinearLayout and add items programmatically so that you emulate the behavior of a ListView or RecyclerView:

 LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.files); layout.removeAllViews(); for (int i = 0; i < fileAdapter.getCount(); i++) { final View item = fileAdapter.getView(i, null, null); item.setClickable(true); item.setId(i); item.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { fileContentRowPosition = v.getId(); # Your click action here } }); layout.addView(item); } 

Here is its XML with file definition:

 <ScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="match_parent"> ... <LinearLayout android:id="@+id/files" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:orientation="vertical"> </LinearLayout> </ScrollView> 

All java code can be checked here and the whole layout.

On the other hand, if you still want to continue your implementation and about your problem, you can check out this article on Handling Scrollable Controls in Scrollview

Yours faithfully,

+2
source

For just one scroll across the entire page, you can use NestedScrollView instead of ScrollView and set recyclerView.setNestedScrollingEnabled (false);

+1
source

Source: https://habr.com/ru/post/1236685/


All Articles