使用方法
1、添加引用
implementation 'com.android.support:recyclerview-v7:27.1.1'
2、创建布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".recycler.RecyclerActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
3 、创建item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_launcher_background" />
<TextView
android:layout_marginLeft="20dp"
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
android:textSize="22sp" />
</LinearLayout>
4、创建Adapter
RecyclerView的apater 和listview、Gradview 的adapter有些不同 ,Holider 需要继承RecyclerView.ViewHolder,adapter 需要继承RecyclerView.Adapter<T>。
主要方法:
创建ViewHolder
@NonNull
@Override
public MyHolider onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//ViewHolider 和布局文件的绑定
View view = LayoutInflater.from(mContext).inflate(R.layout.item_name, null);
MyHolider myHolider = new MyHolider(view);
return myHolider;
}
设置item显示的内容
@Override
public void onBindViewHolder(@NonNull MyHolider holder, int position) {
holder.name.setText(mList.get(position));
}
item 的数量
@Override
public int getItemCount() {
return mList.size();
}
ViewHolider
public class MyHolider extends RecyclerView.ViewHolder {
//此处可以添加自己显示的属性
private TextView name;
public MyHolider(View itemView) {
super(itemView);
//..........
name = itemView.findViewById(R.id.name);
}
}
adapter 全部代码
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolider> {
private List<String> mList;
private Context mContext;
public MyAdapter(List<String> mList, Context mContext) {
this.mList = mList;
this.mContext = mContext;
}
@NonNull
@Override
public MyHolider onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_name, null);
MyHolider myHolider = new MyHolider(view);
return myHolider;
}
@Override
public void onBindViewHolder(@NonNull MyHolider holder, int position) {
holder.name.setText(mList.get(position));
}
@Override
public int getItemCount() {
return mList.size();
}
public class MyHolider extends RecyclerView.ViewHolder {
private TextView name;
public MyHolider(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
}
}
}
Activity 代码
public class RecyclerActivity extends AppCompatActivity {
RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler);
mRecyclerView = findViewById(R.id.recyclerview);
// mRecyclerView.setLayoutManager(new GridLayoutManager(this,5));
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(new MyAdapter(getData(),this));
}
private List<String> getData() {
List<String> list = new ArrayList<>();
for (int i = 1; i < 30; i++) {
list.add(" 张三 " + i + " 号");
}
return list;
}
}
正常listView效果
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
效果
GridView 效果
后面的数字是一行显示几个
mRecyclerView.setLayoutManager(new GridLayoutManager(this,2));
效果