一,简述
RecycleView是谷歌support包里的一个新控件,通过设置LayoutManager可以简单的制作出列表,横向列表,网格布局等布局。
二,封装RecycleView
正常使用RecycleView的话,我们至少需要重写onCreateViewHolder,onBindViewHolder,getItemCount,还要写一个ViewHolder类,需要重写的函数过多。
那么我们可不可以简化这种方式呢,答案当然是可以的。接下来附上代码。
/**
* Created by henryzheng on 2017/3/15.
*/
public abstract class CCRecyclerViewAdapt<T> extends RecyclerView.Adapter<CCViewHolder> {
private final Context mContext;
private final LayoutInflater mLayoutInflater;
private CCViewHolder holder; //自定义ViewHolder
private List<T> datas;// 数据源
private CCRecycleViewListener.OnItemClickListener onItemClickListener;
private CCRecycleViewListener.OnItemLongClickListener onItemLongClickListener;
public CCRecyclerViewAdapt(Context context, List<T> datas){
this(context);
}
public CCRecyclerViewAdapt(Context context){
mContext=context;
datas=new ArrayList<>();
mLayoutInflater= LayoutInflater.from(context);
}
@Override
public CCViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=mLayoutInflater.inflate(getItemLayoutId(),null);
holder=new CCViewHolder(mContext,view, onItemClickListener, onItemLongClickListener);
return holder;
}
@Override
public void onBindViewHolder(CCViewHolder holder, int position) {
convert(holder,datas.get(position));
}
/**
* 滑动时,重复执行函数
* @param holder
* @param bean
*/
protected abstract void convert(CCViewHolder holder, T bean);
/**
* 让子类返回要显示的layoutId
* @return
*/
public abstract int getItemLayoutId() ;
public void setOnItemClickListener(CCRecycleViewListener.OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public void setOnItemLongClickListener(CCRecycleViewListener.OnItemLongClickListener onItemLongClickListener) {
this.onItemLongClickListener = onItemLongClickListener;
}
@Override
public int getItemCount() {
if (datas.size() > 0) {
return datas.size() ;
}
return 0;
}
/**
* 加载更多的数据
* @param data
*/
public void loadMoreData(List<T> data) {
int start = this.datas.size() ;
for (int i = 0; i < data.size(); i++) {
this.datas.add(data.get(i));
notifyItemInserted(start);
}
CCLog.i("loadMoreData:" + this.datas.size());
}
/**
* 刷新数据
* @param data
*/
public void refreshData(List<T> data) {
this.datas.clear();
notifyDataSetChanged();
for (int i = (data).size()-1; i >= 0; i--) {
this.datas.add(0, data.get(i));
notifyItemInserted(0);
}
}
}
/**
* Created by henryzheng on 2017/3/15.
*/
public class CCViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener {
View convertView;
Context context;
private CCRecycleViewListener.OnItemClickListener onItemClickListener;
private CCRecycleViewListener.OnItemLongClickListener onItemLongClickListener;
/**
* View滑动时执行的函数
* @param context
* @param itemView
* @param onItemClickListener
* @param onItemLongClickListner
*/
public CCViewHolder(Context context, View itemView, CCRecycleViewListener.OnItemClickListener onItemClickListener, CCRecycleViewListener.OnItemLongClickListener onItemLongClickListner) {
super(itemView);
this.convertView = itemView;
this.context = context;
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
this.onItemClickListener=onItemClickListener;
this.onItemLongClickListener=onItemLongClickListner;
}
@Override
public void onClick(View v) {
onItemClickListener.onItemClickListener(v,getPosition());
}
public void setText(int id, String text) {
TextView tx = (TextView) convertView.findViewById(id);
tx.setText(text);
}
@Override
public boolean onLongClick(View v) {
onItemLongClickListener.onItemLongClickListener(v,getPosition());
return false;
}
}
/**
* Created by henryzheng on 2017/3/15.
*/
public class CCRecycleViewListener {
public interface OnItemClickListener {
void onItemClickListener(View v, int position);
}
public interface OnItemLongClickListener {
void onItemLongClickListener(View v, int position);
}
}
这样就完成了对RecycleView的简单封装,接下来,来看下怎么使用吧。
三,CCRecycleView的简单使用
1.写一个TestRecyclerAdapt 类继承于CCRecyclerViewAdapt,并重写getItemLayoutId(itemView的layoutId),和convert函数。
/**
* Created by henryzheng on 2017/3/15.
*/
public class TestRecyclerAdapt extends CCRecyclerViewAdapt{
public TestRecyclerAdapt(Context context) {
super(context);
}
public TestRecyclerAdapt(Context context, List datas) {
super(context, datas);
}
@Override
protected void convert(CCViewHolder holder, Object bean) {
Person p= (Person) bean;
holder.setText(R.id.tv_name,p.getName());
holder.setText(R.id.tv_year,p.getYear());
}
@Override
public int getItemLayoutId() {
return R.layout.main_activity_recycler_view_item_view;
}
}
**
* Created by henryzheng on 2017/3/15.
*/
public class Person {
private String name;
private String year;
public Person(String name, String year) {
this.name = name;
this.year = year;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
}
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
private TestRecyclerAdapt adapt;
private List persons;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView= (RecyclerView) findViewById(R.id.recyclerView);
persons=new ArrayList();
for (int i=0;i<200;i++){
Person p=new Person("名字"+i,i+"岁");
persons.add(p);
}
adapt=new TestRecyclerAdapt(this);
LinearLayoutManager manager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(adapt);
adapt.refreshData(persons);
adapt.setOnItemClickListener(new CCRecycleViewListener.OnItemClickListener() {
@Override
public void onItemClickListener(View v, int position) {
CCLog.e("view "+position+" click");
}
});
adapt.setOnItemLongClickListener(new CCRecycleViewListener.OnItemLongClickListener() {
@Override
public void onItemLongClickListener(View v, int position) {
CCLog.e("view "+position+" LongClick");
}
});
}
}
封装之后使用很简单,继承CCRecyclerViewAdapt,主要重写一个函数convert就可以了,而且,数据源是泛型,使用起来的可用性也较强。
这是RecycleView.Adapt的简单封装,但是实际情况下,还有下拉刷新和上拉加载,基于上面的封装,可以进行二次封装,让效果更完整。