我有回收器视图与它的项目,并可以垂直滚动。目前我所实现的是像列表一样一个接一个地添加项目。我需要把它们并排放置。
喜欢下面的图片
,我的输出是
我的回收者-视图设置代码:
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);
}
}
}
发布于 2018-06-05 18:41:24
你可以使用谷歌最新的设计组件ChipGroup来做到这一点
或者你可以通过在网格布局中显示你的标签来使用Flexbox-Layout。
如果您希望使用Flexbox-Layout,请查看answer of avik
发布于 2018-06-05 18:52:21
我可以建议你用一个简单的解决方案,但是,你不能实现与此代码的完整要求。你们会并排在一起。
替换
topicListView.setLayoutManager(new LinearLayoutManager(getActivity()));
使用
topicListView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
// 3 denotes the number of rows per column
发布于 2018-06-05 18:28:41
添加这个
topicListView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL,false));
https://stackoverflow.com/questions/50697791
复制相似问题