【问题标题】:Android Recyclerview item side by sideAndroid Recyclerview 项目并排
【发布时间】:2018-11-14 19:19:02
【问题描述】:

我有recycler-view,里面有项目,可以垂直滚动。目前我实现的是项目像列表一样一个接一个地添加。我需要将它们并排放置。

喜欢下面的图片

我的输出是

我的回收站视图设置代码:

topicAdapter = new TopicAdapter(topicList, getActivity());
        topicListView.setLayoutManager(new LinearLayoutManager(getActivity()));
        topicListView.setAdapter(topicAdapter);

适配器代码为:

public class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.CategoryViewHolder> {
    private List<Topic> topicList;
    Context context;

    public TopicAdapter(List<Topic> topicList, Context context) {
        this.topicList = topicList;
        this.context = context;
    }

    @Override
    public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //inflate the layout file
        View groceryProductView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_suggested_topics, parent, false);
        CategoryViewHolder holder = new CategoryViewHolder(groceryProductView);
        return holder;
    }

    @Override
    public void onBindViewHolder(CategoryViewHolder holder, final int position) {
        holder.txtview.setText(topicList.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return topicList.size();
    }

    public class CategoryViewHolder extends RecyclerView.ViewHolder {
        TextView txtview;

        public CategoryViewHolder(View view) {
            super(view);
            txtview = view.findViewById(R.id.titleView);
        }
    }
}

【问题讨论】:

标签: android android-recyclerview recyclerview-layout


【解决方案1】:

我可以建议您提供一个简单的解决方案,但是您无法使用此代码实现完整的要求。你们会肩并肩的。

替换

topicListView.setLayoutManager(new LinearLayoutManager(getActivity()));

topicListView.setLayoutManager(new GridLayoutManager(getActivity(), 3)); 

// 3 denotes the number of rows per column

【讨论】:

    【解决方案2】:

    您可以使用 Google 的最新设计组件 ChipGroup 来做到这一点
    否则,您可以通过在网格布局中显示您的标签来使用Flexbox-Layout

    如果您想使用 Flexbox-Layout,请查看answer of avik

    【讨论】:

    • 我通过使用 flexbox 实现了这一点。但任何想法我怎样才能使元素居中?这是我当前的输出:imgur.com/5oGHiMW
    • 是的,就像JustifyContent.CENTER 一样flexboxLayoutManager.setJustifyContent(JustifyContent.CENTER);
    • 但您可能还需要将layout_gravity 属性设置为您的RecyclerView android:layout_gravity="center"
    【解决方案3】:

    添加这个

      topicListView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL,false));
    

    【讨论】:

    • 这并不能解决他的问题,这将使它们水平但在单个文件中。他希望它出现在多条线上
    【解决方案4】:

    使用 StaggeredGridLayoutManager 进行 recyclerview

    【讨论】:

      【解决方案5】:

      我认为一个很好的方法是使用Material Choice Chips,您可以学习如何使用它们here。然后,您可以使用 ChipGroup 对它们进行分组并允许它们跨多行流动。

      但是,要解决您手头的问题,您可以使用GridLayoutManager,然后提供SpanSizeLookup

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-13
        相关资源
        最近更新 更多