RecyclerView滚动,但不会触发scrollListener

时间:2016-11-16 03:44:13

标签: android android-recyclerview onscrolllistener

我有一个带有recyclerView的片段(FragmentA)。当我将片段设置为viewpager的项目时,recyclelerview的滚动和监听器工作正常:

public Fragment getItem(int position) {
    final Fragment result;
    switch (position) {
        case 0:
            result = new FragmentA();
            break;
        case 1:
            result = new FragmentB();
            break;
        case 2:
            result = new FragmentC();
            break;
        default:
            result = null;
            break;
    }

    return result;
}

当我尝试使用FragmentA替换在viewpager上注册的另一个片段时出现问题:

FragmentA fragment = new FragmentA();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.addToBackStack(null);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    transaction.replace(R.id.frame, fragment).commit();

在这种情况下,recyclelerview的滚动仍然按预期工作,但是没有触发监听器。

FragmentA:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_a, container, false);

    recyclerView = (RecyclerView) view.findViewById(R.id.list);

    recyclerView.setNestedScrollingEnabled(false);

    LinearLayoutManager laym = new LinearLayoutManager(getActivity());

    PrincipalActivity principal = (PrincipalActivity) getActivity();

    recyclerView.setLayoutManager(laym);

    adpt = new CategoriaAdapter(principal.getListCategoria(), getActivity());        

    recyclerView.setAdapter(adpt);

    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            //NEVER CALLED
            super.onScrolled(recyclerView, dx, dy);
        }
    });

    return view;
}

主要布局文件:

<?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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="br.com.domain.app.PrincipalActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/tabanim_appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.NoActionBar.AppBarOverlay">

    <FrameLayout
        android:id="@+id/toolbar_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.Toolbar
            android:id="@+id/tabanim_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"

            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.NoActionBar.PopupOverlay">

        </android.support.v7.widget.Toolbar>

        <com.miguelcatalan.materialsearchview.MaterialSearchView
            android:id="@+id/search_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
    </FrameLayout>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabanim_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorColor="@android:color/white"
        app:tabMode="fixed"
        app:tabGravity="fill"
        >

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

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

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

viewpager位于FrameLayout中的片段内。

片段布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="br.com.domain.app.FragmentA"
android:background="#eeeeee"
android:id="@+id/frameList"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>

<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/fragment_a" />

</LinearLayout>

0 个答案:

没有答案