2016-07-15 143 views
1

我有一個swipeRefresh裏面的回收器視圖每當我嘗試在頂部滾動滑動刷新方法被調用和佈局刷新,但我只想刷新佈局滾動到屏幕的頂部。有沒有人有這個問題的答案。如何刷新刷新佈局android

任何幫助表示讚賞,謝謝

我的佈局如下所示。

<android.support.v4.widget.SwipeRefreshLayout 
    android:id="@+id/swipe_container" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <RelativeLayout 
     android:id="@+id/rel" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_collapseMode="pin" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recycler" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@id/toolbar" 
      android:scrollbars="vertical" /> 
    </RelativeLayout> 

</android.support.v4.widget.SwipeRefreshLayout> 
SwipeRefreshLayout

upload_swipe_refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
     @Override 
     public void onRefresh() { 

      webServiceCall(); 
      upload_swipe_refresh_layout.setRefreshing(false); 
     } 
    }); 

+0

爲什麼'RecyclerView'和'SwipeRefreshLayout'之間有'RelativeLayout'? 「SwipeRefreshLayout」不應該是「RecyclerView」的直接父代嗎?請參閱https://developer.android.com/training/swipe/add-swipe-in​​terface.html – frozenkoi

回答

2

我解決我的問題。我不得不重新安排我的代碼才能正常工作。 現在,在到達屏幕頂部時,刷新刷新。

<RelativeLayout 
    android:id="@+id/rel" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     app:layout_collapseMode="pin" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

    <android.support.v4.widget.SwipeRefreshLayout 
     android:id="@+id/swipe_container" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/toolbar"> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recycler" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@id/swipe_container" 
      android:scrollbars="vertical" /> 

    </android.support.v4.widget.SwipeRefreshLayout> 
</RelativeLayout> 
1

使用setOnRefreshListener方法對於不斷刷新

upload_swipe_refresh_layout.post(new Runnable() { 
    @Override 
    public void run() { 
     upload_swipe_refresh_layout.setRefreshing(true); 
    } 
}); 
+0

我正在使用setOnRefreshListener,但它每次嘗試向上滾動時都會調用它。而我想刷新到達屏幕的頂部。 –

+0

然後你可以保持它一直刷新 –

+0

我編輯了我的答案 –