recyclerview

//Main

public class MainActivity extends AppCompatActivity {
 private int[] imgId = new int[]{
   R.drawable.aaaa,
   R.drawable.abc,
   R.drawable.b,
   R.drawable.c
 };
 private RecyclerView rv;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  //RcyclerView找到控件,设置适配器
  rv = (RecyclerView) findViewById(R.id.recyclerView);
  ArrayList<User> list = new ArrayList<>();
  //初始化数据
  for (int i = 0; i < 100; i++) {
   String conent = i + "林志玲";
   int img = imgId[i % imgId.length];
   User user = new User(img, conent);
   list.add(user);
  }
  //设置布局管理器
  rv.setLayoutManager(new LinearLayoutManager(MainActivity.this));
  //设置动画
  rv.setItemAnimator(new DefaultItemAnimator());
  //设置适配器
  final MyAdapter adapter = new MyAdapter(MainActivity.this, list);
  rv.setAdapter(adapter);
  //设置条目点击事件
  adapter.setOnMyItemClickListener(new MyAdapter.MyItemClick() {
   @Override
   public void itemClick(View view, int postion) {
    Toast.makeText(MainActivity.this,"---"+postion,Toast.LENGTH_SHORT).show();
    User user = new User(R.mipmap.ic_launcher, "hello 机器人");
//    adapter.addUser(user,postion);
    adapter.removeUser(postion);
   }
  });
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.item, menu);
  return true;
 }
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  int itemId = item.getItemId();
  switch (itemId) {
   case R.id.one:
    //listView
    rv.setLayoutManager(new LinearLayoutManager(MainActivity.this));
    break;
   case R.id.two:
    //gridview
    rv.setLayoutManager(new GridLayoutManager(MainActivity.this,2));
    break;
   case R.id.three:
//流失布局
    rv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
    break;
  }
  return super.onOptionsItemSelected(item);
 }
}

//MyAdapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
 private  Context context;
 private  ArrayList<User> list;
 private MyItemClick myItemClick;//监听器
 //
 public MyAdapter(Context context, ArrayList<User> list){
  this.context = context;
  this.list = list;
 }
 //创建ViewHolder的时候
 //实例化ViewHolder
 @Override
 public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  //条目布局
  View view = LayoutInflater.from(context).inflate(R.layout.item, null);
  //实例化ViewHolder ,将布局传入
  MyViewHolder viewHolder = new MyViewHolder(view);
  return viewHolder;
 }
 //当绑定ViewHolder的时候
 //给控件设置数据
 @Override
 public void onBindViewHolder(final MyViewHolder holder, final int position) {
  holder.getTextView().setText(list.get(position).getContent());
  holder.getImageView().setImageResource(list.get(position).getImageId());
  //设置点击事件
  if (myItemClick!=null){
   holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
     //调用接口里面的方法
     myItemClick.itemClick(holder.itemView,position);
    }
   });
  }
 }
 //条目数量
 @Override
 public int getItemCount() {
  return list.size();
 }
 //定义接口,实现条目点击事件
 public interface MyItemClick{
  void itemClick(View view,int postion);
 }
 public void setOnMyItemClickListener(MyItemClick myItemClick){
  this.myItemClick = myItemClick;
 }
 //添加数据
 public void addUser(User user,int position){
  list.add(position,user);
  //更新适配器
  notifyItemInserted(position);//参数,位置,在那里插入了数据
 }
 //删除数据
 public void removeUser(int position){
  list.remove(position);
  //通知适配器
  notifyItemRemoved(position);
 }
 //ViewHolder
 class MyViewHolder extends RecyclerView.ViewHolder{
  private ImageView imageView;
  private TextView textView;
  public MyViewHolder(View itemView) {
   super(itemView);
   this.imageView = itemView.findViewById(R.id.image);
   this.textView = itemView.findViewById(R.id.content);
  }
  //getter  setter
  public ImageView getImageView() {
   return imageView;
  }
  public void setImageView(ImageView imageView) {
   this.imageView = imageView;
  }
  public TextView getTextView() {
   return textView;
  }
  public void setTextView(TextView textView) {
   this.textView = textView;
  }
 }


//User
public class User {
 private int imageId;
 private String content;
 public User(int imageId, String content) {
  this.imageId = imageId;
  this.content = content;
 }
 public User() {
 }
 public int getImageId() {
  return imageId;
 }
 public void setImageId(int imageId) {
  this.imageId = imageId;
 }
 public String getContent() {
  return content;
 }
 public void setContent(String content) {
  this.content = content;
 }
}

//item布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:gravity="center"
              android:orientation="vertical">
    <ImageView
        android:scaleType="centerCrop"
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/aaaa"/>
    <TextView
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:gravity="center"
        android:text="测试"
        android:textSize="30sp"/>
</LinearLayout>


//main布局LinearLayout
   <android.support.v7.widget.RecyclerView
       android:id="@+id/recyclerView"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值