
本文实例为大家分享了AndroID自定义下拉刷新上拉加载的具体实现步骤,供大家参考,具体内容如下
实现的方式是SwipeRefreshLayout + RecyclerVIEw 的VIEwType
首先看效果:
总的思路:
布局文件
<androID.support.v4.Widget.SwipeRefreshLayout androID:layout_margintop="?attr/actionbarSize" androID:ID="@+ID/one_refresh" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <androID.support.v7.Widget.RecyclerVIEw androID:ID="@+ID/one_recyclerVIEw" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" > </androID.support.v7.Widget.RecyclerVIEw> </androID.support.v4.Widget.SwipeRefreshLayout>
下拉刷新的实现思路
用于测试的Model
public class TestModel { private String mTitle; private String mDesc; private String mTime; public TestModel(String mTitle,String mDesc,String mTime) { this.mTitle = mTitle; this.mDesc = mDesc; this.mTime = mTime; } //...一堆getXxx,setXxx方法 //equals必写,添加数据时候用于判断 @OverrIDe public boolean equals(Object o) { TestModel model = (TestModel) o; if (!mTitle.equals(model.getmTitle())) { return false; } else if (!mDesc.equals(model.getmDesc())) { return false; } else if (!mTime.equals(model.getmTitle())) { return false; } return true; }模拟获取网络数据的代码
private class GetData { int size = 0 ; int max = 25; //数据的最大值 public voID setStart(int size) { this.size = size; } //根据size获取指定大小的List,最大不能超过max public List<TestModel> initData(int size) { List<TestModel> mDatas = new ArrayList<>(); TestModel model = null; for (int i = start; i < ((size + start) > max ? max : (size + start)); i++) { model = new TestModel("Title" + i,"Desc" + i,"今天 11:30"); mDatas.add(model); } start += size; return mDatas; } }数据获取并通知初始化RecyclerVIEw
public voID initData() { if (getData == null) { getData = new GetData(); } mLists = getData.initData(size); //获取默认显示的数量的item mhandler.sendEmptyMessage(REFRESH); //通知handler更新 }Handler中用于处理第一次显示数据和以后刷新 *** 作的代码
if (msg.what == REFRESH) {if (mAdapter == null) {mAdapter = new OneAdapter(mContext);mAdapter.setmDatas(mLists);//设置数据//...对适配器的设置,这里先省去,免得混淆mRecyclerVIEw.setLayoutManager(new linearlayoutmanager(mContext));mRecyclerVIEw.setAdapter(mAdapter); } else { mAdapter.setmDatas(mLists); mAdapter.cleadnCount(); mAdapter.notifyDataSetChanged(); } initRefresh(); //判断refreshLayout是否在刷新,是的话取消刷新 *** 作 .就不贴代码了显的乱糟糟RefreshLayout的刷新事件
mRefresh.setonRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @OverrIDe public voID onRefresh() { new Thread(mRunnable).start();//runnable调用了initData()方法; } });此时就可以对刷新 *** 作做出响应了,与平时使用RefreshLayout的 *** 作一样
上拉刷新的实现思路(主要在适配器中,activity中只需要一个当需要加载更多的时候更新数据源就行)
普通内容的布局
<?xml version="1.0" enCoding="utf-8"?><androID.support.v7.Widget.CardVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_margin="5dp" androID:orIEntation="vertical" tools:context=".MainActivity"> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="horizontal"> <ImageVIEw androID:scaleType="centerInsIDe" androID:ID="@+ID/item_head" androID:layout_wIDth="70dp" androID:layout_height="70dp" androID:layout_gravity="center" androID:layout_margin="5dp" androID:src="@mipmap/ic_launcher" /> <linearLayout androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_marginleft="5dp" androID:layout_weight="1" androID:orIEntation="vertical"> <TextVIEw androID:ID="@+ID/item_Title" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_gravity="center_vertical" androID:layout_weight="1" androID:gravity="center_vertical" androID:text="Title" androID:textSize="20sp" /> <TextVIEw androID:ID="@+ID/item_desc" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_gravity="center_vertical" androID:layout_weight="1" androID:gravity="center_vertical" androID:text="Desc" androID:textSize="16sp" /> <TextVIEw androID:ID="@+ID/item_time" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_gravity="center_vertical|right" androID:layout_marginRight="14dp" androID:layout_weight="1" androID:gravity="center_vertical|right" androID:text="Time" androID:textSize="20sp" /> </linearLayout> </linearLayout></androID.support.v7.Widget.CardVIEw>
加载更多的内容布局(默认显示Progressbar,没有更多的图标隐藏)
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="40dp" androID:orIEntation="horizontal"> <ImageVIEw androID:ID="@+ID/load_image" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_vertical|right" androID:layout_weight="1" androID:scaleType="centerInsIDe" androID:src="@mipmap/ic_launcher" androID:visibility="gone" /> <Progressbar androID:ID="@+ID/load_progress" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_vertical|right" androID:layout_weight="1" /> <TextVIEw androID:ID="@+ID/load_tv" androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:layout_gravity="center" androID:layout_weight="1" androID:gravity="center_vertical|left" androID:text="正在加载更多...." androID:textcolor="@color/colorBlank" androID:textSize="20sp" /></linearLayout>
itemCount(因为我们要在最后显示信息,所以item的总数应该是加1,但是也是分情况的:
@OverrIDe public int getItemCount() { if (mDatas.size() > minShowLoad) { //当前item能将屏幕显示满 return mDatas.size() + 1; //则默认显示加载或者没有更多 } return mDatas.size(); //如果不能显示满,则不显示加载和没有更多 }getVIEwType(根据不同的位置显示不同的type)
@OverrIDe public int getItemVIEwType(int position) { if (position == mDatas.size()) { return VIEWTYPE_LOAD; //最后一个显示加载信息 } return VIEWTYPE_CONTENT;//否则显示正常布局 }正常内容的VIEwHolder
//内容布局 private class ContentVIEwHolder extends RecyclerVIEw.VIEwHolder { private TextVIEw mTitle; private TextVIEw mDesc; private TextVIEw mTime; private ImageVIEw mhead; private VIEw itemVIEw; public ContentVIEwHolder(VIEw itemVIEw) { super(itemVIEw); this.itemVIEw = itemVIEw; mTitle = (TextVIEw) itemVIEw.findVIEwByID(R.ID.item_Title); mDesc = (TextVIEw) itemVIEw.findVIEwByID(R.ID.item_desc); mTime = (TextVIEw) itemVIEw.findVIEwByID(R.ID.item_time); mhead = (ImageVIEw) itemVIEw.findVIEwByID(R.ID.item_head); } }加载信息的VIEwHolder
//加载更多的布局 (用于显示正在加载和没有更多 private class LoadMoreVIEwHolder extends RecyclerVIEw.VIEwHolder { private ImageVIEw mImage; private Progressbar mProgress; private TextVIEw mMsg; public LoadMoreVIEwHolder(VIEw itemVIEw) { super(itemVIEw); mImage = (ImageVIEw) itemVIEw.findVIEwByID(R.ID.load_image); mProgress = (Progressbar) itemVIEw.findVIEwByID(R.ID.load_progress); mMsg = (TextVIEw) itemVIEw.findVIEwByID(R.ID.load_tv); } }onCreateVIEwHolder中初始化不同的VIEwHolder
@OverrIDe public RecyclerVIEw.VIEwHolder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) { VIEw itemVIEw = null; switch (vIEwType) { case 1: itemVIEw = LayoutInflater.from(mContext).inflate(R.layout.load_layout,parent,false); return new LoadMoreVIEwHolder(itemVIEw); case 2: itemVIEw = LayoutInflater.from(mContext).inflate(R.layout.item_test,false); return new ContentVIEwHolder(itemVIEw); } return null; }定义一个回调,用于当显示加载的时候通知activity更新数据
public interface onLoadMoreListener { voID loadMore(); } //全局变量private onLoadMoreListener onLoadMoreListener;onBindVIEwHolder(对不同的情况进行数据显示)
@OverrIDe public voID onBindVIEwHolder(RecyclerVIEw.VIEwHolder holder,int position) { if (holder instanceof ContentVIEwHolder) { TestModel model = mDatas.get(position); ((ContentVIEwHolder) holder).mTitle.setText(model.getmTitle()); ((ContentVIEwHolder) holder).mDesc.setText(model.getmDesc()); ((ContentVIEwHolder) holder).mTime.setText(model.getmTime()); } else if (holder instanceof LoadMoreVIEwHolder) { if (mDatas.size() < itemsCount) { //没有更多 ((LoadMoreVIEwHolder) holder).mMsg.setText("没有更多了~~~"); ((LoadMoreVIEwHolder) holder).mProgress.setVisibility(VIEw.GONE); ((LoadMoreVIEwHolder) holder).mImage.setVisibility(VIEw.VISIBLE); } else { onLoadMoreListener.loadMore(); ((LoadMoreVIEwHolder) holder).mMsg.setText("正在加载更多...."); ((LoadMoreVIEwHolder) holder).mProgress.setVisibility(VIEw.VISIBLE); ((LoadMoreVIEwHolder) holder).mImage.setVisibility(VIEw.GONE); } } }加载更多的回调在Activity中的使用
mAdapter.setonLoadMoreListener(new OneAdapter.onLoadMoreListener() { @OverrIDe public voID loadMore() { //增加数据到数据源中 //调用adapter的addData方法 //更新适配器显示 }}至此下拉刷新上拉加载就完成了,Demo地址:SwipeToRefreshTest。
以上就是AndroID自定义下拉刷新上拉加载的全部内容,希望能给大家一个参考,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android自定义下拉刷新上拉加载全部内容,希望文章能够帮你解决Android自定义下拉刷新上拉加载所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)