带有嵌套回收者视图和卡片侦听器的Recyler视图垂直

时间:2019-11-13 05:38:58

标签: android xml layout android-recyclerview

因此,我有一个回收站视图,并且回收站视图中的项目是动态的。即可以有一张卡或两张卡。如您所见,recyclerview索引1仅包含一项,而recyclerview索引0具有两张卡片。我该如何实现? enter image description here

3 个答案:

答案 0 :(得分:1)

您可以使用nested recylerview来实现reacyclerview inside recyclerview。

您可以按照以下方式进行操作。

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_row的{​​{1}}设计,其中有一个CategoryAdapter

category_section_row.xml

recyclerview

<?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> 的适配器

CategoryItemAdapter.java

items

category_item_row.xml

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);
        }
    }
}

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

Category.java

<?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>

更新: 创建这样的界面

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中的instance传递到MainActivity,然后传递CategoryAdapter。看到我更新的这些课程。

答案 1 :(得分:0)

在onCreateViewHolder的Recycler Adaptor中,添加if或为多个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 :(得分:0)

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