亲宝软件园·资讯

展开

Recyclerview添加头布局和尾布局 Recyclerview添加头布局和尾布局、item点击事件详解

段合江 人气:0
想了解Recyclerview添加头布局和尾布局、item点击事件详解的相关内容吗,段合江在本文为您仔细讲解Recyclerview添加头布局和尾布局的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Recyclerview,布局,点击事件,下面大家一起来学习吧。

简介:

本篇博客主要包括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.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
 private Context context;
 private List<Person> 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<Person> 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<headViewCount;
 }
 public boolean isFootView(int position){
  return footViewCount!=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 List<Person> datas;
 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<imgs.length;i++){
   Person p=new Person();
   p.setName("hahfa"+i);
   p.setDetail("今天是星期一,还有5天才能休息"+i);
   p.setImgId(imgs[i]);
   datas.add(p);
  }
 }

 @Override
 public void onItemClick(View view, int position) {
  Intent intent=new Intent(MainActivity.this,ImageActivity.class);
  intent.putExtra("imgId",datas.get(position).getImgId());
  startActivity(intent);
 }
}

加载全部内容

相关教程
猜你喜欢
用户评论