gpt4 book ai didi

java - 使用 ViewPager 在选项卡之间切换时,RecyclerView 一次自动滚动一项

转载 作者:行者123 更新时间:2023-11-29 19:09:03 25 4
gpt4 key购买 nike

我有一个基本的布局设置:DrawerLayout,它由 Activity 的主布局和导航 View 的布局组成。主布局有一个 CoordinatorLayout 和实现折叠工具栏功能所需的所有布局,以及一个 ViewPager,我稍后在代码中用 fragment 填充它。该 fragment 的布局主要由 RecyclerView 和其他小 View 组成。

问题如下:每当我向下滚动两个选项卡中的任何一个并滑动到一个新选项卡并返回到原始选项卡时,RecyclerView 都会收到 onScrolled 回调(它不应该因为我没有' t 滚动 RV 本身,但 ViewPager) 使用负的 deltaY 参数(这意味着方向是向上的)导致从顶部开始的下一个项目因此可见。

一些注意事项:

  • deltaY 参数似乎总是等于下一个项目从顶部开始的高度(这给人的印象是 RV 会在整个过程发生时向上滚动一个项目)
  • 我没有将 OnPageChangeListener 设置为 ViewPager 以获得通知每当滚动页面、选择页面或更改状态时。
  • 我向 fragment 的 RecyclerView 提供 RecyclerViewScrollListener,以便在滚动 RV 以执行某些逻辑时收到通知(例如将此事件传播到 Activity 以显示或隐藏 float 操作按钮)
  • 我已经截取了调用 onScrolled 时的调用堆栈。也许有人会觉得它有用。 Take a look .
  • 我还制作了一个短视频来展示这种行为。 Take a look .
  • 我在 Internet 上查了很多资源,但都没有帮助我解决这个问题。随意问任何问题。任何帮助将不胜感激。

dashboard_activity_layout.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();

}




}

最佳答案

使用此功能一次自动滚动一项

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

编码愉快

关于java - 使用 ViewPager 在选项卡之间切换时,RecyclerView 一次自动滚动一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46045349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com