【问题标题】:Recyler view vertical with nested recycler view and card listenerRecyclerview 垂直,带有嵌套的回收器视图和卡片侦听器
【发布时间】:2020-03-08 21:15:51
【问题描述】:

所以,我有一个回收站视图,而回收站视图中的项目是动态的。也就是说,可以有一张卡或两张卡。如您所见,recyclerview 索引 1 只有一项,而 recyclerview 索引 0 有两张卡。我怎样才能做到这一点?

【问题讨论】:

标签: android xml layout android-recyclerview


【解决方案1】:

在 onCreateViewHolder 的 Recycler 适配器中添加 if else 或 switch case 用于多个 ui 布局

@Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == LAYOUT_TYPE_ONE) {
            View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_type_one, parent, false);
            return new FeedViewHolder(layoutView);
        } else {
            View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_type_two, parent, false);
            return new FeedViewHolder(layoutView);
        }
    }

【讨论】:

    【解决方案2】:

    你可以使用 notifyDataSetChange();在您的适配器上更新回收站每个

    【讨论】:

      【解决方案3】:

      您可以使用nested recylerview 表示reacyclerview insiderecyclerview。

      您可以按照以下方式。

      activity_main.xml

      <android.support.constraint.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">
      
          <android.support.v7.widget.RecyclerView
              android:id="@+id/recyclerView"
              android:layout_width="match_parent"
              android:layout_height="match_parent"/>
      
      </android.support.constraint.ConstraintLayout>
      

      MainActivity.java

      public class MainActivity extends  AppCompatActivity implements YourInterface {
      
          RecyclerView recyclerView;
      
          private CategoryAdapter adapter;
      
           @Override
           protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              setUpRecyclerView();
      
      
              // add your data to your arraylist and pass it to setInfoToAdapter method like below
      
              ArrayList<CategoryItem> categoryItemList = new ArrayList<>();
              categoryItemList.add(new CategoryItem("Item Title"))
      
              categoryList.add(new Category("Section Title", categoryItemList));
      
              setInfoToAdapter(categoryList );
           }
      
          //setup recycler view
          private void setUpRecyclerView() {
              recyclerView = findViewById(R.id.recyclerView);
              recyclerView.setHasFixedSize(true);
              recyclerView.setLayoutManager(new LinearLayoutManager(this));
          }
      
      
          private void setInfoToAdapter(List<Category> categoryList) {
              adapter = new CategoryAdapter(this, categoryList, this); // here last parameters for your inteface
              recyclerView.setAdapter(adapter);
          }
      
          @Override
          public void doSomething(String title) {
              // here you can do whatever you want it tigger when item adapter call this method.
          }
      }
      

      CategoryAdapter.java

      public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.CategorySectionHolder> {
          private Context context;
          private List<Category> categoryList;
          private YourInterface interface;
      
          public CategoryAdapter(Context context, List<Category> categoryList,YourInterface interface) {
              this.context = context;
              this.categoryList = categoryList;
              this.interface = interface; //
      
          }
      
          @Override
          public CategorySectionHolder onCreateViewHolder(ViewGroup parent, int viewType) {
              View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_section_row, parent, false);
              return new CategorySectionHolder(view);
          }
      
          @Override
          public void onBindViewHolder(CategorySectionHolder holder, int position) {
              final Category category = categoryList.get(position);
              final String categoryName = category.getName();
              holder.tvSectionLabel.setText(categoryName);
      
              // data set for your items
              final ArrayList<CategoryItem> itemList =category.getItems(); // here you got your item list for each category
      
              // adapter for your items
              final CategoryItemAdapter adapter;
              adapter = new CategoryItemAdapter(context, itemList, interface);
      
              //recycler view for items
              holder.rvCategoryItems.setHasFixedSize(true);
              holder.rvCategoryItems.setNestedScrollingEnabled(false);
              holder.rvCategoryItems.setLayoutManager(new LinearLayoutManager(context));
              holder.rvCategoryItems.setAdapter(adapter);
          }
      
          @Override
          public int getItemCount() {
              return categoryList.size();
          }
      
          public static class CategorySectionHolder extends RecyclerView.ViewHolder {
      
              TextView tvSectionLabel;
              RecyclerView rvCategoryItems;
      
              public CategorySectionHolder(View itemView) {
                  super(itemView);
                  // initialize your views here
                  tvSectionLabel = itemView.findViewById(R.id.tvSectionLabel);
                  rvCategoryItems = itemView.findViewById(R.id.rvCategoryItem);
              }
          }
      }
      

      category_section_rowCategoryAdapter 设计,其中有一个recyclerview

      category_section_row.xml

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:padding="@dimen/_5sdp">
      
          <TextView
              android:id="@+id/tvSectionLabel"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="Label"
              android:layout_alignParentLeft="true"
              android:textStyle="bold"
              android:textColor="@android:color/darker_gray"
              android:textSize="@dimen/_14ssp" />
      
          <!--  recycler view for items -->
          <android.support.v7.widget.RecyclerView
              android:id="@+id/rvCategoryItem"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_below="@+id/tvSectionLabel"
              android:layout_marginTop="@dimen/_5sdp" />
      </RelativeLayout>
      

      items的适配器

      CategoryItemAdapter.java

      public class CategoryItemAdapter extends RecyclerView.Adapter<CategoryItemAdapter.CategoryItemsHolder> {
          private Context context;
          private List<CategoryItem> itemModels;
      
          private YourInterface interface;
      
          public CategoryItemAdapter(Context context, List<CategoryItem> itemModels,YourInterface interface) {
              this.context = context;
              this.itemModels = itemModels;
              this.interface = interface;
          }
      
          @Override
          public CategoryItemsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
              View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_item_row, parent, false);
              return new CategoryItemsHolder(view);
          }
      
          @Override
          public void onBindViewHolder(final CategoryItemsHolder holder, final int position) {
      
              holder.itemName.setText(itemModels.get(position).getTitle());
      
              holder.cardView.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      // here you can do whatever you want to click of item cardview.
      
                      // here you call method of your interface 
                      interface.doSomething("Hello from item adapter")
                  }
              });
      
          }
      
          @Override
          public int getItemCount() {
              return itemModels.size();
          }
      
          public static class CategoryItemsHolder extends RecyclerView.ViewHolder {
              TextView itemName;
              CardView cardView
              public CategoryItemsHolder(View itemView) {
                  super(itemView);
                  // initialize your views here.
                  itemName = itemView.findViewById(R.id.tvItemNameCategory);
                  cardView = itemView.findViewById(R.id.cardView);
              }
          }
      }
      

      category_item_row.xml

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.v7.widget.CardView 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="wrap_content"
          android:id="@+id/cardView"
          app:cardBackgroundColor="@color/black"
          app:cardCornerRadius="@dimen/_3sdp"
          app:cardElevation="@dimen/_3sdp">
      
          <android.support.constraint.ConstraintLayout
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:orientation="vertical">
      
              <TextView
                  android:id="@+id/tvItemNameCategory"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:background="?android:selectableItemBackground"
                  android:fontFamily="sans-serif"
                  android:padding="@dimen/_5sdp"
                  android:text="@string/app_name"
                  android:textColor="@color/white"
                  android:textSize="@dimen/_14ssp"
                  app:layout_constraintLeft_toLeftOf="parent"
                  app:layout_constraintTop_toTopOf="parent" />  
      
               <!--Your other view goes there-->
      
          </android.support.constraint.ConstraintLayout>
      </android.support.v7.widget.CardView>
      

      最后,您的 Model 类应该如下所示。

      Category.java

      public class Category{
          private String name;
          private ArrayList<CategoryItems> items;
      
          public Category(String name, ArrayList<CategoryItems> items) {
              this.name = name;
              this.items = items;
      
          }
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name= name;
          }
      
          public ArrayList<CategoryItems> getItems() {
              return items;
          }
      
          public void setItems(ArrayList<CategoryItems> items) {
              this.items= items;
          }
      
         public class CategoryItems{
             private String title;
      
             public Category(String title) {
                this.title = title;
             }
      
             public String getTitle() {
                 return title;
             }
      
             public void setTitle(String title) {
                 this.title= title;
             }
         }
      }
      

      更新: 创建一个这样的界面

      interface YourInterface{
         public void doSomething(String title); //here you can use parameters as you want
      }
      

      现在你必须在YourActivit 中实现这个接口,在这个例子中是MainActivity。 并将instanceMainActivity 传递给CategoryAdapter 然后CategoryItemAdapter。查看我更新的这些课程。

      【讨论】:

      • 好答案,如何为子回收者视图创建一个界面,以便我可以从类别活动中访问它们?
      • 谢谢你是个救星。
      • 多类型数据怎么样?假设在类别数据类中可能有另一个 ummm 布料列表和控制台如何显示它们,就我而言,它会同时显示列表中的任何一个......
      猜你喜欢
      • 2016-12-17
      • 2021-09-12
      • 2018-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多