码迷,mamicode.com
首页 > 其他好文 > 详细

Recycler多布局(适配器)之无Type值

时间:2018-01-03 14:12:51      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:mil   adapter   lis   image   view   title   recycle   instance   layout   

RecyclerView简易适配器 --无Type值。

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {


    private List<MyData.ResultBean.DataBean> mList;
    private Context mContext;
    private final static int ITEM_ONE = 1;
    private final static int ITEM_TWO = 2;

public MyAdapter(List<MyData.ResultBean.DataBean> mList, Context mContext) {
this.mList = mList;
this.mContext = mContext;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view;
RecyclerView.ViewHolder holder;
if (viewType == ITEM_ONE) {
view = inflater.inflate(R.layout.recy_item_one, parent, false);
holder = new HolderOne(view);
} else {
view = inflater.inflate(R.layout.recy_item_two, parent, false);
holder = new HolderTwo(view);
}
view.setOnClickListener(this);
return holder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
MyData.ResultBean.DataBean dataBean = mList.get(position);
if (holder instanceof HolderOne) {
((HolderOne) holder).mTextOne.setText(dataBean.getTitle());
Picasso.with(mContext).load(dataBean.getThumbnail_pic_s()).into(((HolderOne) holder).mImageOne);
((HolderOne) holder).itemView.setTag(position);
} else {
((HolderTwo) holder).mTextTwo.setText(dataBean.getTitle());
Picasso.with(mContext).load(dataBean.getThumbnail_pic_s()).into(((HolderTwo) holder).mImageTwo);
((HolderTwo) holder).itemView.setTag(position);
}
}

@Override
public int getItemCount() {
return mList.isEmpty() ? 0 : mList.size();
}

//控制到底用的是那个模板
@Override
public int getItemViewType(int position) {
if (position % 2 == 0) {
return ITEM_ONE;
} else {
return ITEM_TWO;
}
}


class HolderOne extends RecyclerView.ViewHolder {
private TextView mTextOne;
private ImageView mImageOne;

public HolderOne(View itemView) {
super(itemView);
mTextOne = (TextView) itemView.findViewById(R.id.Recy_One_Text);
mImageOne = (ImageView) itemView.findViewById(R.id.Recy_One_Image);
}
}

class HolderTwo extends RecyclerView.ViewHolder {
private TextView mTextTwo;
private ImageView mImageTwo;

public HolderTwo(View itemView) {
super(itemView);
mTextTwo = (TextView) itemView.findViewById(R.id.Recy_Two_Text);
mImageTwo = (ImageView) itemView.findViewById(R.id.Recy_Two_Image);
}
}

public interface OnItemClick {
void setOnItem(View v, int position);
}

private OnItemClick onitem;

@Override
public void onClick(View v) {
if (onitem != null) {
onitem.setOnItem(v, (int) v.getTag());
}
}

public void setOnItemListener(OnItemClick item) {
this.onitem = item;
}
}

Recycler多布局(适配器)之无Type值

标签:mil   adapter   lis   image   view   title   recycle   instance   layout   

原文地址:https://www.cnblogs.com/ggjy/p/8183087.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!