Recyclerview
一:Recyclerview功能
RecyclerView比ListView的优势:
1.RecyclerView比listview实现效果更多
2.RecycelrView支持多布局;
3.RecyclerView根据项目需要插拔功能
RecyclerView默认不支持点击事件->程序员代码中通过回调接口的方式添加监听
线性![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3bcdfc07d1bb6a63b1c49035d6a2ec31.png)
//线性
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerId.setLayoutManager(manager);
网格
//网格
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3);
recyclerId.setLayoutManager(gridLayoutManager);
瀑布
//瀑布流布局
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerId.setLayoutManager(staggeredGridLayoutManager);
二:代码
MainActivity
package com.example.recyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.annotation.SuppressLint;
import android.os.Bundle;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerId;
private List<Integer> list = new ArrayList<>();
@SuppressLint("WrongConstant")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerId = (RecyclerView) findViewById(R.id.recycler_id);
//线性
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
// recyclerId.setLayoutManager(manager);
//网格
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3);
// recyclerId.setLayoutManager(gridLayoutManager);
//瀑布流布局
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerId.setLayoutManager(staggeredGridLayoutManager);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String s = format.format(new Date());
list.add(R.mipmap.yi);
list.add(R.mipmap.er);
list.add(R.mipmap.san);
list.add(R.mipmap.si);
list.add(R.mipmap.wu);
list.add(R.mipmap.liu);
list.add(R.mipmap.qi);
list.add(R.mipmap.ba);
list.add(R.mipmap.jiu);
list.add(R.mipmap.shi);
list.add(R.mipmap.one);
list.add(R.mipmap.two);
list.add(R.mipmap.three);
list.add(R.mipmap.four);
list.add(R.mipmap.five);
list.add(R.mipmap.six);
list.add(R.mipmap.seven);
list.add(R.mipmap.ength);
list.add(R.mipmap.nine);
list.add(R.mipmap.ten);
MyAdapter myAdapter = new MyAdapter(list, this);
recyclerId.setAdapter(myAdapter);
DefaultItemAnimator animator = new DefaultItemAnimator();
animator.setAddDuration(2000);
animator.setRemoveDuration(2000);
recyclerId.setItemAnimator(animator);//设置添加删除动画
DividerItemDecoration decoration = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
recyclerId.addItemDecoration(decoration);//添加分割线
}
}
适配器
package com.example.recyclerview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHodler> {
List<Integer> list;
Context context;
public MyAdapter(List<Integer> list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public MyViewHodler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//返回ViewHodler
View item = LayoutInflater.from(context).inflate(R.layout.item_layout,null);
MyViewHodler hodler = new MyViewHodler(item);
return hodler;
}
@Override
public void onBindViewHolder(@NonNull MyViewHodler holder, final int position) {
int hightValue = (int) (Math.random()*201+100);
ViewGroup.LayoutParams layoutParams = holder.imageView.getLayoutParams();
layoutParams.height = hightValue;
holder.imageView.setLayoutParams(layoutParams);
holder.imageView.setImageResource(list.get(position));
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.remove(position);
notifyItemRemoved(position);//更新
notifyItemRangeChanged(position,list.size());//删除时解决错位问题
Toast.makeText(context, "删除"+list.get(position), Toast.LENGTH_SHORT).show();
}
});
holder.imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
list.add(R.mipmap.liu);
notifyItemInserted(position);//添加电动画
return true;
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class MyViewHodler extends RecyclerView.ViewHolder{
ImageView imageView;
public MyViewHodler(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.textView_id);
}
}
}