由于公司项目频繁使用适配器,于是就使用java牛逼的泛型写了这和Recycleviwew那样的设计架构的lsitview adapter
超级好用的仿recycleview的listview适配器与viewholder+ javabean 解耦
已经实现好了复用了,另外如果要给适配器设置tag,你也可以改我这代码的,比如改我这个价一个资源id绑定什么的,
超级简单实用,比谁的都简单。绝对原创,你不要嘲笑我说这个东西过时了,我一直在用pulltoRefushListview做下拉上拉,而recyclview还是有一点的局限性的.暂时没发现牛逼人物做到了用recycleview实现了上拉下拉 左拉右拉,公司的项目横向刷新我采用的也是recycleview,但是我都没有做那个拉的回弹什么的,只是当到了最后一条就自动加载更多....好了废话不多说了,希望大家喜欢我的文章,为我点赞,特别是github的项目哈...
SuperAdapter.java
/**
* Created by luozheng on 2015/11/7.
update 2016-01-07 15:10:03
*/
public abstract class SuperAdapter<T,VH extends BaseViewHolder> {
public int mPage;
private String TAG="DefaultAdapter";
public List<T> data;
public Context context;
public List<T> getData(){
return this.data;
}
public void setData(List<T> data) {
this.data = data;
}
public SuperAdapter() {
}
public SuperAdapter(Context context) {
this.context=context;
}
public SuperAdapter(List<T> data, Context context) {
this.data = data;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
VH viewHolder = null;
int itemViewType = getItemViewType(position);
if (convertView == null) {
viewHolder = onCreateViewHolder(parent, itemViewType);//请再里面给itenview赋值。。。
viewHolder.itemview.setTag(viewHolder);
}else{
viewHolder = (VH) convertView.getTag();
}
onBindViewHolder(viewHolder,position,itemViewType);
return viewHolder.itemview;
}
/**
* 创建一个viewholder,并给view的itemview赋值
* @return
*/
public abstract VH onCreateViewHolder(ViewGroup parent, int viewType);
//viewholder
public abstract void onBindViewHolder(VH viewHolder,int position,int viewType);
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public int getCount() {
return data==null?0: data.size();
}
}
BaseViewHolder.java
mport android.view.View;
/**
* Created by luozheng on 16/1/7.
*/
public class BaseViewHolder {
public View itemview;
public BaseViewHolder(View view) {
this.itemview = view;
this.itemview.setTag(this);
// onSetTag(view);
}
// abstract void onSetTag(View view);
}
SuperAdapter
/**
* Created by luozheng on 2015/11/7.
* update 2016-01-07 15:10:03
*/
public abstract class SuperAdapter<T, VH extends RecyclerView.ViewHolder> extends BaseAdapter {
public int mPage;
private String TAG = "DefaultAdapter";
public List<T> data;
public Context context;
public List<T> getData() {
return this.data;
}
public void setData(List<T> data) {
this.data = data;
}
public SuperAdapter(Context context) {
this.context = context;
}
public SuperAdapter(List<T> data, Context context) {
this.data = data;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
VH viewHolder = null;
int itemViewType = getItemViewType(position);
if (convertView == null) {
viewHolder = onCreateViewHolder(parent, itemViewType);//请再里面给itenview赋值。。。
viewHolder.itemView.setTag(R.id.convert_tag, viewHolder);
} else {
viewHolder = (VH) convertView.getTag(R.id.convert_tag);
}
onBindViewHolder(viewHolder, position, itemViewType);
return viewHolder.itemView;
}
/**
* 创建一个viewholder,并给view的itemview赋值
*
* @return
*/
public abstract VH onCreateViewHolder(ViewGroup parent, int viewType);
//viewholder
public abstract void onBindViewHolder(VH viewHolder, int position, int viewType);
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public int getCount() {
return data == null ? 0 : data.size();
}
}
如何使用?
附上一个使用的例子,这个例子就是我做公司交给我的闹钟提醒需求viewholder,和adapater:
viewholder
/**
* Created by luozheng on 16/2/16.
*/
public class RemindViewHolder extends BaseViewHolder {
public final TextView tvTime;
public final TextView tvMode;
public final TextView tvContent;
public final CheckBox checkBox;
public final ImageView ivDelete;
public final View viewType;
public RemindViewHolder(View view) {
super(view);
viewType = view.findViewById(R.id.view_type);
tvMode = (TextView) view.findViewById(R.id.tv_content1);
tvTime = (TextView) view.findViewById(R.id.tv_content2);
tvContent = (TextView) view.findViewById(R.id.tv_content3);
checkBox = (CheckBox) view.findViewById(R.id.switchButton);
ivDelete = (ImageView) view.findViewById(R.id.iv_delete);
}
}
adapter
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* Created by luozheng on 16/2/16.
*/
public class RemindAdapter extends SuperAdapter<RemindModel,BaseViewHolder> {
private static final String TAG = "RemindAdapter";
private RemindEngine remindEngine;
public RemindAdapter(Context context) {
super(context);
remindEngine = new RemindEngine();
}
public RemindAdapter(List<RemindModel> data, Context context) {
super(data, context);
remindEngine = new RemindEngine();
}
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case TYPE_HEAD_ADD:
return new BaseViewHolder(LayoutInflater.from(context).inflate(R.layout.view_remind_item_head,parent,false));
case TYPE_DATA:
return new RemindViewHolder(LayoutInflater.from(context).inflate(R.layout.view_remind_item,parent,false));
default:
return null;
}
}
@Override
public int getCount() {
return super.getCount()+1;//包含头部一个
}
@Override
public int getItemViewType(int position) {//返回的值就是数组下标 - -不能超过getViewTypeCount大小
if(position<=0){
return VIEWTYPE[0];//0 -1 2
}else{
return VIEWTYPE[1];//1
}
}
@Override
public int getViewTypeCount() {
return 2;
}
int[] VIEWTYPE;
final int TYPE_HEAD_ADD = 0;
final int TYPE_DATA = 1;
{
VIEWTYPE = new int[]{TYPE_HEAD_ADD, TYPE_DATA};
}
@Override
public void onBindViewHolder(BaseViewHolder baseviewHolder, final int position, int viewType) {
switch (viewType){
case TYPE_HEAD_ADD:
baseviewHolder.itemview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onAddClickListener!=null){
onAddClickListener.onClick();
}
}
});
break;
case TYPE_DATA:
RemindViewHolder viewHolder= (RemindViewHolder) baseviewHolder;
final int temp_position=position-1;
final RemindModel remindModel = getData().get(temp_position);
viewHolder.tvContent.setText(""+remindModel.getClass_title());
viewHolder.tvTime.setText(CalendarUtils.intAppendZero(remindModel.getHour())+":"+CalendarUtils.intAppendZero(remindModel.getMinute()));
RemindModel.MODE currentMode = remindModel.getCurrentMode();
RemindModel.RemindTYPE remindType = remindModel.getRemindType();
Log.i(TAG,"remindModel:"+remindModel);
switch (remindType){
case GETUP:
viewHolder.viewType.setBackgroundResource(R.drawable.shape_oval_red);
break;
case GOTBED:
viewHolder.tvTime.setText(""+remindModel.getMinute()+"分钟后");
viewHolder.viewType.setBackgroundResource(R.drawable.shape_oval_blue);
break;
case REMIND:
viewHolder.viewType.setBackgroundResource(R.drawable.shape_oval_green);
break;
}
switch (currentMode){
case ONE:
viewHolder.tvMode.setText("一次");
break;
case EVERYDAY:
viewHolder.tvMode.setText("每天");
break;
case WORKDAY:
viewHolder.tvMode.setText("工作日");
break;
case CUSTOM:
ArrayList<Integer> customDays = remindModel.getCustomDays();
StringBuilder sb=new StringBuilder();
for (Integer customDay : customDays) {
sb.append("星期"+ StringUtils.intWeekDayToString(customDay)+" ");
}
viewHolder.tvMode.setText(""+sb.toString());
break;
}
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked==remindModel.isEnabled()){
return;//证明当前只是查询并赋值
}
RemindDao remindDao=new RemindDao(context);
if(isChecked){
remindEngine.registerCurrentAlarm(context,remindModel);
}else{
remindEngine.unregisterCurrentAlarm(context,remindModel);
}
remindModel.setEnabled(isChecked);
remindDao.update(remindModel,"enabled");
notifyDataSetChanged();
}
});
viewHolder.checkBox.setChecked(remindModel.isEnabled());
viewHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alertDialog=new AlertDialog.Builder(context);
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.setTitle(""+remindModel.getClass_title());
alertDialog.setMessage("真的要删除吗?");
alertDialog.setPositiveButton("删除", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(remindModel.getRemindType()!= RemindModel.RemindTYPE.REMIND){
Utils.showToast(context,"叫我起床提醒或伴我睡觉定时任务的id一经初始化就不能再删除哦!将导致id无法重建哦", Toast.LENGTH_LONG);
return;
}
if(remindModel.isEnabled()){
remindEngine.unregisterAlerm(context,remindModel);
}
RemindDao remindDao=new RemindDao(context);
remindDao.delete(remindModel.getId());
remindDao.close();
getData().remove(temp_position);
notifyDataSetChanged();
}
});
alertDialog.setNegativeButton("取消",null);
alertDialog.show();
}
});
break;
}
}
public interface OnAddClickListener {
public void onClick();
}
public OnAddClickListener getOnAddClickListener() {
return onAddClickListener;
}
public void setOnAddClickListener(OnAddClickListener onAddClickListener) {
this.onAddClickListener = onAddClickListener;
}
OnAddClickListener onAddClickListener;
}
上面的适配器是listview需求中比较常见的多种item的实现。没有基础的可以看下面这个简单一点的- -
/**
* Created by luozheng on 2016-01-06 14:42:12.
*/
public class StoryListAdapter extends SuperAdapter<BaseStoryModel,StoryListViewHolder> {
private HomeModel playListHomeModel;
public StoryListAdapter(List<BaseStoryModel>baseStoryModels, Context context) {
super(baseStoryModels,context);
playListHomeModel = GlobalInfo.getPlayListHomeModel();
if(playListHomeModel==null){
playListHomeModel=new HomeModel();
}
}
@Override
public StoryListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new StoryListViewHolder(LayoutInflater.from(context).inflate(R.layout.view_item_play_list,parent,false));
}
@Override
public void onBindViewHolder(StoryListViewHolder viewHolder, int position, int viewType) {
final BaseStoryModel storyModel = getData().get(position);
}
总之我写的都是 javaben viewhoder,adapter的结合了..不需要bean的可以再进行修改咯,不过几乎没有不用bean的吧?上面的baseStoryModel就是一个bean了。
new adapter的时候传递 的是list集合 里面放的是上面申明的javabean。。。
更多内容关注:http://github.com/qssq
标签:viewholder,return,int,适配器,recycleview,viewHolder,context,data,public From: https://blog.51cto.com/u_15458814/5885144