繁体   English   中英

ViewPager内部的水平Recycler视图不滚动

[英]Horizontal Recycler View inside ViewPager not scrolling

问题

我在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所在的嵌套滚动的样子:

<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

尝试为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);
        }
    });

您应该按照以下方式使用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*/);
        }
    }
}

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

recyclerView.setNestedScrollingEnabled(false);

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

这是我的方法

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

这是我的Fragment 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM