效果圖:
步驟:
RecyclerView 四大設置:
四大設置 | 描述 | 對應的設置方法 |
---|---|---|
(必選)RecyclerView.Adapter | 為Item提供數據 | .setAdapter() |
(必選)RecyclerView.LayoutManager | 用來管理UI布局顯示 | .setLayoutManager() |
(可選)RecyclerView.ItemDecoration | 可用來繪製Divider(底線樣式)、設置padding等 | .addItemDecoration() |
(可選)RecyclerView.ItemAnimator | 負責添加、刪除資料時的"動畫效果", 默認DefaultItemAnimator() | .setItemAnimator() |
其中RecyclerView.LayoutManager 常見佈局設置有:
item樣式item_layout.xml
<?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"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginVertical="6dp"
android:orientation="horizontal">
<TextView
android:id="@+id/ind_text"
android:layout_width="35dp"
android:layout_height="match_parent"
android:gravity="right|center_vertical"
android:paddingRight="6dp"
android:text="1" />
<ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="match_parent"
android:padding="3dp"
android:scaleType="fitCenter"
app:srcCompat="@drawable/ic_android_24dp" />
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
主頁面activity_main.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="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
static class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private static class ViewHolder extends RecyclerView.ViewHolder {
public TextView indText;
public TextView textView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
indText = itemView.findViewById(R.id.ind_text);
textView = itemView.findViewById(R.id.textview);
}
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// 加載Item布局文件
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_layout, parent, false);
return new ViewHolder(view);
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.indText.setText(String.valueOf(position + 1));
holder.textView.setText("hashcode:" + holder.hashCode());
// 設置item的點擊事件處理
holder.itemView.setOnClickListener((view) -> {
Toast.makeText(
view.getContext(),
"你點擊了Item" + (position + 1),
Toast.LENGTH_SHORT
).show();
});
}
@Override
public int getItemCount() {
return 30;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// 設置布局管理器,設為線性布局(預設垂直)
binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
// 對RecyclerView添加底線樣式
binding.recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
// 設置我們實作的RecyclerVIew Adapter
binding.recyclerView.setAdapter(new MyAdapter());
...
}
...
}
static class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
...
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
...
// 設置item的點擊事件處理
holder.itemView.setOnClickListener((view) -> {
Toast.makeText(
view.getContext(),
"你點擊了Item" + (position + 1),
Toast.LENGTH_SHORT
).show();
});
}
...
}