我有一个基本的布局设置:DrawerLayout,包含活动的主要布局和导航视图的布局。主要布局有一个CoordinatorLayout和所有必要的实现折叠工具栏功能,以及一个ViewPager,我稍后在代码中填充片段。片段的布局主要包括RecyclerView和其他次要视图。
问题如下:每当我向下滚动两个选项卡中的任意一个并滑动到新选项卡并返回到原始选项卡时,RecyclerView会收到onScrolled回调(它不应该因为我没有' t滚动RV本身,但ViewPager)具有负deltaY参数(这意味着方向向上),这导致顶部的下一个项目因此而进入可见性。
几点说明:
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();
}
}
答案 0 :(得分:2)
自动使用此卷轴一次
RecyclerView my_recycler_view= (RecyclerView) findViewById(R.id.my_recycler_view);
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(my_recycler_view);
快乐的编码干杯
答案 1 :(得分:0)
希望会有所帮助
RecyclerView my_recycler_view= (RecyclerView) findViewById(R.id.my_recycler_view);
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(my_recycler_view);
答案 2 :(得分:0)
即使我在2年前发布了这个问题,我还是决定提供答案。迟到总比没有好,对吧?
据我所知,问题在于RecyclerView
项的布局具有TextView
,其中包含android:textIsSelectable="true"
属性。事实证明,通过删除该属性,问题消失了。您可能会问,我如何实现文本选择呢?老实说,我记得很久了。
无论如何,如果您遇到这样的问题,请尝试删除android:textIsSelectable="true"
属性,它很可能会为您解决问题。