RecyclerView是一种新的视图组,目标是为任何基于适配器的视图提供相似的渲染方式。它被作为ListView和GridView控件的继承者,在support-v7中提供支持。
RecyclerView架构,提供了一种插拔式的体验,高度的解耦,异常的灵活,通过设置它提供的不同LayoutManager,ItemDecoration,ItemAnimator实现令人膛目的效果。
总之,ListView和GridView能做的RecyclerView都能做,并且能实现瀑布流效果。
RecyclerView效果展示
implementation 'com.android.support:recyclerview-v7:21.0.3'
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="添加"
android:textAllCaps="false"/>
<Button
android:id="@+id/delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="删除"
android:textAllCaps="false"/>
<Button
android:id="@+id/list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="List"
android:textAllCaps="false"/>
<Button
android:id="@+id/grid"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Grid"
android:textAllCaps="false"/>
<Button
android:id="@+id/flow"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Flow"
android:textAllCaps="false"/>
LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
LinearLayout>
在RecyclerViewActivity类中引用这个布局,并编写initView()和initData()方法初始化视图和数据,再添加各个按钮的监听
//布局中的视图
private Button add,delete,list,grid,flow;
private RecyclerView recyclerView;
private MyRecyclerViewAdapter adapter;
//用来放置数据的集合
private ArrayList<String> datas;
/**
* 初始化数据
*/
private void initData() {
//准备数据集合
datas = new ArrayList<>();
for(int i = 0;i < 100;i++){
datas.add("Content_" + i);
}
}
/**
* 初始化视图
*/
private void initView() {
add = findViewById(R.id.add);
delete = findViewById(R.id.delete);
list = findViewById(R.id.list);
grid = findViewById(R.id.grid);
flow = findViewById(R.id.flow);
recyclerView = findViewById(R.id.recyclerview);
add.setOnClickListener(this);
delete.setOnClickListener(this);
list.setOnClickListener(this);
grid.setOnClickListener(this);
flow.setOnClickListener(this);
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="#ffffff">
<LinearLayout
android:background="#22000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<ImageView
android:id="@+id/iv_icon"
android:src="@drawable/nullseach"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Content"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:layout_gravity="center"
android:textAllCaps="false"
android:textColor="#000000"/>
LinearLayout>
RelativeLayout>
设置RecyclerView的适配器:新建一个MyRecyclerViewAdapter.java类,继承自RecyclerView.Adapter并传入泛型
编写内部类MyViewHolder继承自RecyclerView.ViewHolder
class MyViewHolder extends RecyclerView.ViewHolder{
private ImageView iv_icon;
private TextView tv_title;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
iv_icon = itemView.findViewById(R.id.iv_icon);
tv_title = itemView.findViewById(R.id.tv_title);
}
}
构造器并传入数据:
private final Context context;
private ArrayList<String> datas;
public MyRecyclerViewAdapter(Context context, ArrayList<String> datas) {
this.context = context;
this.datas = datas;
}
重写3个方法:onCreateViewHolder()、onBindViewHolder()、getItemCount()
/**
* 相当于getView方法中创建View和ViewHolder
* @param parent
* @param viewType
* @return
*/
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = View.inflate(context, R.layout.item_recyclerview,null);
return new MyViewHolder(itemView);
}
/**
* 相当于getView方法中数据和View绑定
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
//根据位置得到对应的数据
String data = datas.get(position);
holder.tv_title.setText(data);
}
/**
* 得到总条数
* @return
*/
@Override
public int getItemCount() {
return datas.size();
}
RecyclerView通过设置LayoutManager来实现不同的展示效果
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false));
recyclerView.setLayoutManager(new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false));
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
设置分割线(默认的样式,可自定义):
//添加RecyclerView的分割线
recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
设置item的点击事件
/**
* 对各个按钮进行监听
* @param v
*/
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.add:
adapter.addData(0,"new_content");
recyclerView.scrollToPosition(0);
break;
case R.id.delete:
adapter.removeData(0);
break;
case R.id.list:
//设置ListView类型效果
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false));
break;
case R.id.grid:
//设置GridView类型效果
recyclerView.setLayoutManager(new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false));
break;
case R.id.flow:
//设置瀑布流类型效果
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
break;
}
}
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "data==" + datas.get(getLayoutPosition()), Toast.LENGTH_SHORT).show();
}
});
iv_icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,"我是图片==" + datas.get(getLayoutPosition()),Toast.LENGTH_LONG).show();
}
});
/**
* 添加数据
* @param position 添加数据的位置
* @param data 添加的数据内容
*/
public void addData(int position, String data) {
datas.add(position,data);
//刷新适配器
notifyItemInserted(position);
}
/**
* 移除数据
* @param position
*/
public void removeData(int position) {
datas.remove(position);
//刷新适配器
notifyItemRemoved(position);
}
添加动画(默认的,可自定义):
//设置动画
recyclerView.setItemAnimator(new DefaultItemAnimator());
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="添加"
android:textAllCaps="false"/>
<Button
android:id="@+id/delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="删除"
android:textAllCaps="false"/>
<Button
android:id="@+id/list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="List"
android:textAllCaps="false"/>
<Button
android:id="@+id/grid"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Grid"
android:textAllCaps="false"/>
<Button
android:id="@+id/flow"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Flow"
android:textAllCaps="false"/>
LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="#ffffff">
<LinearLayout
android:background="#22000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<ImageView
android:id="@+id/iv_icon"
android:src="@drawable/nullseach"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Content"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:layout_gravity="center"
android:textAllCaps="false"
android:textColor="#000000"/>
LinearLayout>
RelativeLayout>
package com.swpuiot.test.test4;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
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 com.swpuiot.test.R;
import java.util.ArrayList;
public class RecyclerViewActivity extends AppCompatActivity implements View.OnClickListener {
//布局中的视图
private Button add,delete,list,grid,flow;
private RecyclerView recyclerView;
private MyRecyclerViewAdapter adapter;
//用来放置数据的集合
private ArrayList<String> datas;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recyclerview);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("RecyclerView测试");
//初始化视图
initView();
//初始化数据
initData();
//设置RecyclerView的适配器
adapter = new MyRecyclerViewAdapter(RecyclerViewActivity.this,datas);
recyclerView.setAdapter(adapter);
//设置RecyclerView的LayoutManager
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false));
//添加RecyclerView的分割线
recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
//设置动画
recyclerView.setItemAnimator(new DefaultItemAnimator());
}
/**
* 初始化数据
*/
private void initData() {
//准备数据集合
datas = new ArrayList<>();
for(int i = 0;i < 100;i++){
datas.add("Content_" + i);
}
}
/**
* 初始化视图
*/
private void initView() {
add = findViewById(R.id.add);
delete = findViewById(R.id.delete);
list = findViewById(R.id.list);
grid = findViewById(R.id.grid);
flow = findViewById(R.id.flow);
recyclerView = findViewById(R.id.recyclerview);
add.setOnClickListener(this);
delete.setOnClickListener(this);
list.setOnClickListener(this);
grid.setOnClickListener(this);
flow.setOnClickListener(this);
}
/**
* 对各个按钮进行监听
* @param v
*/
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.add:
adapter.addData(0,"new_content");
recyclerView.scrollToPosition(0);
break;
case R.id.delete:
adapter.removeData(0);
break;
case R.id.list:
//设置ListView类型效果
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false));
break;
case R.id.grid:
//设置GridView类型效果
recyclerView.setLayoutManager(new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false));
break;
case R.id.flow:
//设置瀑布流类型效果
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
break;
}
}
}
package com.swpuiot.test.test4;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.swpuiot.test.R;
import java.util.ArrayList;
/**
* Time: 2021/4/10
* Author: lenovo
* Description:
*/
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> {
private final Context context;
private ArrayList<String> datas;
public MyRecyclerViewAdapter(Context context, ArrayList<String> datas) {
this.context = context;
this.datas = datas;
}
/**
* 相当于getView方法中创建View和ViewHolder
* @param parent
* @param viewType
* @return
*/
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = View.inflate(context, R.layout.item_recyclerview,null);
return new MyViewHolder(itemView);
}
/**
* 相当于getView方法中数据和View绑定
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
//根据位置得到对应的数据
String data = datas.get(position);
holder.tv_title.setText(data);
}
/**
* 得到总条数
* @return
*/
@Override
public int getItemCount() {
return datas.size();
}
/**
* 添加数据
* @param position 添加数据的位置
* @param data 添加的数据内容
*/
public void addData(int position, String data) {
datas.add(position,data);
//刷新适配器
notifyItemInserted(position);
}
/**
* 移除数据
* @param position
*/
public void removeData(int position) {
datas.remove(position);
//刷新适配器
notifyItemRemoved(position);
}
class MyViewHolder extends RecyclerView.ViewHolder{
private ImageView iv_icon;
private TextView tv_title;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
iv_icon = itemView.findViewById(R.id.iv_icon);
tv_title = itemView.findViewById(R.id.tv_title);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "data==" + datas.get(getLayoutPosition()), Toast.LENGTH_SHORT).show();
}
});
iv_icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,"我是图片==" + datas.get(getLayoutPosition()),Toast.LENGTH_LONG).show();
}
});
}
}
}
完