首先是布局 以及一个RecycleerView的依赖 implementation 'androidx.recyclerview:recyclerview:1.1.0'
private String name;
private int imageId;
public TestRecyclerView(String name, int imageId){
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public int getImageId() {
return imageId;
}
} 然后是主页面设置
private List<TestRecyclerView > fruitList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.home_pager_content_list);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
FruitAdapter adapter = new FruitAdapter(fruitList);//创建适配器
recyclerView.setAdapter(adapter);
}
private void initFruits() { for (int i = 0; i < 2; i++) { Fruit apple = new Fruit("Apple", R.drawable.apple_pic); fruitList.add(apple); Fruit banana = new Fruit("Banana", R.drawable.banana_pic); fruitList.add(banana); Fruit orange = new Fruit("Orange", R.drawable.orange_pic); fruitList.add(orange); Fruit watermelon = new Fruit("Watermelon", R.drawable.watermelon_pic); fruitList.add(watermelon); Fruit pear = new Fruit("Pear", R.drawable.pear_pic); fruitList.add(pear); Fruit grape = new Fruit("Grape", R.drawable.grape_pic); fruitList.add(grape); Fruit pineapple = new Fruit("Pineapple", R.drawable.pineapple_pic); fruitList.add(pineapple); Fruit strawberry = new Fruit("Strawberry", R.drawable.strawberry_pic); fruitList.add(strawberry); Fruit cherry = new Fruit("Cherry", R.drawable.cherry_pic); fruitList.add(cherry); Fruit mango = new Fruit("Mango", R.drawable.mango_pic); fruitList.add(mango);
}
}
创建FruitAdapter适配器 public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
private List
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.home_pager_content_list);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
FruitAdapter adapter = new FruitAdapter(fruitList);//创建适配器
recyclerView.setAdapter(adapter);
}
private void initFruits() {
for (int i = 0; i < 20; i++) {
TestRecyclerView apple = new TestRecyclerView("Apple", R.mipmap.empty);
fruitList.add(apple);
TestRecyclerView banana = new TestRecyclerView("Banana", R.mipmap.back);
fruitList.add(banana);
TestRecyclerView orange = new TestRecyclerView("Orange", R.mipmap.wifi);
fruitList.add(orange);
TestRecyclerView watermelon = new TestRecyclerView("Watermelon", R.mipmap.home_normal);
fruitList.add(watermelon);
TestRecyclerView pear = new TestRecyclerView("Pear", R.mipmap.home_normal);
fruitList.add(pear);
TestRecyclerView grape = new TestRecyclerView("Grape", R.mipmap.wifi);
fruitList.add(grape);
TestRecyclerView pineapple = new TestRecyclerView("Pineapple", R.mipmap.red_packet_checked);
fruitList.add(pineapple);
TestRecyclerView strawberry = new TestRecyclerView("Strawberry",R.mipmap.wifi);
fruitList.add(strawberry);
TestRecyclerView cherry = new TestRecyclerView("Cherry",R.mipmap.scan_white);
fruitList.add(cherry);
TestRecyclerView mango = new TestRecyclerView("Mango", R.mipmap.shanchuyuan);
fruitList.add(mango);
}
}