package com.hjhrq1991.adapter;
import java.util.List;
import com.hjhrq1991.bean.TestInfo;
import com.hjhrq1991.recyclerviewdemo.R;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class TestAdaper extends RecyclerView.Adapter<ViewHolder> {
private List<TestInfo> mList;
private Context context;
public TestAdaper(Context context, List<TestInfo> mList) {
super();
this.context = context;
this.mList = mList;
}
/**
* 内部TextHoler
*/
public class TextHoler extends RecyclerView.ViewHolder {
public TextView textView;
public TextHoler(View view) {
super(view);
}
}
/**
* iamgeHolder
*/
public class ImageHoler extends RecyclerView.ViewHolder {
public TextView textView;
public ImageView Imageview;
public ImageHoler(View view) {
super(view);
this.textView = (TextView) view.findViewById(R.id.text);
this.Imageview = (ImageView) view.findViewById(R.id.image);
}
}
/**
* 按钮的holder
*/
public class CbHoler extends RecyclerView.ViewHolder {
public TextView textView;
public ImageView Imageview;
public CheckBox checkbox;
public CbHoler(View view) {
super(view);
this.textView = (TextView) view.findViewById(R.id.text);
this.Imageview = (ImageView) view.findViewById(R.id.image);
this.checkbox = (CheckBox) view.findViewById(R.id.checkbox);
}
}
@Override
public int getItemCount() {
// TODO Auto-generated method stub
return mList.size();
}
/**
* 获取item的类型
*/
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return mList.get(position).getType();
}
/**
* 创建view
*/
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewtype) {
// TODO Auto-generated method stub
View v = null;
ViewHolder holer = null;
switch (viewtype) {
case TestInfo.TASK_TEXT:
v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.item_text_layout, null);
holer = new TextHoler(v);
break;
case TestInfo.TASK_DRAWABLE:
v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.item_image_layout, null);
holer = new ImageHoler(v);
break;
case TestInfo.TASK_CHEXKBOX:
v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.item_checkbox_layout, null);
holer = new CbHoler(v);
break;
}
return holer;
}
/**
* 在这里进行数据的绑定,参数1是onCreateViewHolder创建的viewholder,参数2是你item的位置
*/
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// TODO Auto-generated method stub
switch (getItemViewType(position)) {
case TestInfo.TASK_TEXT:
TextHoler textholer = (TextHoler) holder;
textholer.textView.setText(mList.get(position).getContent());
break;
case TestInfo.TASK_DRAWABLE:
ImageHoler imageHolder = (ImageHoler) holder;
imageHolder.textView.setText(mList.get(position).getContent());
imageHolder.Imageview.setImageResource(mList.get(position)
.getDrawableurl());
break;
case TestInfo.TASK_CHEXKBOX:
CbHoler cbHoler = (CbHoler) holder;
cbHoler.textView.setText(mList.get(position).getContent());
cbHoler.Imageview.setImageResource(mList.get(position)
.getDrawableurl());
cbHoler.checkbox.setText(mList.get(position).getCbcontent());
break;
}
}
}