最新消息:文章中包含代码时,请遵守代码高亮规范!

Android RecyclerView Adapter及Holder的封装【原创】

Android Robin 30719浏览 0评论

项目中使用到了横向的ListView样子的功能,一开始使用Gallery做,由于Gallery的自带弹性功能,效果不理想,于是使用网上的一些开源的横向ListView,但是效果很卡,

最后使用了v7包中的RecyclerView,初次使用,不太熟练,对其进行了简单的封装


package cn.zmit.sujiamart.adapter.base;

import java.util.ArrayList;
import java.util.List;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import cn.zmit.sujiamart.holder.base.RecyclerViewHolderBase;

/**
* 横向RecyclerView基类adapter
* @author Robin
* time 2015-04-10 12:33:43
*
* @param <ItemDataType> 数据实体类型
*/
public abstract class HorizontalRecyclerViewBaseAdapter<ItemDataType> extends
RecyclerView.Adapter<RecyclerViewHolderBase> {

protected ArrayList<ItemDataType> mItemDataList = new ArrayList<ItemDataType>();

public HorizontalRecyclerViewBaseAdapter(){
}

/**
* 动态增加一条数据
* @param itemDataType 数据实体类对象
*/
public void append(ItemDataType itemDataType){
if (itemDataType!=null) {
mItemDataList.add(itemDataType);
notifyDataSetChanged();
}
}

/**
* 动态增加一组数据集合
* @param itemDataTypes 数据实体类集合
*/
public void append(List<ItemDataType> itemDataTypes){
if (itemDataTypes.size()>0) {
for (ItemDataType itemDataType : itemDataTypes) {
mItemDataList.add(itemDataType);
}
notifyDataSetChanged();
}
}

/**
* 替换全部数据
* @param itemDataTypes 数据实体类集合
*/
public void replace(List<ItemDataType> itemDataTypes){
mItemDataList.clear();
if (itemDataTypes.size()>0) {
mItemDataList.addAll(itemDataTypes);
notifyDataSetChanged();
}
}

/**
* 移除一条数据集合
* @param position
*/
public void remove(int position){
mItemDataList.remove(position);
notifyDataSetChanged();
}

/**
* 移除所有数据
*/
public void removeAll(){
mItemDataList.clear();
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return mItemDataList.size();
}

@Override
public void onBindViewHolder(RecyclerViewHolderBase viewHolder, int i) {
showData(viewHolder, i, mItemDataList);
}

@Override
public RecyclerViewHolderBase onCreateViewHolder(ViewGroup viewGroup, int i) {
View view=createView(viewGroup, i);
RecyclerViewHolderBase holder = createViewHolder(view);
return holder;
}

/*public static class ViewHolder extends RecyclerView.ViewHolder {

private ImageView img_product;
private TextView tv_title;

public ViewHolder(View itemView) {

super(itemView);

img_product=(ImageView) itemView.findViewById(R.id.img_product);
tv_title=(TextView) itemView.findViewById(R.id.tv_title);
}

}*/

/**
* 显示数据抽象函数
* @param viewHolder 基类ViewHolder,需要向下转型为对应的ViewHolder(example:MainRecyclerViewHolder mainRecyclerViewHolder=(MainRecyclerViewHolder) viewHolder;)
* @param i 位置
* @param mItemDataList 数据集合
*/
public abstract void showData(RecyclerViewHolderBase viewHolder, int i,List<ItemDataType> mItemDataList );
/**
* 加载item的view,直接返回加载的view即可
* @param viewGroup 如果需要Context,可以viewGroup.getContext()获取
* @param i
* @return item 的 view
*/
public abstract View  createView(ViewGroup viewGroup, int i) ;
/**
* 加载一个ViewHolder,为RecyclerViewHolderBase子类,直接返回子类的对象即可
* @param view item 的view
* @return  RecyclerViewHolderBase 基类ViewHolder
*/
public abstract RecyclerViewHolderBase  createViewHolder(View view) ;
}

上面是基类,如果需要多个不同的横向RecyclerView,可以继承上面的基类

下面是一个Example:


package cn.zmit.sujiamart.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import cn.zmit.sujiamart.R;
import cn.zmit.sujiamart.adapter.base.HorizontalRecyclerViewBaseAdapter;
import cn.zmit.sujiamart.constants.Url;
import cn.zmit.sujiamart.entity.ProductsEntity;
import cn.zmit.sujiamart.holder.MainHorizontalRecyclerViewHolder;
import cn.zmit.sujiamart.holder.base.RecyclerViewHolderBase;
import cn.zmit.sujiamart.image.ImageDisplayer;
import cn.zmit.sujiamart.ui.activity.ProductDetailsActivity;

/**
* 主页横向RecyclerView的适配器
* @author Robin
* time 2015-04-10 12:50:31
*
*/
public class MainHorizontalRecyclerViewAdapter extends
HorizontalRecyclerViewBaseAdapter<ProductsEntity> {
private Context context;
public MainHorizontalRecyclerViewAdapter(){
}

@Override
public void showData(RecyclerViewHolderBase viewHolder, final int i,
final List<ProductsEntity> mItemDataList) {
//向下转型为子类
MainHorizontalRecyclerViewHolder mainRecyclerViewHolder=(MainHorizontalRecyclerViewHolder) viewHolder;
//为控件赋值
ImageDisplayer.getInstance().dispalyImageWithRadius(Url.IMAGE_URL_ROOT+mItemDataList.get(i).getImage(), mainRecyclerViewHolder.img_product);
mainRecyclerViewHolder.tv_title.setText(mItemDataList.get(i).getName());
mainRecyclerViewHolder.tv_price.setText(mItemDataList.get(i).getPrice());
mainRecyclerViewHolder.tv_old_price.setText("¥"+mItemDataList.get(i).getPrice_special());
mainRecyclerViewHolder.mRootRelativeLayout.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent=new Intent(context, ProductDetailsActivity.class);
Bundle bundle=new Bundle();
bundle.putString("productId", mItemDataList.get(i).getProduct_id());
intent.putExtras(bundle);
context.startActivity(intent);
}
});
}

@Override
public View createView(ViewGroup viewGroup, int i) {
context=viewGroup.getContext();
//加载item的布局
View view=View.inflate(context,R.layout.item_main_horizontal_listview, null);
return view;
}

@Override
public RecyclerViewHolderBase createViewHolder(View view) {
//直接返回viewholder对象
return new MainHorizontalRecyclerViewHolder(view);
}

}

RycycleView需要一个ViewHolder,这里也对ViewHolder进行了简单封装,下面是ViewHolder的基类


package cn.zmit.sujiamart.holder.base;

import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.View;

/**
* RecyclerViewHolder基类
* @author Robin
*time 2015-04-10 11:43:49
*/
public abstract class RecyclerViewHolderBase extends ViewHolder {

public RecyclerViewHolderBase(View itemView) {

super(itemView);
}

}

 

如上Adapter需要对应一个ViewHolder,只需要继承上面的Viewholder基类,即可。


package cn.zmit.sujiamart.holder;

import android.graphics.Paint;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.zmit.sujiamart.R;
import cn.zmit.sujiamart.holder.base.RecyclerViewHolderBase;

/**
* 主页横向RecyclerView的viewholder
* @author Robin
* time 2015-04-10 12:53:02
*
*/
public class MainHorizontalRecyclerViewHolder extends RecyclerViewHolderBase {

public ImageView img_product;
public TextView tv_title;
public TextView tv_price;
public TextView tv_old_price;
public RelativeLayout mRootRelativeLayout;

/**
* 在构造函数中进行findviewbyid
* @param itemView item的view
*/
public MainHorizontalRecyclerViewHolder(View itemView) {
super(itemView);
img_product=(ImageView) itemView.findViewById(R.id.img_product);
tv_title=(TextView) itemView.findViewById(R.id.tv_title);
tv_price=(TextView) itemView.findViewById(R.id.tv_price);
tv_old_price=(TextView) itemView.findViewById(R.id.tv_old_price);
tv_old_price.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);  //设置中划线加清晰
mRootRelativeLayout=(RelativeLayout) itemView.findViewById(R.id.rl_root);
}

}

 

转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/10387.html


pay_weixin
pay_weixin
微信打赏
pay_weixin
支付宝打赏
感谢您对作者wangbin的打赏,我们会更加努力!    如果您想成为作者,请点我

您必须 登录 才能发表评论!