当前位置:   article > 正文

安卓RecyclerView简单用法

安卓RecyclerView简单用法

废话不多说上代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity">
  9. <androidx.recyclerview.widget.RecyclerView
  10. android:id="@+id/recyclerView1"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:orientation="horizontal" />
  14. <androidx.recyclerview.widget.RecyclerView
  15. android:id="@+id/recyclerView2"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content" />
  18. </LinearLayout>

对应Activity

  1. import androidx.appcompat.app.AppCompatActivity;
  2. import androidx.recyclerview.widget.LinearLayoutManager;
  3. import androidx.recyclerview.widget.RecyclerView;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. public class MainActivity extends AppCompatActivity {
  9. private RecyclerView recyclerView1;
  10. private RecyclerView recyclerView2;
  11. private List<MenuData> dataList;
  12. MenuAdapter adapter;
  13. ListAdapter listAdapter;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. recyclerView1 = findViewById(R.id.recyclerView1);
  19. recyclerView2 = findViewById(R.id.recyclerView2);
  20. LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
  21. recyclerView1.setLayoutManager(layoutManager);
  22. // 创建模拟数据
  23. createMockData();
  24. adapter = new MenuAdapter(dataList, selectedData -> {
  25. Log.d("菜单选择", selectedData.getName());
  26. });
  27. recyclerView1.setAdapter(adapter);
  28. LinearLayoutManager layoutManager2 = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
  29. recyclerView2.setLayoutManager(layoutManager2);
  30. listAdapter = new ListAdapter(dataList, selectedData -> {
  31. Log.d("列表选择", selectedData.getName());
  32. });
  33. recyclerView2.setAdapter(listAdapter);
  34. }
  35. private void createMockData() {
  36. dataList = new ArrayList<>();
  37. for (int i = 1; i <= 10; i++) {
  38. MenuData data = new MenuData("Item " + i, i); // 假设 MenuData 类有一个带有字符串和整型参数的构造函数
  39. dataList.add(data); // 将新创建的 MenuData 对象添加到 dataList 中
  40. }
  41. }
  42. }

 MenuData.实体类

  1. public class MenuData {
  2. private String name;
  3. private int id;
  4. public String getName() {
  5. return name;
  6. }
  7. public void setName(String name) {
  8. this.name = name;
  9. }
  10. public int getId() {
  11. return id;
  12. }
  13. public void setId(int id) {
  14. this.id = id;
  15. }
  16. public MenuData(String name, int id) {
  17. this.name = name;
  18. this.id = id;
  19. }
  20. }

对应适配器menuAdapter,主要实现菜单选择

  1. import android.graphics.Color;
  2. import android.view.LayoutInflater;
  3. import android.view.View;
  4. import android.view.ViewGroup;
  5. import android.widget.TextView;
  6. import androidx.annotation.NonNull;
  7. import androidx.recyclerview.widget.RecyclerView;
  8. import java.util.List;
  9. public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.ViewHolder> {
  10. private List<MenuData> dataList;
  11. private int selectedPosition = -1;
  12. private OnItemSelectedListener listener;
  13. public MenuAdapter(List<MenuData> dataList, OnItemSelectedListener listener) {
  14. this.dataList = dataList;
  15. this.listener = listener;
  16. }
  17. @NonNull
  18. @Override
  19. public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  20. // 创建 ViewHolder
  21. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
  22. return new ViewHolder(view);
  23. }
  24. @Override
  25. public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  26. // 绑定数据到 ViewHolder
  27. MenuData data = dataList.get(position);
  28. holder.bindData(data);
  29. holder.itemView.setOnClickListener(v -> {
  30. if (listener != null) {
  31. selectedPosition = position;
  32. notifyDataSetChanged(); // 刷新列表以更新选中项的样式
  33. listener.onItemSelected(dataList.get(position));
  34. }
  35. });
  36. // 设置选中项的样式
  37. if (position == selectedPosition) {
  38. holder.textView.setTextColor(Color.BLUE);
  39. } else {
  40. holder.textView.setTextColor(Color.BLACK);
  41. }
  42. }
  43. @Override
  44. public int getItemCount() {
  45. // 返回数据列表的大小
  46. return dataList.size();
  47. }
  48. public class ViewHolder extends RecyclerView.ViewHolder {
  49. private TextView textView;
  50. public ViewHolder(@NonNull View itemView) {
  51. super(itemView);
  52. textView = itemView.findViewById(R.id.textView);
  53. }
  54. public void bindData(MenuData data) {
  55. // 绑定数据到视图
  56. textView.setText(data.getName());
  57. }
  58. }
  59. public interface OnItemSelectedListener {
  60. void onItemSelected(MenuData selectedData);
  61. }
  62. }

item_layout.xml

  1. <!-- item_layout.xml -->
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical"
  6. android:padding="8dp">
  7. <TextView
  8. android:id="@+id/textView"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:textSize="16sp"
  12. android:textColor="@android:color/black"/>
  13. </LinearLayout>

接下来就是 ListAdapter适配器,这个主要实现向下列表得

  1. import android.graphics.Color;
  2. import android.view.LayoutInflater;
  3. import android.view.View;
  4. import android.view.ViewGroup;
  5. import android.widget.TextView;
  6. import androidx.annotation.NonNull;
  7. import androidx.recyclerview.widget.RecyclerView;
  8. import java.util.List;
  9. public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListViewHolder> {
  10. private List<MenuData> dataList;
  11. private OnItemSelectedListener listener;
  12. private MenuData selectedData;
  13. public ListAdapter(List<MenuData> dataList, OnItemSelectedListener listener) {
  14. this.dataList = dataList;
  15. this.listener = listener;
  16. }
  17. @NonNull
  18. @Override
  19. public ListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  20. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false);
  21. return new ListViewHolder(view);
  22. }
  23. @Override
  24. public void onBindViewHolder(@NonNull ListViewHolder holder, int position) {
  25. MenuData data = dataList.get(position);
  26. holder.bind(data, listener, data == selectedData);
  27. }
  28. @Override
  29. public int getItemCount() {
  30. return dataList.size();
  31. }
  32. public void updateSelectedData(MenuData selectedData) {
  33. this.selectedData = selectedData;
  34. notifyDataSetChanged();
  35. }
  36. public static class ListViewHolder extends RecyclerView.ViewHolder {
  37. private TextView textView;
  38. public ListViewHolder(@NonNull View itemView) {
  39. super(itemView);
  40. textView = itemView.findViewById(R.id.textView);
  41. }
  42. public void bind(MenuData data, OnItemSelectedListener listener, boolean isSelected) {
  43. textView.setText(data.getName());
  44. itemView.setBackgroundColor(isSelected ? Color.LTGRAY : Color.WHITE);
  45. itemView.setOnClickListener(v -> {
  46. if (listener != null) {
  47. listener.onItemSelected(data);
  48. }
  49. });
  50. }
  51. }
  52. public interface OnItemSelectedListener {
  53. void onItemSelected(MenuData selectedData);
  54. }
  55. }

对应item_list.xml

  1. <!-- item_layout.xml -->
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical"
  6. android:padding="8dp">
  7. <TextView
  8. android:id="@+id/textView"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:textSize="16sp"
  12. android:textColor="@android:color/black"/>
  13. </LinearLayout>

最后效果图

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/293275
推荐阅读
相关标签
  

闽ICP备14008679号