簡體   English   中英

CoordinatorLayout中的Recyclerview-無休止的滾動問題

[英]Recyclerview inside CoordinatorLayout - Endless scrolling issue

當在coordinatorlayout中使用recyclerview時,滾動無止境地出現問題。 這是我的主要活動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"
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.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabTextColor="@android:color/white"
        app:tabSelectedTextColor="@android:color/white"
        app:tabIndicatorColor="@android:color/white"
        app:tabIndicatorHeight="6dp"/>


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



<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>



<android.support.design.widget.FloatingActionButton
    android:id="@+id/fabButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/createlook"
    android:layout_gravity="end|bottom"
    android:layout_margin="@dimen/fab_margin"
    app:borderWidth="0dp"
    app:layout_behavior="com.abhishek.materialdesign.ScrollingFABBehavior" />


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

設置了viewpager以使用片段。 這是片段xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abhishek.materialdesign.FirstFragment">

<!-- TODO: Update blank fragment layout -->
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/first_recycler_view">

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

<ProgressBar
    android:id="@+id/progress_bar"
    style="?android:attr/progressBarStyleInverse"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:indeterminate="true"
    android:visibility="gone" />

<ProgressBar
    android:id="@+id/progress_bar_paging"
    style="?android:attr/progressBarStyleInverse"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:indeterminate="true"
    android:visibility="gone" />

</RelativeLayout>

我想通過以上recyclerview實現無盡的滾動。 這是代碼:

private int visibleThreshold = 5;
int  visibleItemCount, totalItemCount, firstVisibleItem ;
static boolean loadingMore = true;
static boolean noMoreDataOnServer = false;
private int previousTotal = 0;
firstRecyclerView =      (RecyclerView)view.findViewById(R.id.first_recycler_view);
gridLayoutManager = new GridLayoutManager(getActivity(),2);
firstRecyclerView.setLayoutManager(gridLayoutManager);
firstRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int    newState) {
            // TODO Auto-generated method stub
            super.onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if(recyclerView.getAdapter() != null && !noMoreDataOnServer){

               // mRecyclerViewHelper = RecyclerViewPositionHelper.createHelper(recyclerView);
                visibleItemCount = firstRecyclerView.getChildCount();
                totalItemCount = gridLayoutManager.getItemCount();
                firstVisibleItem = gridLayoutManager.findFirstVisibleItemPosition();

                if (loadingMore) {
                    if (totalItemCount > previousTotal) {
                        loadingMore = false;
                        previousTotal = totalItemCount;
                    }
                }

                if (!loadingMore && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
                    // End has been reached
                    currentPage++;
                    new FetchMoviesData(currentPage).execute(movieType);
                    loadingMore = true;
                }
            }
        }
    });

問題在於,甚至在我們還沒有開始滾動時,多次執行獲取更多數據的塊。

嘗試此代碼。

@Override
public void onScrolled(RecyclerView recyclerViewa, int dx, int dy) {
    super.onScrolled(recyclerViewa, dx, dy);

    int th = 4;
    int count = gridLayoutManager.getItemCount();

    if (layoutManager1.findLastCompletelyVisibleItemPosition() >= count
            - th) {
        if (!loadingMore) {
            loadingMore = true;
            currentPage++;
            new FetchMoviesData(currentPage).execute(movieType);
        }
    }
}

和內onPostExecute()FetchMoviesData使loadingMore再假。

protected void onPostExecute(String result) {
    super.onPostExecute(result);
    loading = false;
    ....
}

編碼愉快。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM