RecyclerView的简单使用

RecyclerView的功能很强大,本文为大家分享RecyclerView的简单使用方法。

效果图:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <android.support.v7.widget.RecyclerView
  android:id="@+id/recycler_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>

</LinearLayout>

fruit_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_margin="5dp">

 <ImageView
  android:id="@+id/fruit_image"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"/>

 <TextView
  android:id="@+id/fruit_name"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="left"
  android:layout_marginTop="10dp"/>

</LinearLayout>

Fruit实体类:

public class Fruit {

 private String name;
 private int imageId;

 public Fruit(String name, int imageId) {
  this.name = name;
  this.imageId = imageId;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getImageId() {
  return imageId;
 }

 public void setImageId(int imageId) {
  this.imageId = imageId;
 }
}

FruitAdapter:

public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {

 private List<Fruit> mFruitList;

 static class ViewHolder extends RecyclerView.ViewHolder{
  ImageView fruitImage;
  TextView fruitName;
  public ViewHolder(View view){
   super(view);
   fruitImage = view.findViewById(R.id.fruit_image);
   fruitName = view.findViewById(R.id.fruit_name);
  }
 }

 public FruitAdapter(List<Fruit> fruitList){
  mFruitList = fruitList;
 }

 @Override
 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  View view = LayoutInflater.from(parent.getContext()).inflate(
    R.layout.fruit_item,parent,false);
  final ViewHolder holder = new ViewHolder(view);
  holder.fruitImage.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
    Fruit fruit = mFruitList.get(holder.getAdapterPosition());
    Toast.makeText(view.getContext(),"you clicked view "+fruit.getName(),Toast.LENGTH_SHORT).show();
   }
  });
  return holder;
 }

 @Override
 public void onBindViewHolder(ViewHolder holder, int position) {
  Fruit fruit = mFruitList.get(position);
  holder.fruitImage.setImageResource(fruit.getImageId());
  holder.fruitName.setText(fruit.getName());
 }

 @Override
 public int getItemCount() {
  return mFruitList.size();
 }

}

MainActivity:

public class MainActivity extends AppCompatActivity {

 List<Fruit> fruitList = new ArrayList<>();

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initFruit();
  RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
  StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(3,
    StaggeredGridLayoutManager.VERTICAL);
  recyclerView.setLayoutManager(manager);
  FruitAdapter adapter = new FruitAdapter(fruitList);
  recyclerView.setAdapter(adapter);
 }

 public void initFruit(){
  for (int i = 0; i < 2; i++) {
   Fruit apple = new Fruit(getRandomLengthName("Apple"), R.drawable.apple_pic);
   fruitList.add(apple);
   Fruit banana = new Fruit(getRandomLengthName("Banana"), R.drawable.banana_pic);
   fruitList.add(banana);
   Fruit orange = new Fruit(getRandomLengthName("Orange"), R.drawable.orange_pic);
   fruitList.add(orange);
   Fruit watermelon = new Fruit(getRandomLengthName("Watermelon"), R.drawable.watermelon_pic);
   fruitList.add(watermelon);
   Fruit pear = new Fruit(getRandomLengthName("Pear"), R.drawable.pear_pic);
   fruitList.add(pear);
   Fruit grape = new Fruit(getRandomLengthName("Grape"), R.drawable.grape_pic);
   fruitList.add(grape);
   Fruit pineapple = new Fruit(getRandomLengthName("Pineapple"), R.drawable.pineapple_pic);
   fruitList.add(pineapple);
   Fruit strawberry = new Fruit(getRandomLengthName("Strawberry"), R.drawable.strawberry_pic);
   fruitList.add(strawberry);
   Fruit cherry = new Fruit(getRandomLengthName("Cherry"), R.drawable.cherry_pic);
   fruitList.add(cherry);
   Fruit mango = new Fruit(getRandomLengthName("Mango"), R.drawable.mango_pic);
   fruitList.add(mango);
  }
 }


 public String getRandomLengthName(String name){
  Random random = new Random();
  int length = random.nextInt(20)+1;
  StringBuilder builder = new StringBuilder();
  for(int i = 0; i < length; i++){
   builder.append(name);
  }
  return builder.toString();
 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持菜鸟教程(cainiaojc.com)。

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#cainiaojc.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。