作者:庄超人 链接:https://www.jianshu.com/p/42254630bdce
因为发展趋势变成了使用RecyclerView来替代ListVIew,但网上大部分博客写的内容相对简单,很多用法没讲清楚,自己在使用过程中走了不少弯路,特此记录一下自己的心得。首先是继承父类
extends RecyclerView.Adapter<RecyclerView.ViewHolder>
需要实现以下几个函数
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {}
@Override
public int getItemCount() {
return 0;
}
onCreateViewHolder()负责为Item创建视图,onBindViewHolder()负责将数据绑定到Item的视图上。那么接下来就需要有ViewHolder了。
public class MenuItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView newsType;
public MenuItemViewHolder(View v) {
super(v);
newsType = (TextView)v.findViewById(R.id.tv_item);
}
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(v, this.getPosition());
}
}
}
以上就是基本的用法,通过这些内容就可以使用一个建议的RecyclerView。
但如果这个RecyclerView里需要设置多种Item的view视图,这就需要设置好几个变量并用到这个函数
private static final int TYPE_TOPIC = 0;
private static final int TYPE_TITLE = 1;
Override
public int getItemViewType(int position) {}
通过getItemViewType()为每个item设置好它的类型后在即可在onCreateViewHolder()根据Type创建相应的视图。
如果需要像ListView一样使用headerView,则需要预先自定义一个HeaderView,示例如下
private View mHeaderView;
public void setHeaderView(View headerView) {
mHeaderView = headerView;
notifyItemInserted(0);
}
并在ViewHolder对其进行设置。
public MainTopicViewHolder (View itemView) {
super(itemView);
if (itemView == mHeaderView) return;
tv_topic = (TextView) itemView.findViewById(R.id.tv_topic);
}
在设置监听时,需要在Adapter里定义好接口和set的函数
private OnItemClickListener mOnItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.mOnItemClickListener = onItemClickListener;
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
及在onBindViewHolder()绑定的时候将监听器也绑定上
if (mOnItemClickListener != null) {
((MainTopicViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
Override
public void onClick(View v) {
int pos = holder.getLayoutPosition();
mOnItemClickListener.onItemClick(holder.itemView, pos);
}
});
}
以后在此基础上另有心得时再补充,附上一个完整示例
public class MainNewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TYPE_HEADER = 0;
private static final int TYPE_TOPIC = 1;
private static final int TYPE_TITLE = 2;
private Context mContext;
private OnItemClickListener mOnItemClickListener;
private List list;
private ImageLoader imageLoader;
private DisplayImageOptions displayImageOptions;
private View mHeaderView;
public MainNewsAdapter(Context context) {
mContext = context;
list = new ArrayList<>();
imageLoader = ImageLoader.getInstance();
displayImageOptions = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).build();
}
public void addList(List items) {
this.list.addAll(items);notifyDataSetChanged();
}
public void setHeaderView(View headerView) {
mHeaderView = headerView;
notifyItemInserted(0);
}
public View getHeaderView() {
return mHeaderView;
}
public StoriesEntity getItem(int position) {
return list.get(position-1);
}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return TYPE_HEADER;
} else {
StoriesEntity entity = list.get(position-1);
if (entity.getType() == Constant.TOPIC) {
return TYPE_TOPIC;
} else {
return TYPE_TITLE;
}
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_TOPIC) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_news_topic, parent, false);
MainTopicViewHolder mainTopicViewHolder = new MainTopicViewHolder(v);
return mainTopicViewHolder;
} else if (viewType == TYPE_TITLE) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_news_item, parent, false);
MainNewsViewHolder mainNewsViewHolder = new MainNewsViewHolder(v);
return mainNewsViewHolder;
} else {
MainTopicViewHolder mainTopicViewHolder = new MainTopicViewHolder(mHeaderView);
return mainTopicViewHolder;
}
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
if (position == 0) {
return;
}
if (holder instanceof MainNewsViewHolder) {
StoriesEntity entity = list.get(position — 1);
((MainNewsViewHolder) holder).tv_title.setText(entity.getTitle());
imageLoader.displayImage(entity.getImages().get(0),((MainNewsViewHolder) holder).iv_title, displayImageOptions);
if (mOnItemClickListener != null) {
((MainNewsViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = holder.getLayoutPosition();
mOnItemClickListener.onItemClick(holder.itemView, pos);
}
});
}
} else {
StoriesEntity entity = list.get(position-1);
((MainTopicViewHolder) holder).tv_topic.setVisibility(View.VISIBLE);((MainTopicViewHolder) holder).tv_topic.setText(entity.getTitle());
if (mOnItemClickListener != null) {
((MainTopicViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v) {
int pos = holder.getLayoutPosition();
mOnItemClickListener.onItemClick(holder.itemView, pos);
}
});
}
}
}
@Override
public int getItemCount() {
return mHeaderView == null ? list.size() : list.size() + 1;
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.mOnItemClickListener = onItemClickListener;
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
public class MainTopicViewHolder extends RecyclerView.ViewHolder {
TextView tv_topic;
public MainTopicViewHolder(View itemView) {
super(itemView);
if (itemView == mHeaderView) return;
tv_topic = (TextView) itemView.findViewById(R.id.tv_topic);
}
}
public class MainNewsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView tv_title;
ImageView iv_title;
public MainNewsViewHolder(View itemView) {
super(itemView);
tv_title = (TextView) itemView.findViewById(R.id.tv_title);
iv_title = (ImageView) itemView.findViewById(R.id.iv_title);}
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(v, this.getPosition());
}
}
}
}