目录
1.RecyclerView概述
在谷歌Android官网,给RecyclerView的描述是:
那RecyclerView凭什么要比ListView要更高级?更灵活?
答案是,RecyclerView可以自定义布局管理器来决定item的排布规则。如,
LinearLayoutManager、GridLayoutManager、StaggeredGridLayoutManager 等。
2.RecyclerView的简单使用
那我们先用LinearLayoutManager来实现一个简单的列表吧
首先在Gradle添加RecyclerView的依赖
dependencies {
...
implementation 'androidx.recyclerview:recyclerview:1.1.0'
}
在layout里面使用RecyclerView
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_successive_dynasties_huoying"
android:layout_width="match_parent"
android:layout_height="match_parent" />
在MainActivity里面实例化RecyclerView,并设置LayouManager和Adapter
private void initView()
{
recyclerView = findViewById(R.id.rv_successive_dynasties_huoying);
// 线性布局管理器
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
// 用于描述item的适配器
recyclerAdapter = new RecyclerAdapter(huoyingList);
recyclerView.setAdapter(recyclerAdapter);
}
实现RecyclerViewAdpater
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>
{
private List<Huoying> huoyingList;
public RecyclerAdapter(List<Huoying> huoyingList)
{
this.huoyingList = huoyingList;
}
@NonNull
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
ViewHolder viewHolder = new ViewHolder(itemView);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerAdapter.ViewHolder holder, int position)
{
holder.name.setText(huoyingList.get(position).getName());
holder.number.setText(String.valueOf(huoyingList.get(position).getNumber()));
holder.trump.setText(huoyingList.get(position).getTrump());
}
@Override
public int getItemCount()
{
return huoyingList.size();
}
class ViewHolder extends RecyclerView.ViewHolder
{
TextView name; //火影名字
TextView number; //火影的代号
TextView trump; //他的绝招
public ViewHolder(@NonNull View itemView)
{
super(itemView);
this.name = itemView.findViewById(R.id.tv_name);
this.number = itemView.findViewById(R.id.tv_number);
this.trump = itemView.findViewById(R.id.tv_trump);
}
}
}
RecyclerAdapter的关键在与
RecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
}
void onBindViewHolder(@NonNull RecyclerAdapter.ViewHolder holder, int position) {
}
onCreateViewHolder用于将item.xml实例化,并以ViewHolder的形式呈现,
onBindViewHolder用于在初始和滑动RecyclerView时,给item里面的子控件赋值。
其实按照我的浅显理解,onCreateViewHolder和onBindViewHolder加起来就类似于ListView adapter里面的
View getView(int position, View itemView, ViewGroup parent)
你看getView()里面既有parent可以用来实例化item.xml,又有position可以找到item位置来赋值,是不是就如同onCreateViewHolder
和onBindViewHolder的功能!
差异就只是在item的表示形式从View变成了ViewHolder。
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="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="@+id/tv_name"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="千手柱间" />
<TextView
android:id="@+id/tv_number"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="1" />
<TextView
android:id="@+id/tv_trump"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="木遁" />
</LinearLayout>
再看看,我是怎么描述火影的
public class Huoying
{
private int number;
private String name;
private String trump;
public Huoying(int number, String name, String trump)
{
this.number = number;
this.name = name;
this.trump = trump;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getTrump()
{
return trump;
}
public void setTrump(String trump)
{
this.trump = trump;
}
public int getNumber()
{
return number;
}
public void setNumber(int number)
{
this.number = number;
}
@Override
public String toString()
{
return "Huoying{" +
"number=" + number +
", name='" + name + '\'' +
", trump='" + trump + '\'' +
'}';
}
}
最后我们在MainActivity里面给将历代火影数据传递给adapter
public class MainActivity extends AppCompatActivity
{
private RecyclerView recyclerView;
private RecyclerAdapter recyclerAdapter;
private List<Huoying> huoyingList;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
setListener();
}
private void initData()
{
huoyingList = new ArrayList<>(7);
huoyingList.add(new Huoying(1, "千手柱间", "木遁"));
huoyingList.add(new Huoying(2, "千手扉间", "水遁"));
huoyingList.add(new Huoying(3, "猿飞日斩", "猿魔"));
huoyingList.add(new Huoying(4, "波风水门", "飞雷神"));
huoyingList.add(new Huoying(5, "千手纲手", "百豪之术"));
huoyingList.add(new Huoying(6, "旗木卡卡西", "雷切"));
huoyingList.add(new Huoying(7, "漩涡鸣人", "螺旋丸"));
}
private void initView()
{
recyclerView = findViewById(R.id.rv_successive_dynasties_huoying);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerAdapter = new RecyclerAdapter(huoyingList);
recyclerView.setAdapter(recyclerAdapter);
}
private void setListener()
{
}
}
我把完整的R.layout.activity_main也贴出来
<?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="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="历代火影" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="名字" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="序号" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="绝招" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_successive_dynasties_huoying"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
编译运行,效果如下:
3.改变布局管理器,RecyclerView的变化
我们在使用LinearLayoutManager来管理item的样式时,LinearLayoutManager的默认Orientation是LinearLayout.VERTICAL
所以我们可以改成LinearLayout.HORIZONTAL,看看RecyclerView会是怎样的效果
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
编译运行,效果如下:
就变成了横滑显示。
我们再试试GridLayoutManager
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
spanCount设置为3,看看运行效果:
虽然文件看的有点辛苦,但RecyclerView确实变成了有3列数据的网状结构。
还有一些其他的布局管理器大家可以自己慢慢尝试^_^
4.源码下载
https://github.com/messiwangzi/RecyclerView