RecyclerView可以说是一个增强版ListView,不仅可以实现和ListView同样的效果,还优化了ListView中存在的各种不足之处。
新建一个项目
1、在build.gradle中添加包依赖(编译环境 SDK API 28)
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
......
compile 'com.android.support:recyclerview-v7:28.0.0'
}
2、布局文件activity_main.xml添加RecyclerView
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
3、代码中新建一个Fruit、FruitAdapter类
package com.example.recyclerviewtest; 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; } }
fruit_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp"> <ImageView android:layout_width="100dp" android:layout_height="80dp" android:gravity="center_horizontal" android:focusable="true" android:id="@+id/image_view"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text_view" android:textSize="20sp" android:layout_marginLeft="20dp" /> </LinearLayout>
FruitAdapter类
package com.example.recyclerviewtest; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import java.util.List; public class FruitAdapter extends RecyclerView.Adapter <FruitAdapter.ViewHolder>{ private List<Fruit> fruitList; static class ViewHolder extends RecyclerView.ViewHolder{ ImageView fruitImage; TextView fruitName; public ViewHolder(@NonNull View view) { super(view); fruitImage = view.findViewById(R.id.image_view); fruitName = view.findViewById(R.id.text_view); } } public FruitAdapter(List<Fruit> fruitList){ this.fruitList = fruitList; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fruit_item, viewGroup, false); final ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) { Fruit fruit = fruitList.get(i); viewHolder.fruitName.setText(fruit.getName()); viewHolder.fruitImage.setImageResource(fruit.getImageId()); } @Override public int getItemCount() { return fruitList.size(); } }
主MainActivity中
package com.example.recyclerviewtest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<Fruit> fruitList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFruit(); RecyclerView recyclerView = findViewById(R.id.recycler_view); //增加显示部分 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(linearLayoutManager); FruitAdapter fruitAdapter = new FruitAdapter(fruitList); recyclerView.setAdapter(fruitAdapter); } private void initFruit() { for (int i = 0; i < 2; i++){ Fruit apple = new Fruit("apple", R.drawable.apple); fruitList.add(apple); Fruit pear = new Fruit("pear", R.drawable.pear); fruitList.add(pear); Fruit grape = new Fruit("grape", R.drawable.grape); fruitList.add(grape); Fruit orange = new Fruit("orange", R.drawable.orange); fruitList.add(orange); Fruit pineapple = new Fruit("pineapple", R.drawable.pineapple); fruitList.add(pineapple); Fruit strawberry = new Fruit("strawberry", R.drawable.strawberry); fruitList.add(strawberry); Fruit watermelon = new Fruit("watermelon", R.drawable.watermelon); fruitList.add(watermelon); } } }
运行效果
4、实现横向滚动
修改布局文件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="100dp"
android:layout_height="120dp">
<ImageView
android:layout_width="100dp"
android:layout_height="80dp"
android:gravity="center_horizontal"
android:focusable="true"
android:id="@+id/image_view"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_view"
android:textSize="20sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
修改MainActivity中
@Override
protected void onCreate(Bundle savedInstanceState) {
......
RecyclerView recyclerView = findViewById(R.id.recycler_view);
//增加显示部分
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(linearLayoutManager);
FruitAdapter fruitAdapter = new FruitAdapter(fruitList);
recyclerView.setAdapter(fruitAdapter);
}
效果图
5、Recyclerview的点击事件
修改FruitAdapter代码
public class FruitAdapter extends RecyclerView.Adapter <FruitAdapter.ViewHolder>{ private List<Fruit> fruitList; static class ViewHolder extends RecyclerView.ViewHolder{ ImageView fruitImage; TextView fruitName; View fruitView; public ViewHolder(@NonNull View view) { super(view); fruitView = view; fruitImage = view.findViewById(R.id.image_view); fruitName = view.findViewById(R.id.text_view); } } public FruitAdapter(List<Fruit> fruitList){ this.fruitList = fruitList; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fruit_item, viewGroup, false); final ViewHolder holder = new ViewHolder(view); holder.fruitView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = holder.getAdapterPosition(); Fruit fruit = fruitList.get(position); Toast.makeText(v.getContext(), "You click view"+fruit.getName(), Toast.LENGTH_SHORT).show(); } }); holder.fruitImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = holder.getAdapterPosition(); Fruit fruit = fruitList.get(position); Toast.makeText(v.getContext(), "You click image"+fruit.getName(), Toast.LENGTH_SHORT).show(); } }); return holder; } ...... }
效果图