官方教程: Recyclerview
添加依赖,如果不看教程,我是不知道这个全名: androidx.recyclerview:recyclerview:1.1.0 。所以我一般都会去搜索,android studio 中的添加项目依赖的路径: File>Project Structure>Dependencies 然后选择你要添加的模块,一般都是选择 app ,如下图:
然后进去会有这样的对话框:
gradle.build 中 dependencies 中具体的含义可以看 gradle dependencies
添加完成以后就可以正儿八经的看 Recyclerview 的用法了。
Recyclerview 依赖 RecyclerView.Adapter 和 RecyclerView.LayoutManager
深入的学习会在以后进行,今天主要是简单用法,写一个超级简单的列表,像下图一样:
- 添加布局,首先在布局文件中添加:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/lists"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
在编写简单的代码前,先抛出几个问题:
- 我们有了数据,怎样跟视图建立联系?
- 视图按怎样的方式进行显示?
这两个问题刚好对应了上面的两个: Adapter 和 LayoutManager 。
- Adapter 将数据与视图进行适配,因为布局不直接识别数据,需要有一个适配器。
- LayoutManager 就是布局管理器,主要的解决的就是上面数据怎样显示的问题。
其中布局方面官方已经示例化几个布局,比如线性布局( LinearLayoutManager ) , 网络布局( GridLayoutManager )。
这里我用的是线性布局。下面要说的是 Adapter ,需要实现这个抽象类,我是这样写的:
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {
private final Context mContext;
private final ArrayList<String> strs;
public MyRecyclerViewAdapter(Context context, ArrayList<String> strings) {
this.mContext = context;
this.strs = strings;
}
@NonNull
@NotNull
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
return new RecyclerViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_layout, parent, false));
}
@Override
public void onBindViewHolder(@NonNull @NotNull RecyclerViewHolder holder, int position) {
holder.mButtom.setText(R.string.name);
holder.mTextView.setText(strs.get(position));
}
@Override
public int getItemCount() {
return strs.size();
}
}
其中要实现的 onCreateViewHolder 方法需要返回一个 RecyclerView.ViewHolder 对象,而这个也是抽象类,所以也得实现:
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
Button mButtom;
TextView mTextView;
public RecyclerViewHolder(@NonNull @org.jetbrains.annotations.NotNull View itemView) {
super(itemView);
mButtom = itemView.findViewById(R.id.button);
mTextView = itemView.findViewById(R.id.textView);
}
}
这个的具体作用我理解的就是桥梁,每一项视图和每一项数据的桥梁:
我是这样理解的,也不知道对不对,感觉是这样的,如果不对有大佬帮我纠正一下。
其中 item_layout.xml 布局文件我是这样写的:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBaseline_toBaselineOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
然后把这个在 Activity 中连接起来:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView mRecyclerView = findViewById(R.id.lists);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(linearLayoutManager);
ArrayList<String> strs = new ArrayList<>();
strs.add("吴敬");
strs.add("吴天歌");
strs.add("吴敬悦");
mRecyclerView.setAdapter(new MyRecyclerViewAdapter(this, strs));
}
}
这样就得到了上面的效果了,我估计很多小伙伴看到我做的就会丧失学习的动力。不过没关系,理解了就可以做更好看的页面了。