1

我有这种类型的 Json:

[
    {
           "title": "title1",
            ....
           "category": "tag1"
        },

        {
           "title": "title1",
            ....
           "category": "tag2"

        },

          {
          "title": "title1",
            ....
           "category": "tag3"

        }

     ]

我在我的 recyclerview 中使用 flexboxlayout 对相同标题的类别进行分组:

弹性盒布局:

<com.google.android.flexbox.FlexboxLayout
                android:id="@+id/tags_holder"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="10dp"
                app:flexWrap="wrap"
                app:showDividerVertical="middle"
                app:flexDirection="column_reverse"
                app:alignItems="stretch"
                app:alignContent="stretch"/>

适配器:

@Override public void bindViewHolder(final FlexibleAdapter adapter, final SimpleViewHolder holder, int position, List payloads) {

    holder.title.setText(replacement.getTitle());

LinearLayout 布局 = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.textview, null); TextView tagView = (TextView) layout.findViewById(R.id.textview);

tagView.setText(replacement.getCategory());
holder.mTagsHolder.addView(layout);

主要活动;

private void initializeRecyclerView(final List<NewsItem> categoryList) {
            List<AbstractFlexibleItem> items = new ArrayList<>();

            SimpleItem simpleItem;
            List<String> list = new ArrayList<>();

            String category;


            for (int i = 0; i < categoryList.size(); i++) {

                category = categoryList.get(i).getCategory();
                list.add(category);

              simpleItem = new SimpleItem(categoryList.get(i), list, this, this);

                while (i + 1 < categoryList.size()
                        && replacementList.get(i + 1).getTitle().equals(categoryList.get(i).getTitle())) {
                    category = categoryList.get(i+1).getCategory();
                    list.add(category);
                    simpleItem = new SimpleItem(categoryList.get(i), list, this, this);

                    i++;
                }

                items.add(simpleItem);

            }

            this.items = items;
            this.list=list;

            recyclerNewsList.setVisibility(View.VISIBLE);
            adapter.setNewsListAdapter(replacementList);

            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            recyclerNewsList.setLayoutManager(linearLayoutManager);
            recyclerNewsList.setHasFixedSize(true); //Size of RV will not change

            adapter = new ExampleAdapter(items, null);

            adapter.notifyDataSetChanged();
            recyclerNewsList.setAdapter(adapter);


        }
.
4

0 回答 0