【问题标题】:Horizontal Recycler View inside ViewPager not scrollingViewPager 内的水平回收器视图不滚动
【发布时间】:2017-11-30 18:33:01
【问题描述】:

问题

我在 ViewPager 内的 NestedScrollView 中有一个水平 RecyclerView。现在,当我尝试滚动 RecyclerView 时,有时它会滚动,但有时只有 ViewPager 滚动。

代码

这就是我的 RecyclerView XML 的样子:

<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"/>

这就是 RecyclerView 所在的 Nested Scroll 的样子:

<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"
    >

这是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" />

我正在使用这个自定义 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);
    }
}

我的方法

到目前为止我所尝试的,如您所见,我编写了一个自定义 ViewPager。我试图告诉 ViewPager,如果滚动来自 RecyclerView,它就无法滚动。但是这不起作用。

日志:

这是 ViewPager 滚动而不是 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 V.E...... ........
> 0,0-1080,2860 #7f090199 app:id/movie_overview_holder}
>     06-27 17:50:53.506 32362-32362/com.mt.moviesiwanttowatch E/PAGER: IS

【问题讨论】:

  • 尝试设置 ViewCompat.setNestedScrollingEnabled(recycleView,false);
  • 我做了,但没有帮助

标签: java android xml android-layout android-recyclerview


【解决方案1】:

尝试为recycleView设置onTouchListener:

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);
        }
    });

【讨论】:

    【解决方案2】:

    您应该按照以下方式使用 NonSwipeableViewPager。您的寻呼机将无法工作

    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*/);
            }
        }
    }
    

    【讨论】:

    • 嗯,这样可以,但我不想从寻呼机中删除滚动选项
    【解决方案3】:

    如果您在 NestedScrollView 中使用 RecyclerView,您应该对您的回收器执行此操作(我将它用于真正的回收器)。

    recyclerView.setNestedScrollingEnabled(false);
    

    我正在开发的应用程序的场景完全相同。 ViewPager 不是在我滚动 Horizo​​ntal Recycler 时滚动,而是在 Recycler 位于列表末尾并且没有任何内容可显示时滚动。 我希望我的问题是正确的

    【讨论】:

      【解决方案4】:

      这是我的方法

      <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" />
      

      这是我的片段 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>
      

      这是我的片段 Java 文件

       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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-07
        相关资源
        最近更新 更多