之前做一个应用商店类的项目,第一次使用RecyclerView,下面说说怎么使用,这里只说adapter使用
1,首先是Adapter 的继承,不再像Listview继承BaseAdapter,而是继承 RecycleView.Adapter<>,注意后面的泛型是你的ViewHolder类型.
public class RecommendAdapter extends RecyclerView.Adapter<MyViewHolder> implements Observer {}
2,创建ViewHolder:
class MyViewHolder extends RecyclerView.ViewHolder{
TextView textView;
ImageView imageView;
Button button;
LinearLayout downloadLayout;
LinearLayout describeLayout;
TextView sizeView;
TextView statusView;
TextView describeView;
TextView size;
TextView titleView;
TextView rateView;
ProgressBar progressBar;
LinearLayout reconmmendItem;
public MyViewHolder(View itemView) {
super(itemView);
titleView = (TextView) itemView.findViewById(R.id.app_type_count);
textView = (TextView) itemView.findViewById(R.id.app_name_tv);
imageView = (ImageView) itemView.findViewById(R.id.app_icon);
button = (Button) itemView.findViewById(R.id.app_operation_bt);
downloadLayout = (LinearLayout) itemView.findViewById(R.id.app_down_ll);
describeLayout = (LinearLayout) itemView.findViewById(R.id.app_describe_ll);
sizeView = (TextView) itemView.findViewById(R.id.app_size_tv);
statusView = (TextView) itemView.findViewById(R.id.app_status_tv);
describeView = (TextView) itemView.findViewById(R.id.app_describe_tv);
size = (TextView) itemView.findViewById(R.id.app_down_size_tv);
rateView = (TextView) itemView.findViewById(R.id.app_down_rate_tv);
progressBar = (ProgressBar) itemView.findViewById(R.id.app_down_pb);
reconmmendItem = (LinearLayout) itemView.findViewById(R.id.recommend_item);
}
3,实现方法:
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
if(viewType == 0) {
view = layoutInflater.inflate(R.layout.manage_item_text,parent,false);
}else {
view = layoutInflater.inflate(R.layout.recommend_item,parent,false);
}
MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
4: 绑定数据:
public void onBindViewHolder(final MyViewHolder holder, int position){}
这样就搞定了,是不是比ListView的简单一些,最后请记得设置布局管理器,可以切换是Listview还是gridview.....
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
mRecyclerView.setLayoutManager(new
GridLayoutManager(this,4));