2016-10-07 119 views
2

當coordinatorlayout中使用recyclerview時,我遇到無盡滾動的問題。這是我的主要活動xml文件:CoordinatorLayout中的Recyclerview - 無盡滾動問題

<?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; 
       } 
      } 
     } 
    }); 

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

回答

0

試試此代碼。

@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); 
     } 
    } 
} 

和裏面的FetchMoviesDataonPostExecute()使loadingMore再假。

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

編碼愉快。

+0

試過這個,但仍然問題。我每次抓取20個項目,gridLayoutManager.findLastCompletelyVisibleItemPosition()返回19,當我甚至沒有滾動。所以數據再次被提取... – Abhishek

+0

你試過用'firstRecyclerView.setHasFixedSize(true);'? –

+0

是的,我加了firstRecyclerView.setHasFixedSize(true); ,仍然沒有工作 – Abhishek

相關問題