recyclerview一次自动滚动一个项目

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

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.design.widget.CoordinatorLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:id="@+id/coordinatorLayout"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent">
  9. <android.support.design.widget.AppBarLayout
  10. android:id="@+id/appBarLayout"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:elevation="@dimen/toolbar_shadow_height">
  14. <android.support.v7.widget.Toolbar
  15. android:id="@+id/toolbar"
  16. android:layout_width="match_parent"
  17. android:layout_height="@dimen/toolbar_height"
  18. android:paddingTop="@dimen/toolbar_padding_top"
  19. app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
  20. app:titleTextAppearance="@style/TitleTextView"
  21. tools:background="@color/colorPrimary"/>
  22. <android.support.design.widget.TabLayout
  23. android:id="@+id/tabLayout"
  24. android:layout_width="match_parent"
  25. android:layout_height="@dimen/dashboard_activity_content_tab_layout_height"
  26. app:tabMode="fixed"
  27. app:tabGravity="fill"
  28. tools:background="@color/colorPrimary"/>
  29. </android.support.design.widget.AppBarLayout>
  30. <android.support.v4.view.ViewPager
  31. android:id="@+id/viewPager"
  32. android:layout_width="match_parent"
  33. android:layout_height="match_parent"
  34. android:overScrollMode="never"
  35. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  36. tools:background="@color/colorPrimaryDark"/>
  37. <android.support.design.widget.FloatingActionButton
  38. android:id="@+id/actionButtonFab"
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. android:layout_margin="@dimen/dashboard_activity_content_fab_margin"
  42. android:layout_gravity="bottom|end"
  43. android:src="@mipmap/ic_search_white_24dp"
  44. app:fabSize="normal"
  45. app:useCompatPadding="true"
  46. tools:backgroundTint="@color/colorAccent"/>

dashboardactivity.java(相关部分):

  1. private void initViewPager() {
  2. mViewPager = findViewById(R.id.viewPager);
  3. mAdapter = new DashboardViewPagerAdapter(getSupportFragmentManager());
  4. mAdapter.setViewPagerId(mViewPager.getId());
  5. mAdapter.setRecyclerViewStateListener(mRecyclerViewStateListener);
  6. populateAdapter();
  7. mViewPager.setAdapter(mAdapter);
  8. mViewPager.setOffscreenPageLimit(mAdapter.getCount());
  9. }
  10. private void populateAdapter() {
  11. BaseFragment baseFragment = mAdapter.getFragmentForPosition(TAB_TAB_1);
  12. if(baseFragment == null) {
  13. baseFragment = SomeFragment.newInstance(Common.SOME_TYPE_1);
  14. }
  15. mAdapter.addFragment(baseFragment);
  16. baseFragment = mAdapter.getFragmentForPosition(TAB_TAB_2);
  17. if(baseFragment == null) {
  18. baseFragment = SomeFragment.newInstance(Common.SOME_TYPE_2);
  19. }
  20. mAdapter.addFragment(baseFragment);
  21. baseFragment = mAdapter.getFragmentForPosition(TAB_TAB_3);
  22. if(baseFragment == null) {
  23. baseFragment = SomeOtherFragment.init();
  24. }
  25. mAdapter.addFragment(baseFragment);
  26. }
  27. private RecyclerViewStateListener mRecyclerViewStateListener = new RecyclerViewStateListener() {
  28. @Override
  29. public void onScrolledDownwards(RecyclerView recyclerView, int deltaY) {
  30. // Hiding the FAB by animating it
  31. DashboardCommon.hideActionButton(mActionButtonFab, mViewAnimator, true);
  32. }
  33. @Override
  34. public void onScrolledUpwards(RecyclerView recyclerView, int deltaY) {
  35. // Showing the FAB by animating it
  36. DashboardCommon.showActionButton(mActionButtonFab, mViewAnimator, true);
  37. }
  38. };

somefragment.java(相关部分):

  1. private void initRecyclerView() {
  2. mRecyclerView = findViewById(R.id.recyclerView);
  3. mRecyclerView.addOnScrollListener(new RecyclerViewScrollListener(this));
  4. mLayoutManager = new LinearLayoutManager(
  5. getContext(),
  6. LinearLayoutManager.VERTICAL,
  7. false
  8. );
  9. mRecyclerView.setLayoutManager(mLayoutManager);
  10. mAdapter = new SomeRecyclerViewAdapter(getContext(), mItems);
  11. mRecyclerView.setAdapter(mAdapter);
  12. }
  13. // mRecyclerViewStateListener is the listener passed from the
  14. // DashboardActivity
  15. @CallSuper
  16. public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
  17. if(mRecyclerViewStateListener != null) {
  18. mRecyclerViewStateListener.onScrollStateChanged(recyclerView, newState);
  19. }
  20. }
  21. @Override
  22. public void onScrolled(RecyclerView recyclerView, int deltaX, int deltaY) {
  23. if(mRecyclerViewStateListener != null) {
  24. mRecyclerViewStateListener.onScrolled(recyclerView, deltaX, deltaY);
  25. }
  26. }
  27. @Override
  28. public void onScrolledDownwards(RecyclerView recyclerView, int deltaY) {
  29. if(mRecyclerViewStateListener != null) {
  30. mRecyclerViewStateListener.onScrolledDownwards(recyclerView, deltaY);
  31. }
  32. }
  33. @Override
  34. public void onScrolledUpwards(RecyclerView recyclerView, int deltaY) {
  35. if(mRecyclerViewStateListener != null) {
  36. mRecyclerViewStateListener.onScrolledUpwards(recyclerView, deltaY);
  37. }
  38. }
  39. @Override
  40. public void onBottomReached() {
  41. if(mRecyclerViewStateListener != null) {
  42. mRecyclerViewStateListener.onBottomReached();
  43. }
  44. }
  45. @Override
  46. public void onMidpointReached(int direction) {
  47. if(mRecyclerViewStateListener != null) {
  48. mRecyclerViewStateListener.onMidpointReached(direction);
  49. }
  50. }
  51. @Override
  52. public void onTopReached() {
  53. if(mRecyclerViewStateListener != null) {
  54. mRecyclerViewStateListener.onTopReached();
  55. }
  56. }

recyclerviewscrolllistener.java(相关部分):

  1. public class RecyclerViewScrollListener extends RecyclerView.OnScrollListener {
  2. public static final int DIRECTION_UNSPECIFIED = -1;
  3. public static final int DIRECTION_UPWARDS = 0;
  4. public static final int DIRECTION_DOWNWARDS = 1;
  5. // Omitted...
  6. private StateListener mStateListener;
  7. public RecyclerViewScrollListener(StateListener stateListener) {
  8. // Omitted..
  9. mStateListener = stateListener;
  10. }
  11. @Override
  12. public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
  13. if(mStateListener != null) {
  14. mStateListener.onScrollStateChanged(recyclerView, newState);
  15. }
  16. }
  17. @Override
  18. public void onScrolled(RecyclerView recyclerView, int deltaX, int deltaY) {
  19. if(mStateListener != null) {
  20. mStateListener.onScrolled(recyclerView, deltaX, deltaY);
  21. }
  22. f(deltaY > 0) {
  23. // Recycler view's contents are moving downwards
  24. // Notifying about the downwards scroll
  25. if(mStateListener != null) {
  26. mStateListener.onScrolledDownwards(recyclerView, deltaY);
  27. }
  28. // Omitted...
  29. if(someConditionIsTrue) {
  30. // Omitted...
  31. // Notifying the listener
  32. mStateListener.onBottomReached();
  33. } else if((someOtherConditionIsTrue) {
  34. mStateListener.onMidpointReached(DIRECTION_DOWNWARDS);
  35. }
  36. } else if(deltaY < 0) {
  37. // Recycler view's contents are moving upwards
  38. // Notifying about upwards scroll
  39. if(mStateListener != null) {
  40. mStateListener.onScrolledUpwards(recyclerView, deltaY);
  41. }
  42. // Omitted...
  43. if(someConditionIsTrue) {
  44. // Omitted..
  45. // Notifying the listener
  46. mStateListener.onTopReached();
  47. } else if(someOtherConditionIsTrue) {
  48. mStateListener.onMidpointReached(DIRECTION_UPWARDS);
  49. }
  50. }
  51. }
  52. public interface StateListener {
  53. void onScrollStateChanged(RecyclerView recyclerView, int newState);
  54. void onScrolled(RecyclerView recyclerView, int deltaX, int deltaY);
  55. void onScrolledDownwards(RecyclerView recyclerView, int deltaY);
  56. void onScrolledUpwards(RecyclerView recyclerView, int deltaY);
  57. void onBottomReached();
  58. void onMidpointReached(int direction);
  59. void onTopReached();
  60. }
  61. }
xpszyzbs

xpszyzbs1#

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

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

快乐,干杯

gkl3eglg

gkl3eglg2#

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

ql3eal8s

ql3eal8s3#

希望会有帮助

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

相关问题