Recyclerview如何添加头布局和尾布局、item点击事件-创新互联
这篇文章主要介绍了Recyclerview如何添加头布局和尾布局、item点击事件,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、网页空间、营销软件、网站建设、开阳网站维护、网站推广。思路:
主要重写Recyclerview.Adapter中的一些方法
1.public int getItemCount() item熟练 +2(头布局和尾布局)
2.public int getItemViewType(int position) 判断position 设置itemType
3.创建不同的ViewHolder,分别用来加载头布局,正常布局,尾布局
4.public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 根据viewType的不同返回不同的viewholder
5.public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) 根据holder的不同绑定不同的数据
案例:
主布局中只有一个Recyclerview,里面的item除了基本的两种类型,还有头布局和尾布局,点击基本类型跳转到另外一个Activity,显示图片
下面主要贴出Adapter和MainActivity中的代码,其他的布局文件,类比较简单就不贴出了
1.Adapter
public class MyAdapter extends RecyclerView.Adapterimplements View.OnClickListener { private Context context; private List datas; private static final int ITEM_ONE=1; private static final int ITEM_TWO=2; private static final int ITEM_HEAD=3; private static final int ITEM_FOOT=4; private int headViewCount=1; private int footViewCount=1; private OnItemClickListener onItemClickListener; public MyAdapter(Context context, List datas) { this.context = context; this.datas = datas; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if(viewType==ITEM_HEAD){ View view= LayoutInflater.from(context).inflate(R.layout.item_head,parent,false); return new HeaderViewHolder(view); }else if(viewType==ITEM_FOOT){ View view=LayoutInflater.from(context).inflate(R.layout.item_foot,parent,false); return new FootViewHolder(view); } else if(viewType==ITEM_ONE){ View view= LayoutInflater.from(context).inflate(R.layout.item_recyclerview,parent,false); return new ViewHolder(view); }else if(viewType==ITEM_TWO){ View view=LayoutInflater.from(context).inflate(R.layout.item_recyclerview2,parent,false); return new ViewHolder(view); } return null; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { if(holder instanceof HeaderViewHolder){ }else if(holder instanceof FootViewHolder){ } else if(holder instanceof ViewHolder){ //注意除去头布局 holder.itemView.setTag(position-1); holder.itemView.setOnClickListener(this); ((ViewHolder)holder).img.setBackgroundResource(datas.get(position-1).getImgId()); ((ViewHolder)holder).tv1.setText(datas.get(position-1).getName()); ((ViewHolder)holder).tv2.setText(datas.get(position-1).getDetail()); } } @Override public int getItemCount() { if(datas==null){ return headViewCount+footViewCount; } return datas.size()+headViewCount+footViewCount; } @Override public int getItemViewType(int position) { if(isHeadView(position)){ return ITEM_HEAD; } if(isFootView(position)){ return ITEM_FOOT; } if(position%2==0){ return ITEM_ONE; }else{ return ITEM_TWO; } } public boolean isHeadView(int position){ return headViewCount!=0&&position =(datas.size()+headViewCount); } @Override public void onClick(View view) { if(onItemClickListener!=null){ onItemClickListener.onItemClick(view, (Integer) view.getTag()); } } class ViewHolder extends RecyclerView.ViewHolder{ TextView tv1,tv2; ImageView img; public ViewHolder(View itemView) { super(itemView); tv1=itemView.findViewById(R.id.tv1); tv2=itemView.findViewById(R.id.tv2); img=itemView.findViewById(R.id.img); } } class HeaderViewHolder extends RecyclerView.ViewHolder{ public HeaderViewHolder(View itemView) { super(itemView); } } class FootViewHolder extends RecyclerView.ViewHolder{ public FootViewHolder(View itemView) { super(itemView); } } public void setOnItemClickListener(OnItemClickListener onItemClickListener){ this.onItemClickListener=onItemClickListener; } interface OnItemClickListener{ void onItemClick(View view,int position); } }
2.MainActivity
public class MainActivity extends AppCompatActivity implements MyAdapter.OnItemClickListener { private RecyclerView recyclerView; private Listdatas; private MyAdapter adapter; private int[] imgs={R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p5,R.drawable.pic1, R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,R.drawable.pic5,R.drawable.pic6}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView= (RecyclerView) findViewById(R.id.recyclerview); initData(); initRecyclerView(); } private void initRecyclerView() { recyclerView.setLayoutManager(new LinearLayoutManager(this)); adapter=new MyAdapter(this,datas); adapter.setOnItemClickListener(this); recyclerView.setAdapter(adapter); } private void initData() { datas=new ArrayList<>(); for(int i=0;i 感谢你能够认真阅读完这篇文章,希望小编分享的“Recyclerview如何添加头布局和尾布局、item点击事件”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!
新闻标题:Recyclerview如何添加头布局和尾布局、item点击事件-创新互联
网站URL:http://azwzsj.com/article/cseddi.html