RecycleView
-
RecycleView是什么? 在很多列表场景中能替代ListView和GridView RecycleView能够灵活实现大数据的展示,视图的复用管理比ListView更好,能够显示列表,网格,瀑布等形式,且不同的ViewHolder能够实现item多元化的功能 -
RecycleView的用法是什么? 布局文件: 1. 主布局文件中添加<RecycleView>
2. 创建子项布局文件,并对其内部控件设置id 代码:
-
创建实体类(如果有需求)
public class AddressBook {
private String name;
private int imgId;
public AddressBook(String name,int imgId){
this.name=name;
this.imgId=imgId;
}
public String getName() {
return name;
}
public int getImageId() {
return imgId;
}
}
- 2创建适配器
-
public class AddressBookAdapter extends RecyclerView.Adapter<AddressBookAdapter.ViewHolder> {
private List<AddressBook> list;
public AddressBookAdapter(List<AddressBook> list){
this.list=list;
}
@NonNull
@Override
public AddressBookAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.add81_item, parent, false);
ViewHolder viewHolder=new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull AddressBookAdapter.ViewHolder holder, int position) {
AddressBook addressBook=list.get(position);
holder.AddressBookImage.setImageResource(addressBook.getImageId());
holder.AddressBookImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), addressBook+"被点击", Toast.LENGTH_SHORT).show();
}
});
holder.AddressBookName.setText(addressBook.getName());
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), addressBook.getName()+"被点击", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
View view;
ImageView AddressBookImage;
TextView AddressBookName;
public ViewHolder(@NonNull View itemView) {
super(itemView);
view=itemView;
AddressBookImage=itemView.findViewById(R.id.iv_img);
AddressBookName=itemView.findViewById(R.id.tv_name);
}
}
-
3在活动中创建并设置适配器
public class MainActivity extends AppCompatActivity {
private String[] names = {"张三", "李四", "王五","赵六"};
private int[] imgs = {R.mipmap.qingning, R.mipmap.huoguo, R.mipmap.shengjian,R.mipmap.huiyuan};
private List<AddressBook> list = new ArrayList<>();
private RecyclerView rv_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ininDate();
rv_view = findViewById(R.id.rv_view);
RecyclerView recyclerView = findViewById(R.id.rv_view);
//设置布局管理器
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
// rv_view.setLayoutManager(layoutManager);
// recyclerView.setAdapter(adapter);
// rv_view.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
rv_view.setLayoutManager(layoutManager);
AddressBookAdapter adapter = new AddressBookAdapter(list);
rv_view.setAdapter(adapter);
}
private void ininDate() {
for (int i = 0; i < names.length; i++) {
AddressBook addressBook = new AddressBook(names[i], imgs[i]);
list.add(addressBook);
}
}
- 4在活动中设置排列方式及分隔线
-
p_swiper = findViewById(R.id.vp_swiper);
SwiperAdapter swiperAdapter = new SwiperAdapter(list);
rv_view.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
vp_swiper.setAdapter(swiperAdapter); -
-
设置点击事件 -
holder.AddressBookImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), addressBook+"被点击", Toast.LENGTH_SHORT).show();
}
});
holder.AddressBookName.setText(addressBook.getName());
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), addressBook.getName()+"被点击", Toast.LENGTH_SHORT).show();
}
});
}
ViewPager2
-
ViewPager2是什么? ViewPager2 是基于 RecyclerView 实现的,自然继承了 RecyclerView 的众多优点,并且针对 ViewPager 存在的问题做了优化。 支持垂直方向的滑动且实现极其简单。 完全支持 RecyclerView 的相关配置功能。 支持多个 PageTransformer。 支持 DiffUtil,局部数据刷新和 Item 动画。 支持模拟用户滑动与禁止用户操作 -
ViewPager2的用法? 布局文件: 1. 主布局文件中添加<ViewPager2>
2. 创建子项布局文件,并对其内部控件设置id 代码:
-
创建实体类(如果有需求) -
创建适配器 -
在活动中创建并设置适配器 -
. CompositePageTransformer 与自定义 Transformer -
实现一屏多页效果? ?
跟上面代码差不多 请合理编写
|