package com.example.app8;
import android.content.Context;
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 java.util.ArrayList;
/**
* Created by 郝悦 on 2017/9/6. */ public class MyRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {private ArrayList<News> list;
private Context context;public MyRecyclerAdapter(ArrayList list, Context context) {
this.list = list; this.context = context; }public int getItemViewType(int position) { return list.get(position).getType(); }
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { RecyclerView.ViewHolder viewHolder = null; if(viewType==0){ View view = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false); viewHolder = new ViewHolder1(view); } if(viewType==1){ View view = LayoutInflater.from(context).inflate(R.layout.item_layout2, parent, false); viewHolder = new ViewHolder2(view); }
return viewHolder;
}public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { int type = list.get(position).getType(); if(type==0){ Glide.with(context).load(list.get(position).getPic().get(0).getThumbnail_pic()).into(((ViewHolder1)holder).img1); Glide.with(context).load(list.get(position).getPic().get(1).getThumbnail_pic()).into(((ViewHolder1)holder).img2); Glide.with(context).load(list.get(position).getPic().get(2).getThumbnail_pic()).into(((ViewHolder1)holder).img3); ((ViewHolder1)holder).text1Tv.setText(list.get(position).getAuthor_name()); } if(type==1){ Glide.with(context).load(list.get(position).getPic().get(0).getThumbnail_pic()).into(((ViewHolder2)holder).img); ((ViewHolder2)holder).text2Tv.setText(list.get(position).getAuthor_name()); } } public int getItemCount() { return list.size(); }
public class ViewHolder1 extends RecyclerView.ViewHolder {
private ImageView img1;
private ImageView img2; private ImageView img3; private TextView text1Tv;public ViewHolder1(View itemView) {
super(itemView);img1 = (ImageView) itemView.findViewById(R.id.img1);
img2 = (ImageView) itemView.findViewById(R.id.img2); img3 = (ImageView) itemView.findViewById(R.id.img3); text1Tv = (TextView) itemView.findViewById(R.id.tv_text1);}
} public class ViewHolder2 extends RecyclerView.ViewHolder {private ImageView img;
private TextView text2Tv;public ViewHolder2(View itemView) {
super(itemView);img = (ImageView) itemView.findViewById(R.id.img);
text2Tv = (TextView) itemView.findViewById(R.id.tv_text2);}
} }