RecyclerView多條目
阿新 • • 發佈:2018-12-22
1.adapter
public class RecyclerAdapter extends RecyclerView.Adapter {
private Context context;
private List<JsonBean.ResultBean.DataBean> list;
public RecyclerAdapter(Context context, List<JsonBean.ResultBean.DataBean> list) { this.context = context; this.list = list; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewtype) { View view=null; RecyclerView.ViewHolder viewHolder =null; switch (viewtype){ case 0: view = View.inflate(context,R.layout.rec1,null); viewHolder = new MyViewHolder(view); break; case 1: view = View.inflate(context,R.layout.rec2,null); viewHolder = new MyViewHolder2(view); break; case 2: view = View.inflate(context,R.layout.rec3,null); viewHolder = new MyViewHolder3(view); break; } return viewHolder; } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) { if (viewHolder instanceof MyViewHolder){ ((MyViewHolder)viewHolder).textView1.setText(list.get(i).getTitle()); ((MyViewHolder)viewHolder).textView2.setText(list.get(i).getAuthor_name()); ((MyViewHolder)viewHolder).textView3.setText(list.get(i).getDate()); }else if (viewHolder instanceof MyViewHolder2){ ((MyViewHolder2)viewHolder).textView21.setText(list.get(i).getTitle()); ((MyViewHolder2)viewHolder).textView22.setText(list.get(i).getAuthor_name()); ((MyViewHolder2)viewHolder).textView23.setText(list.get(i).getDate()); Glide.with(context).load(list.get(i).getThumbnail_pic_s()).into(((MyViewHolder2) viewHolder).imageView21); ((MyViewHolder2)viewHolder).imageView21.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onItemClickListener.onItemClick(v,i); } }); }else { ((MyViewHolder3)viewHolder).textView31.setText(list.get(i).getTitle()); Glide.with(context).load(list.get(i).getThumbnail_pic_s03()).into(((MyViewHolder3) viewHolder).imageView31); Glide.with(context).load(list.get(i).getThumbnail_pic_s02()).into(((MyViewHolder3) viewHolder).imageView32); Glide.with(context).load(list.get(i).getThumbnail_pic_s()).into(((MyViewHolder3) viewHolder).imageView33); ((MyViewHolder3)viewHolder).imageView31.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onItemClickListener.onItemClick(v,i); } }); } } public void removedata(int position){ list.remove(position); notifyItemRemoved(position); } @Override public int getItemCount() { return list.size(); } public interface OnItemClickListener{ void onItemClick(View view,int position); } private OnItemClickListener onItemClickListener; public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } @Override public int getItemViewType(int position) { if (list.get(position).getThumbnail_pic_s03()!=null){ return 2; }else if (list.get(position).getThumbnail_pic_s()!=null){ return 1; }else { return 0; } } private class MyViewHolder extends RecyclerView.ViewHolder{ private final TextView textView1,textView2,textView3; public MyViewHolder(@NonNull View itemView) { super(itemView); textView1 = itemView.findViewById(R.id.text1); textView2 = itemView.findViewById(R.id.text2); textView3 = itemView.findViewById(R.id.text3); itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { removedata(getAdapterPosition()); return false; } }); } } private class MyViewHolder2 extends RecyclerView.ViewHolder{ private final TextView textView21,textView22,textView23; private final ImageView imageView21; public MyViewHolder2(@NonNull View itemView) { super(itemView); textView21 = itemView.findViewById(R.id.text21); textView22 = itemView.findViewById(R.id.text22); textView23 = itemView.findViewById(R.id.text23); imageView21 = itemView.findViewById(R.id.image21); ViewGroup.LayoutParams params = imageView21.getLayoutParams(); //設定控制元件的寬高 params.width=(1000)/3; params.height =(int)(200+Math.random()*400); imageView21.setLayoutParams(params); itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { removedata(getAdapterPosition()); return false; } }); } } private class MyViewHolder3 extends RecyclerView.ViewHolder{ private final TextView textView31; private ImageView imageView31,imageView32,imageView33; public MyViewHolder3(@NonNull View itemView) { super(itemView); textView31 = itemView.findViewById(R.id.text31); imageView31 = itemView.findViewById(R.id.iamge31); imageView32 = itemView.findViewById(R.id.iamge32); imageView33 = itemView.findViewById(R.id.iamge33); imageView31 = itemView.findViewById(R.id.iamge31); ViewGroup.LayoutParams params = imageView31.getLayoutParams(); //設定控制元件的寬高 params.width=(1000)/3; params.height =(int)(200+Math.random()*400); imageView31.setLayoutParams(params); itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { removedata(getAdapterPosition()); return false; } }); } }
}
2.Activity
public class MainActivity extends AppCompatActivity implements OverView {
private String[] imageurl=new String[]{
};
private Toolbar toolbar; private RecyclerView recyclerView; private LjPresenter ljPresenter; private List<JsonBean.ResultBean.DataBean> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setSupportActionBar(toolbar); getSupportActionBar().setDisplayShowTitleEnabled(false); initdata(); ljPresenter = new LjPresenter(this); ljPresenter.show(); } private void initView() { toolbar = findViewById(R.id.toolbar); recyclerView = findViewById(R.id.recyclerview); } private void initdata() { toolbar.setLogo(R.mipmap.ic_launcher); toolbar.setTitle("機器人"); Banner banner =findViewById(R.id.banner); banner.setDelayTime(2000); banner.setImageLoader(new App()); banner.setImages(Arrays.asList(imageurl)); banner.start(); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case android.R.id.home: finish(); break; } return super.onOptionsItemSelected(item); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu,menu); return super.onCreateOptionsMenu(menu); } @Override public void OnSuccess(List<JsonBean.ResultBean.DataBean> list) { StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL); // LinearLayoutManager layoutManager =new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false); recyclerView.setLayoutManager(staggeredGridLayoutManager); RecyclerAdapter adapter = new RecyclerAdapter(MainActivity.this,list); recyclerView.setAdapter(adapter); adapter.setOnItemClickListener(new RecyclerAdapter.OnItemClickListener() { @Override public void onItemClick(View view, int position) { ObjectAnimator alpha = ObjectAnimator.ofFloat(view,"alpha",2,0,2); alpha.setDuration(3000); alpha.start(); } }); } @Override public void OnFailer(String error) { Toast.makeText(this, "失敗", Toast.LENGTH_SHORT).show(); }
}