剛開始學習安卓開發,對於信息型應用數據的加載刷新是必不可少的。使用RecyclerView能夠容易的實現數據的加載與刷新,我經過學習RecyclerView參考網上實例實現了這兩個功能。
運行效果:
git
向上加載時添加自定義的加載提示
github
關於下拉刷新的實現ide
使用SwipeRefreshLayout控件,實現本身的SwipeRefreshLayout.OnRefreshListener()接口
google提供加載的方式有兩種,能夠自由選擇佈局
頂部水平進度條post
下拉圓形刷新按鈕學習
這部分代碼比較簡單動畫
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener,NewsAdapter.onItemClickListener ,BaseRecyclerView.onLoadMoreListener{ private SwipeRefreshLayout swipeRefreshLayout; private BaseRecyclerView recyclerView; private TextView textView; private BaseRecyclerViewAdapter baseRecyclerViewAdapter; private NewsAdapter newsAdapter; private ArrayList<Data> lists;//例子中不涉及後臺數據,隨意編造的數據列表 private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 0://刷新數據 for(int i=0;i<10;i++){ Data data=new Data(); data.setText("我是刷新的數據"+i); lists.add(i,data); } baseRecyclerViewAdapter.notifyDataSetChanged(); swipeRefreshLayout.setRefreshing(false); break; case 1://加載數據 for(int i=0;i<10;i++){ Data data=new Data(); data.setText("我是加載的數據"+i); lists.add(data); } baseRecyclerViewAdapter.notifyDataSetChanged(); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } //佈局的初始化 private void initView(){ swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefresh); swipeRefreshLayout.setOnRefreshListener(this); swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.blue_light), getResources().getColor(R.color.green_light), getResources().getColor(R.color.orange_light), getResources().getColor(R.color.red_light)); recyclerView= (BaseRecyclerView) findViewById(R.id.recycleview); recyclerView.setListener(this); textView= (TextView) findViewById(R.id.text); } //數據的初始化 private void initData(){ //recyclerView 的相關初始化 recyclerView.setLayoutManager(new LinearLayoutManager(this)); //SimpleItemDecoration 的第一個參數表明item距離左右以及上下的距離,第二個參數用於判斷水平最後一個item(多列的狀況) //第二個參數的值要和StaggeredGridLayoutManager的第一個參數的值相等 recyclerView.addItemDecoration(new SimpleItemDecoration(40,1)); View header= LayoutInflater.from(this).inflate(R.layout.recyclerview_head,null); lists=new ArrayList<>(); for(int i=0;i<10;i++){ Data data=new Data(); data.setText("我是初始化的數據"+i); lists.add(data); } newsAdapter =new NewsAdapter(lists); newsAdapter.setListener(this); baseRecyclerViewAdapter = new BaseRecyclerViewAdapter<>(newsAdapter); //添加頭部 baseRecyclerViewAdapter.addHeader(header); recyclerView.setAdapter(baseRecyclerViewAdapter); } //刷新的監聽 @Override public void onRefresh() { handler.sendEmptyMessageDelayed(0, 2000); } //adapter的item的點擊監聽 @Override public void click(int position) { ToastUtils.shortToast(this,lists.get(position).getText()); } //加載的監聽 @Override public void loadMore() { AnimationUtils.showAndHide(textView,"正在加載數據..."); handler.sendEmptyMessageDelayed(1,2000); } }
關於上拉加載的實現this
自定義上拉加載的recyclerview,這裏判斷上拉操做時事件action_move裏要得到
手指位置,若是結束位置小於開始位置,就是上拉,且上拉後還要肯定當前屏幕最後一項數據就是初始化數據裏的最後一項才執行listenergoogle
public class MyRecyclerView extends RecyclerView { //最後一個可見的佈局的位置 private int lastVisibleItemPosition; private onLoadMoreListener listener; private boolean isInit;//是否初始化 private BaseRecyclerViewAdapter baseRecyclerViewAdapter; private float startY;//手指開始的位置 private float endY;//手指結束的位置 public static boolean isLoading;//避免重複加載 public BaseRecyclerView(Context context) { this(context,null,0); } public BaseRecyclerView(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public BaseRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); Adapter adapter = getAdapter(); if (!(adapter instanceof BaseRecyclerViewAdapter)) { throw new IllegalArgumentException("the adapter must extents BaseRecyclerViewAdapter"); } baseRecyclerViewAdapter = (BaseRecyclerViewAdapter) adapter; } @Override public boolean dispatchTouchEvent(MotionEvent ev) { LayoutManager layoutManager = getLayoutManager(); if (layoutManager instanceof LinearLayoutManager) { lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition(); }else if (layoutManager instanceof GridLayoutManager) { lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition(); }else if (layoutManager instanceof StaggeredGridLayoutManager) { int[] last = null; if (!isInit) { last = new int[((StaggeredGridLayoutManager) layoutManager).getSpanCount()]; isInit = true; } int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) layoutManager).findLastCompletelyVisibleItemPositions(last); for (int i : lastVisibleItemPositions) { lastVisibleItemPosition = i > lastVisibleItemPosition ? i : lastVisibleItemPosition; } } switch (ev.getAction()){ case MotionEvent.ACTION_DOWN: startY=ev.getY(); break; case MotionEvent.ACTION_MOVE: //當正在執行加載的操做時,屏蔽掉多餘的加載操做,直至該加載完成以後執行第二次加載的操做 if(!isLoading){ endY=ev.getY(); //此時滑動到底部而且爲上拉的動做,執行加載的操做方法 if(( endY-startY) < 0 && lastVisibleItemPosition == baseRecyclerViewAdapter.getItemCount() -1 ){ if(listener==null){ break; } listener.loadMore(); isLoading=true; } } break; case MotionEvent.ACTION_UP: startY=0; endY=0; break; case MotionEvent.ACTION_CANCEL: startY=0; endY=0; break; } return super.dispatchTouchEvent(ev); } public interface onLoadMoreListener{ void loadMore(); } public void setListener(onLoadMoreListener listener){ this.listener=listener; } }
自定義加載時的動畫提示spa
//提示信息的顯示動畫以及隱藏 //這裏在myRecyclerView中定義了一個是否正在加載的變量,這樣能夠避免用戶不斷上拉時後臺一直傳輸數據 public static void showAndHide(final TextView textView , String message){ textView.setVisibility(View.VISIBLE); textView.setText(message); textView.setAnimation(moveToViewLocation()); new Handler().postDelayed(new Runnable() { @Override public void run() { textView.setVisibility(View.GONE); textView.setAnimation(moveToViewBottom()); MyRecyclerView.isLoading=false; } },2000); }
個人參考:https://www.easydone.cn/2015/...
http://blog.csdn.net/leilifen...
大神的實現:https://github.com/Chanven/Co...