您好,登錄后才能下訂單哦!
這篇文章給大家介紹在Android項目中使用RecyclerView實現一個上拉加載下拉刷新功能,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
核心實現
package com.example.fly.recyclerviewrefresh.base; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import com.example.fly.recyclerviewrefresh.R; import java.util.ArrayList; import java.util.List; /** * 類名: * 類描述:下拉刷新和上拉加載 * 創建人:fly * 創建日期: 2017/2/2. * 版本:V1.0 */ public abstract class RefreshActivity<T extends Object,A extends RecyclerView.Adapter> extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener { protected SwipeRefreshLayout swipeRefreshLayout; protected RecyclerView recyclerView; protected LinearLayoutManager linearLayoutManager; protected A adapter; protected List<T> lists = new ArrayList<>(); protected T dataBeanLoadMore = createBean(); protected boolean isLoadDataIng; // 默認為false 是否正在加載數據, false 不在加載, true 正在加載 protected boolean isScrollY; // 是否在Y軸(垂直方向)上滑動, false 沒滑動, true 滑動 @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(setLayout()); swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout); swipeRefreshLayout.setOnRefreshListener(this); swipeRefreshLayout.setColorSchemeResources(Refresh.colors); // 設置變化的顏色 recyclerView = (RecyclerView) findViewById(R.id.recyclerView); linearLayoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(linearLayoutManager); loadMore(); } /**作用就是能在父類中初始化公共的控件如上面的onCreate(@Nullable Bundle savedInstanceState)初始化的控件*/ protected abstract int setLayout(); public abstract T createBean(); // public abstract A createAdapter(); /**上拉加載*/ protected void loadMore() { recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { /** * * @param recyclerView 當前在滾動的RecyclerView * @param newState 當前滾動狀態 * newState有三種值: //靜止,沒有滾動 public static final int SCROLL_STATE_IDLE = 0; //正在被外部拖拽,一般為用戶正在用手指滾動 public static final int SCROLL_STATE_DRAGGING = 1; //自動滾動開始 public static final int SCROLL_STATE_SETTLING = 2; */ @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == RecyclerView.SCROLL_STATE_IDLE && isLoadDataIng == false ) { int lastVisibleItem = linearLayoutManager.findLastCompletelyVisibleItemPosition(); // 獲取最后一個item的角標 int totalItemCount = linearLayoutManager.getItemCount(); // 獲取item的總數 if (lastVisibleItem == (totalItemCount - 1)) { // 判斷滑到最后一個item if (!swipeRefreshLayout.isRefreshing()) { //不再下拉刷新狀態 if (isScrollY) { // 在Y軸(垂直方向)上有滑動 // TODO 執行上拉加載 onShowRefresh(); loadMoreData(); } } } } } /** * * @param recyclerView 當前滾動的view * @param dx 水平滾動距離 * @param dy 垂直滾動距離 */ @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); if (dy > 0) { isScrollY = true; }else{ isScrollY = false; } } }); } /**加載數據*/ public abstract void loadMoreData(); /**顯示加在更多的View*/ protected void onShowRefresh() { isLoadDataIng = true; // 正在加載數據 lists.add(dataBeanLoadMore); adapter.notifyDataSetChanged(); } /**隱藏加在更多的View*/ protected void onHintLoadMore() { lists.remove(dataBeanLoadMore); isLoadDataIng = false; // 不在加載數據 adapter.notifyDataSetChanged(); } }
package com.example.fly.recyclerviewrefresh; import android.os.Handler; import android.os.Bundle; import com.example.fly.recyclerviewrefresh.adapter.RecyclerAdapter; import com.example.fly.recyclerviewrefresh.base.Refresh; import com.example.fly.recyclerviewrefresh.base.RefreshActivity; import com.example.fly.recyclerviewrefresh.bean.DataBean; /** * 類名: * 類描述:RecyclerView刷新 方法二 * 創建人:fly * 創建日期: 2017/2/2. * 版本:V1.0 */ public class MainActivity extends RefreshActivity<DataBean,RecyclerAdapter>{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); for (int i = 0; i < 20; i++) { DataBean dataBean = new DataBean(Refresh.COMMON); dataBean.number = String.valueOf(i); lists.add(dataBean); } adapter = new RecyclerAdapter(lists,this); recyclerView.setAdapter(adapter); } @Override public void onRefresh() { new Handler().postDelayed(new Runnable() { @Override public void run() { lists.clear(); adapter.notifyDataSetChanged(); swipeRefreshLayout.setRefreshing(false); } },1000); } @Override protected int setLayout() {return R.layout.activity_main;} @Override public DataBean createBean() {return new DataBean(Refresh.LOAD_MORE);} @Override public void loadMoreData() { for (int i = 0; i < 5; i++) { DataBean dataBean = new DataBean(Refresh.COMMON); dataBean.number = String.valueOf(i) + "fly"; lists.add(dataBean); } adapter.notifyDataSetChanged(); onHintLoadMore(); } }
關于在Android項目中使用RecyclerView實現一個上拉加載下拉刷新功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。