Horizontal reseller View inside ViewPager without scrolling

Problem

I have a horizontal RecyclerView inside a NestedScrollView that is inside a ViewPager. Now when I try to scroll through the RecyclerView, sometimes it scrolls, but sometimes only the ViewPager scrolls.

Code

What my XML RecyclerView looks like:

<android.support.v7.widget.RecyclerView android:id="@+id/sidescroll" android:layout_below="@+id/movie_more_movies2" android:layout_marginTop="@dimen/material_layout_keylines_horizontal_margin" android:layout_marginBottom="@dimen/material_layout_keylines_horizontal_margin" android:layout_width="match_parent" android:orientation="horizontal" app:layoutManager="android.support.v7.widget.LinearLayoutManager" android:layout_height="wrap_content"/> 

What the nested Scroll in which the RecyclerView is located looks like:

 <android.support.v4.widget.NestedScrollView android:id="@+id/detail_holder" android:layout_width="match_parent" android:layout_height="match_parent" android:clipToPadding="false" android:descendantFocusability="blocksDescendants" app:layout_behavior="@string/appbar_scrolling_view_behavior" > 

And this is the viewpager xml:

 <com.mt.moviesiwanttowatch.ui.ViewPagerWithHorizontalRecyclerView android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" android:clipChildren="false" android:clipToPadding="false" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

I am using this Custom ViewPager:

 public class ViewPagerWithHorizontalRecyclerView extends ViewPager { public ViewPagerWithHorizontalRecyclerView(Context context) { super(context); } public ViewPagerWithHorizontalRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { return super.onInterceptTouchEvent(ev); } @Override protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) { if(v instanceof RecyclerView){ Log.e("PAGER", "IS"); return false; } else { Log.e("PAGER", "IS NOT " + v.toString()); } return super.canScroll(v, checkV, dx, x, y); } } 

My approach

What I have tried so far, as you can see, I wrote a custom ViewPager. I tried to show the ViewPager that it cannot scroll if the scroll comes from RecyclerView. However, this does not work.

Magazines:

This is the log when ViewPager scrolls instead of RecyclerView

 > 06-27 17:50:53.506 32362-32362/com.mt.moviesiwanttowatch E/PAGER: IS NOT > com.mt.moviesiwanttowatch.ui.ViewPagerWithHorizontalRecyclerView{c506165 > VFED..... ........ 0,341-1080,1794 #7f090287 app:id/viewpager} > 06-27 17:50:53.506 32362-32362/com.mt.moviesiwanttowatch E/PAGER: IS NOT android.support.v4.widget.NestedScrollView{d21952 VFED..... > ........ 0,0-1080,1453 #7f0900b9 app:id/detail_holder} > 06-27 17:50:53.506 32362-32362/com.mt.moviesiwanttowatch E/PAGER: IS NOT android.widget.RelativeLayout{6ddeec0 VE..... ........ > 0,0-1080,2860 #7f090199 app:id/movie_overview_holder} > 06-27 17:50:53.506 32362-32362/com.mt.moviesiwanttowatch E/PAGER: IS 
+5
source share
4 answers

try setting onTouchListener for recycleView:

 recycleView.setOnTouchListener(new OnTouchListener() { private float mLastX = 0, mLastY = 0; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_MOVE: float deltaX = event.getX() - mLastX; float deltaY = event.getY() - mLastY; if (Math.abs(deltaX) > 20 && Math.abs(deltaX) > Math.abs(deltaY)) { getParent().requestDisallowInterceptTouchEvent(true); } mLastX = event.getX(); mLastY = event.getY(); break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: getParent().requestDisallowInterceptTouchEvent(false); break; } return onTouchEvent(event); } }); 
+1
source

You should use NonSwipeableViewPager as follows. your pager will not work

 import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.animation.DecelerateInterpolator; import android.widget.Scroller; import java.lang.reflect.Field; public class NonSwipeableViewPager extends ViewPager { public NonSwipeableViewPager(Context context) { super(context); setMyScroller(); } public NonSwipeableViewPager(Context context, AttributeSet attrs) { super(context, attrs); setMyScroller(); } @Override public boolean onInterceptTouchEvent(MotionEvent event) { // Never allow swiping to switch between pages return false; } @Override public boolean onTouchEvent(MotionEvent event) { // Never allow swiping to switch between pages return false; } //down one is added for smooth scrolling private void setMyScroller() { try { Class<?> viewpager = ViewPager.class; Field scroller = viewpager.getDeclaredField("mScroller"); scroller.setAccessible(true); scroller.set(this, new MyScroller(getContext())); } catch (Exception e) { e.printStackTrace(); } } public class MyScroller extends Scroller { public MyScroller(Context context) { super(context, new DecelerateInterpolator()); } @Override public void startScroll(int startX, int startY, int dx, int dy, int duration) { super.startScroll(startX, startY, dx, dy, 350 /*1 secs*/); } } } 
0
source

If you are using RecyclerView in NestedScrollView, you must do this for your recycler (I use it for authentication).

 recyclerView.setNestedScrollingEnabled(false); 

I have the same script in the application I'm working on. The ViewPager does not scroll when scrolling through the horizontal recycler, but when the Recycler is at the end of the list and it has nothing to show. I hope I have the right question.

0
source

Here is my approach

 <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:fitsSystemWindows="true"> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="400dp" android:background="@color/bg" android:fitsSystemWindows="true" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsingToolbar" android:layout_width="match_parent" android:layout_height="350dp" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" app:layout_collapseMode="parallax"> <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/scale_30dp"> <ImageView android:id="@+id/ivProfile" android:layout_width="@dimen/scale_90dp" android:layout_height="@dimen/scale_90dp" /> </FrameLayout> <TextView android:id="@+id/tvUserName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/scale_15dp" android:textColor="@color/black" android:textSize="16sp" app:typeface="gotham_medium" /> <TextView android:id="@+id/tvHoots" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/scale_5dp" android:textColor="@color/text_color" android:textSize="12sp" app:typeface="gotham_book" /> <com.vanniktech.emoji.EmojiTextView android:id="@+id/tvSmiley" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/scale_10dp" android:textIsSelectable="true" app:emojiSize="25sp" /> <TextView android:id="@+id/tvMyFriends" android:layout_width="120dp" android:layout_height="wrap_content" android:layout_marginTop="@dimen/scale_15dp" android:background="@drawable/red_rect_box" android:gravity="center" android:paddingBottom="@dimen/scale_10dp" android:paddingEnd="@dimen/scale_15dp" android:paddingStart="@dimen/scale_15dp" android:paddingTop="@dimen/scale_10dp" android:text="@string/my_friends" android:textColor="@color/red" android:textSize="13sp" /> </LinearLayout> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_collapseMode="pin" app:popupTheme="@style/AppTheme.PopupOverlay" app:theme="@style/AppTheme"> <TextView android:id="@+id/tvTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/text_color" android:textSize="18sp" app:typeface="bariol_bold" /> </android.support.v7.widget.Toolbar> </android.support.design.widget.CollapsingToolbarLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:orientation="vertical"> <View android:layout_width="match_parent" android:layout_height="2px" android:background="#eae6e6" /> <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" app:tabGravity="fill" app:tabIndicatorColor="@color/red" app:tabIndicatorHeight="3dp" app:tabMode="scrollable" app:tabPaddingEnd="10dp" app:tabPaddingStart="10dp" app:tabSelectedTextColor="@color/black" app:tabTextColor="@color/light_grey" /> <View android:layout_width="match_parent" android:layout_height="2px" android:background="#eae6e6" /> </LinearLayout> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/mPager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="@dimen/scale_10dp" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

Here is my XML snippet XML

 <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerMyStory" android:layout_width="match_parent" android:layout_height="wrap_content" android:clipToPadding="false" android:paddingBottom="@dimen/scale_10dp" android:paddingLeft="@dimen/scale_10dp" /> </LinearLayout> 

This is my Java Fragment File

  recyclerMyStory = (RecyclerView) view.findViewById(R.id.recyclerMyStory); LinearLayoutManager layoutManager1 = new LinearLayoutManager(getActivity()); layoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL); recyclerMyStory.setLayoutManager(layoutManager1); recyclerMyStory.setNestedScrollingEnabled(false); storyAdapter = new YourStoryAdapter(getActivity(), myStoryList, clickListener); recyclerMyStory.setAdapter(storyAdapter); ivCreate.setOnClickListener(this); 
0
source

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


All Articles