recyclerview一次自动滚动一个项目

pw136qt2  于 2021-07-03  发布在  Java
关注(0)|答案(3)|浏览(492)

我有一个基本的布局设置:drawerlayout,它由活动的主布局和导航视图的布局组成。主布局有一个coordinatorlayout和实现折叠工具栏功能所需的所有布局,还有一个viewpager,我在后面的代码中用片段填充了它。片段的布局主要由recyclerview和其他次要视图组成。
问题如下:每当我向下滚动两个选项卡中的任何一个并滑动到一个新选项卡并返回到原来的选项卡时,recyclerview就会收到onscrold回调(这是不应该的,因为我没有滚动rv本身,但是viewpager)具有负的deltay参数(这意味着方向是向上的),这会导致从顶部开始的下一个项目进入可见状态。
几个注意事项:
deltay参数似乎总是等于下一个项目距顶部的高度(这给人的印象是,每当整个过程发生时,rv向上滚动一个项目)
我不会将onpagechangelistener设置为viewpager,以便在页面滚动、选中或状态更改时获得通知。
我将recyclerviewscrolllistener提供给片段的recyclerview,以便在rv被滚动以执行某些逻辑时得到通知(例如将此事件传播到活动以显示或隐藏浮动操作按钮)
我已经做了一个调用onscrold时调用堆栈的屏幕截图。也许有人会觉得有用。看一看。
我还制作了一个短片来表现这种行为。看一看。
我在网上查了很多资料,但都没有帮我解决这个问题。有问题尽管问。任何帮助都将不胜感激。
Jmeter 板\u活动\u布局.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="@dimen/toolbar_shadow_height">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/toolbar_height"
            android:paddingTop="@dimen/toolbar_padding_top"
            app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
            app:titleTextAppearance="@style/TitleTextView"
            tools:background="@color/colorPrimary"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent" 
            android:layout_height="@dimen/dashboard_activity_content_tab_layout_height"
            app:tabMode="fixed"
            app:tabGravity="fill"
            tools:background="@color/colorPrimary"/>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:overScrollMode="never"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:background="@color/colorPrimaryDark"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/actionButtonFab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/dashboard_activity_content_fab_margin"
        android:layout_gravity="bottom|end"
        android:src="@mipmap/ic_search_white_24dp"
        app:fabSize="normal"
        app:useCompatPadding="true"
        tools:backgroundTint="@color/colorAccent"/>

dashboardactivity.java(相关部分):

private void initViewPager() {
    mViewPager = findViewById(R.id.viewPager);

    mAdapter = new DashboardViewPagerAdapter(getSupportFragmentManager());
    mAdapter.setViewPagerId(mViewPager.getId());
    mAdapter.setRecyclerViewStateListener(mRecyclerViewStateListener);

    populateAdapter();

    mViewPager.setAdapter(mAdapter);
    mViewPager.setOffscreenPageLimit(mAdapter.getCount());
}

private void populateAdapter() {
    BaseFragment baseFragment = mAdapter.getFragmentForPosition(TAB_TAB_1);

    if(baseFragment == null) {
        baseFragment = SomeFragment.newInstance(Common.SOME_TYPE_1);
    }

    mAdapter.addFragment(baseFragment);

    baseFragment = mAdapter.getFragmentForPosition(TAB_TAB_2);

    if(baseFragment == null) {
        baseFragment = SomeFragment.newInstance(Common.SOME_TYPE_2);
    }

    mAdapter.addFragment(baseFragment);

    baseFragment = mAdapter.getFragmentForPosition(TAB_TAB_3);

    if(baseFragment == null) {
        baseFragment = SomeOtherFragment.init();
    }

    mAdapter.addFragment(baseFragment);
}

private RecyclerViewStateListener mRecyclerViewStateListener = new RecyclerViewStateListener() {

    @Override
    public void onScrolledDownwards(RecyclerView recyclerView, int deltaY) {
        // Hiding the FAB by animating it
        DashboardCommon.hideActionButton(mActionButtonFab, mViewAnimator, true);
    }

    @Override
    public void onScrolledUpwards(RecyclerView recyclerView, int deltaY) {
        // Showing the FAB by animating it
        DashboardCommon.showActionButton(mActionButtonFab, mViewAnimator, true);
    }

};

somefragment.java(相关部分):

private void initRecyclerView() {
    mRecyclerView = findViewById(R.id.recyclerView);
    mRecyclerView.addOnScrollListener(new RecyclerViewScrollListener(this));

    mLayoutManager = new LinearLayoutManager(
        getContext(),
        LinearLayoutManager.VERTICAL,
        false
    );
    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new SomeRecyclerViewAdapter(getContext(), mItems);

    mRecyclerView.setAdapter(mAdapter);
}

// mRecyclerViewStateListener is the listener passed from the
// DashboardActivity

@CallSuper
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    if(mRecyclerViewStateListener != null) {
        mRecyclerViewStateListener.onScrollStateChanged(recyclerView, newState);
    }
}

@Override
public void onScrolled(RecyclerView recyclerView, int deltaX, int deltaY) {
    if(mRecyclerViewStateListener != null) {
        mRecyclerViewStateListener.onScrolled(recyclerView, deltaX, deltaY);
    }
}

@Override
public void onScrolledDownwards(RecyclerView recyclerView, int deltaY) {
    if(mRecyclerViewStateListener != null) {
        mRecyclerViewStateListener.onScrolledDownwards(recyclerView, deltaY);
    }
}

@Override
public void onScrolledUpwards(RecyclerView recyclerView, int deltaY) {
    if(mRecyclerViewStateListener != null) {
        mRecyclerViewStateListener.onScrolledUpwards(recyclerView, deltaY);
    }
}

@Override
public void onBottomReached() {
    if(mRecyclerViewStateListener != null) {
        mRecyclerViewStateListener.onBottomReached();
    }
}

@Override
public void onMidpointReached(int direction) {
    if(mRecyclerViewStateListener != null) {
        mRecyclerViewStateListener.onMidpointReached(direction);
    }
}

@Override
public void onTopReached() {
    if(mRecyclerViewStateListener != null) {
        mRecyclerViewStateListener.onTopReached();
    }
}

recyclerviewscrolllistener.java(相关部分):

public class RecyclerViewScrollListener extends RecyclerView.OnScrollListener {

    public static final int DIRECTION_UNSPECIFIED = -1;
    public static final int DIRECTION_UPWARDS = 0;
    public static final int DIRECTION_DOWNWARDS = 1;

    // Omitted...

    private StateListener mStateListener;

    public RecyclerViewScrollListener(StateListener stateListener) {
        // Omitted..

        mStateListener = stateListener;
    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        if(mStateListener != null) {
            mStateListener.onScrollStateChanged(recyclerView, newState);
        }
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int deltaX, int deltaY) {
        if(mStateListener != null) {
            mStateListener.onScrolled(recyclerView, deltaX, deltaY);
        }

        f(deltaY > 0) {
            // Recycler view's contents are moving downwards

            // Notifying about the downwards scroll
            if(mStateListener != null) {
                mStateListener.onScrolledDownwards(recyclerView, deltaY);
            }

            // Omitted...

            if(someConditionIsTrue) {
                // Omitted...

                // Notifying the listener
                mStateListener.onBottomReached();
            } else if((someOtherConditionIsTrue) {
                mStateListener.onMidpointReached(DIRECTION_DOWNWARDS);
            }
        } else if(deltaY < 0) {
            // Recycler view's contents are moving upwards

            // Notifying about upwards scroll
            if(mStateListener != null) {
                mStateListener.onScrolledUpwards(recyclerView, deltaY);
            }

            // Omitted...

            if(someConditionIsTrue) {
                // Omitted..

                // Notifying the listener
                mStateListener.onTopReached();
            } else if(someOtherConditionIsTrue) {
                mStateListener.onMidpointReached(DIRECTION_UPWARDS);
            }
        }
    }

    public interface StateListener {

        void onScrollStateChanged(RecyclerView recyclerView, int newState);

        void onScrolled(RecyclerView recyclerView, int deltaX, int deltaY);

        void onScrolledDownwards(RecyclerView recyclerView, int deltaY);

        void onScrolledUpwards(RecyclerView recyclerView, int deltaY);

        void onBottomReached();

        void onMidpointReached(int direction);

        void onTopReached();

    }

}
xpszyzbs

xpszyzbs1#

使用此功能,一次自动滚动一个项目

RecyclerView my_recycler_view= (RecyclerView) findViewById(R.id.my_recycler_view);
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(my_recycler_view);

快乐,干杯

gkl3eglg

gkl3eglg2#

尽管我两年多前就发布了这个问题,但我还是决定提供一个答案。迟到总比不到好,对吧?
据我所知,问题在于 RecyclerView 项目具有 TextView ,其中包含 android:textIsSelectable="true" 属性。结果发现,通过删除该属性,问题就消失了。你可能会问,我是如何实现文本选择的?老实说,我不记得了,因为那是很久以前的事了。
不管怎样,如果你有这样的问题,试着移除 android:textIsSelectable="true" 属性,它可以很好地为您解决这个问题。

ql3eal8s

ql3eal8s3#

希望会有帮助

RecyclerView my_recycler_view= (RecyclerView) findViewById(R.id.my_recycler_view);

SnapHelper snapHelper = new PagerSnapHelper();

snapHelper.attachToRecyclerView(my_recycler_view);

相关问题