package comp.example.practise9.adapter;
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.List;
import comp.example.practise9.Lei.ShiTi; import comp.example.practise9.R;
/** * Created by 潘明杰 on 2017/11/13. */ public class RecyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<ShiTi.ResultBean.DataBean> data;
public RecyAdapter(Context context, List<ShiTi.ResultBean.DataBean> data) {
this.context = context;
this.data = data;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder holder = null;
if (viewType == 0) {
View inflate = LayoutInflater.from(context).inflate(R.layout.one\_item, null, false);
holder = new ViewHolder1(inflate);
}
if (viewType == 1) {
View inflate1 = LayoutInflater.from(context).inflate(R.layout.two\_item, null, false);
holder = new ViewHolder2(inflate1);
}
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ViewHolder1) {
((ViewHolder1) holder).tv1.setText(data.get(position).getAuthor\_name());
((ViewHolder1) holder).tv2.setText(data.get(position).getTitle());
((ViewHolder1) holder).tv3.setText(data.get(position).getCategory());
Glide.with(context).load(data.get(position).getThumbnail\_pic\_s()).into(((ViewHolder1) holder).image\_1);
Glide.with(context).load(data.get(position).getThumbnail\_pic\_s03()).into(((ViewHolder1) holder).image\_2);
Glide.with(context).load(data.get(position).getThumbnail\_pic\_s02()).into(((ViewHolder1) holder).image\_3);
}
if (holder instanceof ViewHolder2) {
((ViewHolder2) holder).tv\_content.setText(data.get(position).getAuthor\_name());
((ViewHolder2) holder).tv\_text.setText(data.get(position).getTitle());
((ViewHolder2) holder).tv\_title.setText(data.get(position).getDate());
Glide.with(context).load(data.get(position).getThumbnail\_pic\_s()).into(((ViewHolder2) holder).imagee);
}
}
@Override
public int getItemCount() {
return data.size();
}
@Override
public int getItemViewType(int position) {
String thumbnail\_pic\_s02 = data.get(position).getThumbnail\_pic\_s02();
if (thumbnail\_pic\_s02 != null) {
return 0;
} else {
return 1;
}
}
public class ViewHolder1 extends RecyclerView.ViewHolder {
public View rootView;
public TextView tv1;
public TextView tv2;
public TextView tv3;
public ImageView image\_1;
public ImageView image\_2;
public ImageView image\_3;
public ViewHolder1(View rootView) {
super(rootView);
this.rootView = rootView;
this.tv1 = (TextView) rootView.findViewById(R.id.tv1);
this.tv2 = (TextView) rootView.findViewById(R.id.tv2);
this.tv3 = (TextView) rootView.findViewById(R.id.tv3);
this.image\_1 = (ImageView) rootView.findViewById(R.id.image\_1);
this.image\_2 = (ImageView) rootView.findViewById(R.id.image\_2);
this.image\_3 = (ImageView) rootView.findViewById(R.id.image\_3);
rootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
itemClickListener.setOnItemClickListener(view,getLayoutPosition());
}
});
rootView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
OnLongItemClickListener.OnLongItemClickListener(view,getLayoutPosition());
return true;
}
});
}
}
public class ViewHolder2 extends RecyclerView.ViewHolder {
private ImageView imagee;
public View rootView;
public TextView tv\_text;
public TextView tv\_content;
public TextView tv\_title;
public ViewHolder2(View rootView) {
super(rootView);
this.rootView = rootView;
this.tv\_text = (TextView) rootView.findViewById(R.id.tv\_text);
this.tv\_content = (TextView) rootView.findViewById(R.id.tv\_content);
this.tv\_title = (TextView) rootView.findViewById(R.id.tv\_title);
this.imagee = (ImageView) rootView.findViewById(R.id.imagee);
rootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
itemClickListener.setOnItemClickListener(view,getLayoutPosition());
}
});
rootView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
OnLongItemClickListener.OnLongItemClickListener(view,getLayoutPosition());
return true;
}
});
}
}
//1.第一步
public interface OnItemClickListener {
void setOnItemClickListener(View v, int position);
}
public OnItemClickListener itemClickListener;
public void onClick(View v) {
if (itemClickListener != null) {
itemClickListener.setOnItemClickListener(v, (int) v.getTag());
}
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.itemClickListener = listener;
}
//\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
public interface OnLongItemClickListener {
void OnLongItemClickListener(View v, int position);
}
public OnLongItemClickListener OnLongItemClickListener;
public void onLongClick(View v) {
if (OnLongItemClickListener != null) {
OnLongItemClickListener.OnLongItemClickListener(v, (int) v.getTag());
}
}
public void OnLongItemClickListener(OnLongItemClickListener listener) {
this.OnLongItemClickListener = listener;
}
}