需求
Listview的不足
- Listview 的行为是: 垂直排列item, 且不可以定制排列方式
- Listview 每个item 的view,其资源的回收方式需要编程实现
- Listview 需要自行实现ViewHolder,并将其绑定到view
Recyclerview的优势:
- 通过setLayoutManager(XXXLayoutManager manger),指定每个item的排列方式
- 内部自动实现资源回收
- ViewHolder对标准化方式实现(内部类)
布局管理的图示:
引入方式
implementation 'com.android.support:recyclerview-v7:27.1.1'
原理级实现
核心工作:实现自定义的Adapter类
public class [自定义类名] extends RecyclerView.Adapter<[自定ViewHolder类] extends UserAdapter.ViewHolder>
{
[自定ViewHolder类] extends UserAdapter.ViewHolder{
[完成(1)从已渲染后的Item View中findView的工作、(2)事件绑定的工作]
}
[自定义类的构造器](Data){
[完成集合数据接收工作]
}
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
[完成Recyclerview的初始化时生成Itemview的工作,以后它们会被回收使用]
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
[初始化及滚动时,每个item view与item之间的绑定工作。注意:此时view中已绑定了viewHolder,所以view在传参中不再出现。]
}
}
具体实现:
package cn.john.app2;
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 android.widget.Toast;
import java.util.List;
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
/**
* [完成(1)从已渲染后的Item View中findView的工作、(2)事件绑定的工作]
*/
static class ViewHolder extends RecyclerView.ViewHolder{
ImageView pic;
TextView name;
public ViewHolder(View view){
super(view);
pic=view.findViewById(R.id.pic);
name=view.findViewById(R.id.name);
name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(),name.getText(),Toast.LENGTH_LONG).show();
}
});
}
}
/**
*[完成集合数据接收工作]
*/
private List<User> users;
public UserAdapter(List<User> users){
this.users=users;
}
/**
*[完成Recyclerview的初始化时生成Itemview的工作,以后它们会被回收使用]
*/
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.user,parent,false);
ViewHolder viewHolder=new ViewHolder(view);
return viewHolder;
}
/**
*[初始化及滚动时,每个item view与item之间的绑定工作。注意:此时view中已绑定了viewHolder,所以view在传参中不再出现。]
*/
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.pic.setImageResource(users.get(position).getPic());
holder.name.setText(users.get(position).getName());
}
@Override
public int getItemCount() {
return users.size();
}
}
附:item view的布局及pojo类
user.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@android:color/holo_orange_light"
android:layout_height="?attr/actionBarSize" android:orientation="horizontal">
<ImageView
android:id="@+id/pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/name"
android:layout_width="60dp"
android:layout_height="wrap_content" />
</LinearLayout>
User.java
public class User {
private Integer id;
private String name;
private int pic;
...setter和getter
....
组装代码:
package cn.john.app2;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<User> users;
/**
* 每个Item view的外边距
*/
class UserDecoration extends RecyclerView.ItemDecoration{
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.set(10,10,10,10);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
UserAdapter userAdapter=new UserAdapter(users);
//可以综合使用LinearLayoutManager、StaggeredGridLayoutManager等布局
//水平排满(2列)后纵向增长(Vertical)
GridLayoutManager gridLayoutManager=new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false);
RecyclerView recyclerView=findViewById(R.id.recyclerView);
recyclerView.addItemDecoration(new UserDecoration());
//另一种加分隔线的方式
//recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(userAdapter);
}
/**
* 数据初始化
*/
private void initData(){
users=new ArrayList<>();
int[] pics=new int[]{android.R.drawable.ic_delete,
android.R.drawable.ic_btn_speak_now,
android.R.drawable.ic_input_add,
android.R.drawable.ic_dialog_alert};
for(int i=0;i<pics.length;i++){
User user=new User();
user.setPic(pics[i]);
user.setName("name"+i);
users.add(user);
}
}
}