当前位置: 代码迷 >> 综合 >> RecyclerView 学习之不对recyclerView 进行封转,只对adapter封装,正常加载、下拉刷新、加载更多、多type
  详细解决方案

RecyclerView 学习之不对recyclerView 进行封转,只对adapter封装,正常加载、下拉刷新、加载更多、多type

热度:119   发布时间:2023-10-15 09:18:21.0

封装的BaseAdapter:

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;import java.util.ArrayList;
import java.util.List;import tyj.com.yedashenlib.log.CyLogger;
import tyj.com.yedashenlib.toast.ToastUtil;
import tyj.com.yedashenlib.widget.recylerview.holder.SuperViewHolder;
import tyj.com.yedashenlib.widget.recylerview.multitype.MultiTypeEntity;/*** @author ChenYe*         created by on 2017/11/17 0017. 09:52*         <p>*         这个是单列的数据baseAdapter:如果你用这个自定义的recyclerView的话,你的使用场景是跟listview*         差不多的效果,就是单列的话,就可以写一个adapter来继承这个BaseAdapter,在你写的adapter只*         需要对控件设置数据就可以了。这个BaseAdapter的使用范例的adapter是DataAdapter。**/public abstract class BaseDataAdapter<T> extends RecyclerView.Adapter<SuperViewHolder> {private static final String TAG = "BaseDataAdapter";protected List<T> mDataList = new ArrayList<>();@Overridepublic int getItemCount() {return mDataList == null ? 0 : mDataList.size();}@Overridepublic int getItemViewType(int position) {if (null != mDataList && !mDataList.isEmpty()) {T t = mDataList.get(position);if (t instanceof MultiTypeEntity) {return ((MultiTypeEntity) t).getItemType();}}return super.getItemViewType(position);}@Overridepublic SuperViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {if (setLayout(viewType) instanceof Integer) {View view = View.inflate(parent.getContext(), ((Integer) setLayout(viewType)), null);return new SuperViewHolder(view);} else if (setLayout(viewType) instanceof View) {return new SuperViewHolder(((View) setLayout(viewType)));}return null;}@Overridepublic void onBindViewHolder(SuperViewHolder holder, int position) {if (null == mDataList || mDataList.isEmpty() || position > mDataList.size() - 1) {return;}onBindItemHolder(holder, position, mDataList.get(position));}//这个是用来解决局部刷新数据的@Overridepublic void onBindViewHolder(SuperViewHolder holder, int position, List<Object> payloads) {if (payloads.isEmpty()) {onBindViewHolder(holder, position);} else {//主要是这里onBindItemHolder(holder, position, payloads);}}/*** 这个是继承这个BaseAdapter需要传进来的item布局,可以传资源id,可以传view** @param type 多类型,如果你是单一的类型,这里就传0就好了* @return*/protected abstract Object setLayout(int type);/*** 这个是让继承这个BaseAdapter去实现的,然后在这个方法里面去把数据设置到item的* 控件上去的** @param holder   holder* @param position position* @param itemData item数据*/protected abstract void onBindItemHolder(SuperViewHolder holder, int position, T itemData);public void onBindItemHolder(SuperViewHolder holder, int position, List<Object> payloads) {}/*** 向外部提供的传递数据到adapter里面刷新数据用的** @param data 传一个集合进来,那么代表是想刷新整个adapter的数据*/public void setDataList(List<T> data) {mDataList.clear();mDataList.addAll(data);notifyDataSetChanged();}/*** 向外部提供插入一个集合数据到adapter使用的** @param data*/public void addDataList(List<T> data) {mDataList.addAll(data);notifyDataSetChanged();}/*** 在最后面添加一条数据,然后局部刷新一下** @param data*/public void insertLast(T data) {mDataList.add(data);notifyItemInserted(getItemCount());}/*** 返回点击的实体的数据,这个数据是从adapter里面的集合拿到这个数据,position也是adapter里面的,* 这样拿出来的数据才是最安全的。** @param position* @return*/public T getItemData(int position) {if (position < mDataList.size()) {return mDataList.get(position);}return null;}/*** 删除指定的那一条数据** @param position*/public void remove(int position) {this.mDataList.remove(position);notifyItemRemoved(position);if (position != getDataList().size()) {notifyItemRangeChanged(position, this.mDataList.size() - position);}}/*** 删除指定区间的*/public void removeBegin(int beginPosition, int endPosition) {try {if (mDataList.size() > beginPosition && mDataList.size() > endPosition) {for (int i = beginPosition; i < endPosition; i++) {mDataList.remove(beginPosition);}}notifyItemMoved(beginPosition, endPosition);notifyItemRangeChanged(beginPosition, this.mDataList.size() - beginPosition);} catch (Exception e) {ToastUtil.newInstance().showToast("出错了!");CyLogger.e(TAG, e.getMessage());}}/****/public List<T> getDataList() {return mDataList;}}

 

MultiTypeEntity

public interface MultiTypeEntity {/*** 返回数据的type** @return*/int getItemType();
}

我这里的下拉刷新和上拉加载更多是用的SmartRefreshLayout来实现的:

implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-21'

XML 的布局:

<?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="match_parent"android:orientation="vertical"><com.scwang.smartrefresh.layout.SmartRefreshLayoutandroid:id="@+id/smart_layout"android:layout_width="match_parent"android:layout_height="match_parent"><com.scwang.smartrefresh.layout.header.ClassicsHeaderandroid:layout_width="match_parent"android:layout_height="wrap_content" /><android.support.v7.widget.RecyclerViewandroid:id="@+id/rcv"android:layout_width="match_parent"android:layout_height="match_parent" /></com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>

activity的布局:

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;import java.util.ArrayList;
import java.util.List;import tyj.com.testlib.adapter.MultiTypeAdapter;
import tyj.com.testlib.entity.Type1Entity;
import tyj.com.testlib.entity.Type2Entity;
import tyj.com.yedashenlib.widget.recylerview.multitype.MultiTypeEntity;/*** @author ChenYe created by on 2019/3/20 0020. 09:48**/public class MultiTypeRcvActivity extends Activity {@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_multi_type_rcv);final SmartRefreshLayout smartRefreshLayout = (SmartRefreshLayout) findViewById(R.id.smart_layout);final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rcv);recyclerView.setLayoutManager(new LinearLayoutManager(this));final MultiTypeAdapter adapter = new MultiTypeAdapter();recyclerView.setAdapter(adapter);smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {@Overridepublic void onRefresh(@NonNull RefreshLayout refreshLayout) {adapter.setDataList(create());smartRefreshLayout.finishRefresh();}});smartRefreshLayout.autoRefresh(200, 500, 1, false);}private List<MultiTypeEntity> create() {ArrayList<MultiTypeEntity> list = new ArrayList<>();for (int i = 0; i < 20; i++) {if (i % 3 == 0) {list.add(new Type2Entity("测试type2:" + i, "测试"));} else {list.add(new Type1Entity("测试type1:" + i));}}return list;}
}

MultiType1Entity:

import tyj.com.yedashenlib.widget.recylerview.multitype.MultiTypeEntity;/*** @author ChenYe created by on 2019/3/20 0020. 13:55**/public class Type1Entity implements MultiTypeEntity{private String desc;public Type1Entity(String desc) {this.desc = desc;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc = desc;}@Overridepublic int getItemType() {return 1;}
}

MultityType2Entity:

import tyj.com.yedashenlib.widget.recylerview.multitype.MultiTypeEntity;/*** @author ChenYe created by on 2019/3/20 0020. 13:55**/public class Type2Entity implements MultiTypeEntity{private String desc;private String url;public Type2Entity(String desc, String url) {this.desc = desc;this.url = url;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc = desc;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}@Overridepublic int getItemType() {return 2;}
}

RecyclerView 学习之不对recyclerView 进行封转,只对adapter封装,正常加载、下拉刷新、加载更多、多type

  相关解决方案