欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

recycleview适配器

程序员文章站 2024-03-17 09:27:28
...

package com.example.unit1.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.unit1.R;
import com.example.unit1.bean.News;

import java.util.List;

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

final int item_one=1;
final int item_two=2;
List<News.DataBean> mList;
Context mContext;


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

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
     View v=null;
  RecyclerView.ViewHolder holder = null;
    switch (i){
        case item_one:
            v = LayoutInflater.from(mContext).inflate(R.layout.xlist_item_one,null);
            holder=new ViewHolderOne(v);

            break;

        case item_two:
            v = LayoutInflater.from(mContext).inflate(R.layout.xlist_item_two,null);
            holder=new ViewHolderTwo(v);
            break;
    }
    v.setOnClickListener(this);
    return holder;
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
    if( viewHolder instanceof ViewHolderOne){
        ((ViewHolderOne) viewHolder).textView1.setText(mList.get(i).getTitle());
    }else if(viewHolder instanceof ViewHolderTwo){

        //((ViewHolderTwo)viewHolder).imageView.setImageResource(R.mipmap.ic_launcher_round);
        Glide.with(mContext).load(mList.get(i).getThumbnail_pic_s()).into(((ViewHolderTwo)viewHolder).imageView);
        ((ViewHolderTwo)viewHolder).textView2.setText(mList.get(i).getTitle());

    }

    viewHolder.itemView.setTag(i);

}

@Override
public int getItemCount() {
    return mList.size();
}

@Override
public int getItemViewType(int position) {
    if(position%2==0){
        return item_one;

    }else{
        return item_two;
    }
}


class ViewHolderOne extends RecyclerView.ViewHolder{
    TextView textView1;
    public ViewHolderOne(@NonNull View itemView) {
        super(itemView);
        textView1=itemView.findViewById(R.id.tvname1);
    }
}
class ViewHolderTwo extends RecyclerView.ViewHolder{
    ImageView imageView;
    TextView textView2;
    public ViewHolderTwo(@NonNull View itemView) {
        super(itemView);
        imageView=itemView.findViewById(R.id.img);
        textView2=itemView.findViewById(R.id.tvname2);
    }
}
public interface  setOnclick{
    void setData(View view,int position);
}
private setOnclick setonclick ;
public void setOnclick(setOnclick onclick){
    this.setonclick=onclick;
}
@Override
public void onClick(View v) {
    if(setonclick!=null){
        setonclick.setData(v, (Integer) v.getTag());
    }
}

}