摘要:安卓展示列表的加載與刷新剛開(kāi)始學(xué)習(xí)安卓開(kāi)發(fā),對(duì)于信息型應(yīng)用數(shù)據(jù)的加載刷新是必不可少的。使用可以容易的實(shí)現(xiàn)數(shù)據(jù)的加載與刷新,我通過(guò)學(xué)習(xí)參考網(wǎng)上實(shí)例實(shí)現(xiàn)了這兩個(gè)功能。
安卓展示列表的加載與刷新
剛開(kāi)始學(xué)習(xí)安卓開(kāi)發(fā),對(duì)于信息型應(yīng)用數(shù)據(jù)的加載刷新是必不可少的。使用RecyclerView可以容易的實(shí)現(xiàn)數(shù)據(jù)的加載與刷新,我通過(guò)學(xué)習(xí)RecyclerView參考網(wǎng)上實(shí)例實(shí)現(xiàn)了這兩個(gè)功能。
運(yùn)行效果:
向上加載時(shí)添加自定義的加載提示
關(guān)于下拉刷新的實(shí)現(xiàn)
使用SwipeRefreshLayout控件,實(shí)現(xiàn)自己的SwipeRefreshLayout.OnRefreshListener()接口
google提供加載的方式有兩種,可以自由選擇
頂部水平進(jìn)度條
下拉圓形刷新按鈕
這部分代碼比較簡(jiǎn)單
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 lists;//例子中不涉及后臺(tái)數(shù)據(jù),隨意編造的數(shù)據(jù)列表 private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 0://刷新數(shù)據(jù) for(int i=0;i<10;i++){ Data data=new Data(); data.setText("我是刷新的數(shù)據(jù)"+i); lists.add(i,data); } baseRecyclerViewAdapter.notifyDataSetChanged(); swipeRefreshLayout.setRefreshing(false); break; case 1://加載數(shù)據(jù) for(int i=0;i<10;i++){ Data data=new Data(); data.setText("我是加載的數(shù)據(jù)"+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); } //數(shù)據(jù)的初始化 private void initData(){ //recyclerView 的相關(guān)初始化 recyclerView.setLayoutManager(new LinearLayoutManager(this)); //SimpleItemDecoration 的第一個(gè)參數(shù)代表item距離左右以及上下的距離,第二個(gè)參數(shù)用于判斷水平最后一個(gè)item(多列的情況) //第二個(gè)參數(shù)的值要和StaggeredGridLayoutManager的第一個(gè)參數(shù)的值相等 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("我是初始化的數(shù)據(jù)"+i); lists.add(data); } newsAdapter =new NewsAdapter(lists); newsAdapter.setListener(this); baseRecyclerViewAdapter = new BaseRecyclerViewAdapter<>(newsAdapter); //添加頭部 baseRecyclerViewAdapter.addHeader(header); recyclerView.setAdapter(baseRecyclerViewAdapter); } //刷新的監(jiān)聽(tīng) @Override public void onRefresh() { handler.sendEmptyMessageDelayed(0, 2000); } //adapter的item的點(diǎn)擊監(jiān)聽(tīng) @Override public void click(int position) { ToastUtils.shortToast(this,lists.get(position).getText()); } //加載的監(jiān)聽(tīng) @Override public void loadMore() { AnimationUtils.showAndHide(textView,"正在加載數(shù)據(jù)..."); handler.sendEmptyMessageDelayed(1,2000); } }
關(guān)于上拉加載的實(shí)現(xiàn)
自定義上拉加載的recyclerview,這里判斷上拉操作時(shí)事件action_move里要獲得
手指位置,如果結(jié)束位置小于開(kāi)始位置,就是上拉,且上拉后還要確定當(dāng)前屏幕最后一項(xiàng)數(shù)據(jù)就是初始化數(shù)據(jù)里的最后一項(xiàng)才執(zhí)行l(wèi)istener
public class MyRecyclerView extends RecyclerView { //最后一個(gè)可見(jiàn)的布局的位置 private int lastVisibleItemPosition; private onLoadMoreListener listener; private boolean isInit;//是否初始化 private BaseRecyclerViewAdapter baseRecyclerViewAdapter; private float startY;//手指開(kāi)始的位置 private float endY;//手指結(jié)束的位置 public static boolean isLoading;//避免重復(fù)加載 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: //當(dāng)正在執(zhí)行加載的操作時(shí),屏蔽掉多余的加載操作,直至該加載完成之后執(zhí)行第二次加載的操作 if(!isLoading){ endY=ev.getY(); //此時(shí)滑動(dòng)到底部并且為上拉的動(dòng)作,執(zhí)行加載的操作方法 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; } }
自定義加載時(shí)的動(dòng)畫(huà)提示
//提示信息的顯示動(dòng)畫(huà)以及隱藏 //這里在myRecyclerView中定義了一個(gè)是否正在加載的變量,這樣可以避免用戶(hù)不斷上拉時(shí)后臺(tái)一直傳輸數(shù)據(jù) 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...
大神的實(shí)現(xiàn):https://github.com/Chanven/Co...
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/67142.html
摘要:前言安卓開(kāi)發(fā)者都知道,比要靈活的多,但不可否認(rèn)的里面的坑也同樣埋了不少人。下面讓我們看看騰訊開(kāi)發(fā)工程師用實(shí)例講解自己踩坑時(shí)的解決方案和心路歷程。 前言 安卓開(kāi)發(fā)者都知道,RecyclerView比ListView要靈活的多,但不可否認(rèn)的里面的坑也同樣埋了不少人。下面讓我們看看騰訊開(kāi)發(fā)工程師用實(shí)例講解自己踩坑時(shí)的解決方案和心路歷程。 話說(shuō)有圖有真相,首先來(lái)對(duì)比一下局部刷新前后的效果: 優(yōu)...
閱讀 3049·2021-11-22 15:29
閱讀 1729·2021-10-12 10:11
閱讀 1751·2021-09-04 16:45
閱讀 2229·2021-08-25 09:39
閱讀 2790·2021-08-18 10:20
閱讀 2509·2021-08-11 11:17
閱讀 447·2019-08-30 12:49
閱讀 3305·2019-08-30 12:49