使用SwipeRefreshLayout,實現下拉刷新

RegPoate 8年前發布 | 4K 次閱讀 Java Android

編輯你的 app / build.gradle 文件包括一個支持庫比版本19新:

SwipeRefreshLayout 是一個ViewGroup只能持有一個可滾動視圖。 這可以是一個 滾動視圖 或者一個 AdapterView 如 列表視圖 或者一個 RecyclerView 。
</div>

 

ListView SwipeRefreshLayout 步驟1:包裝列表視圖    

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipeContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <ListView
      android:id="@+id/lvItems"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true" >
  </ListView>

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

第二步:設置SwipeRefreshLayout    

public class TimelineActivity extends Activity {
    private SwipeRefreshLayout swipeContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swipeContainer);
        // Setup refresh listener which triggers new data loading
        swipeContainer.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                // Your code to refresh the list here.
                // Make sure you call swipeContainer.setRefreshing(false)
                // once the network request has completed successfully.
                fetchTimelineAsync(0);
            } 
        });
        // Configure the refreshing colors
        swipeContainer.setColorSchemeResources(android.R.color.holo_blue_bright, 
                android.R.color.holo_green_light, 
                android.R.color.holo_orange_light, 
                android.R.color.holo_red_light);
    }

    public void fetchTimelineAsync(int page) {
        client.getHomeTimeline(0, new JsonHttpResponseHandler() {
            public void onSuccess(JSONArray json) {
                // Remember to CLEAR OUT old items before appending in the new ones
                adapter.clear();
                // ...the data has come back, add new items to your adapter...
                adapter.addAll(...);
                // Now we call setRefreshing(false) to signal refresh has finished
                swipeContainer.setRefreshing(false);
            }

            public void onFailure(Throwable e) {
                Log.d("DEBUG", "Fetch timeline error: " + e.toString());
            }
        });
    }
}


/*請注意 ,重新加載成功后,我們還必須刷新完成后通過調用的信號 setRefreshing(假) 。 還要注意,你應該 清除舊的物品 在刷新之前添加新的。*/

[代碼]SwipeRefreshLayout RecyclerView 步驟1:包裝RecyclerView    

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipeContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <android.support.v7.widget.RecyclerView
      android:id="@+id/rvItems"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true" />

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

[代碼]步驟2:更新RecyclerView.Adapter    

/* Within the RecyclerView.Adapter class */

// Clean all elements of the recycler
public void clear() { 
    items.clear(); 
    notifyDataSetChanged(); 
}

// Add a list of items
public void addAll(List<list> list) { 
    items.addAll(list); 
    notifyDataSetChanged(); 
}

[代碼]步驟3:設置SwipeRefreshLayout    

/*接下來,我們需要配置 SwipeRefreshLayout 在視圖初始化的活動。 實例化的活動 SwipeRefreshLayout 

應該添加一個 OnRefreshListener 通知只要刷新動作完成。

 SwipeRefreshLayout 將通知偵聽器再次完成每一次的姿態,監聽器負責正確確定何時來發起一個刷新的內容。*/
 本文由用戶 RegPoate 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!