I have been using RecycleView with an horizontalScrollView to create this(don't mind the blue ball)
发生的事情取决于标签的文字大小,之后的所有其他标签会在它们之间产生巨大差距,下面我留下了一张图片来向您展示正在发生的事情。
这里我有来自回收视图的xml文件。
<HorizontalScrollView
android:id="@+id/searchNeighborhoodsHorizontalScroll"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_below="@id/searchNeighborhoodsRelative"
android:layout_marginTop="10dp"
android:visibility="visible">
<android.support.v7.widget.RecyclerView
android:id="@+id/searchNeighborhoodsTagView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:visibility="visible" />
</HorizontalScrollView>
以下是回收项目中的xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_marginLeft="5dp"
android:background="@drawable/tag_background">
<TextView
android:id="@+id/tagText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="Text test"
android:textColor="#FFFFFF" />
<ImageButton
android:id="@+id/tagDeleteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="20dp"
android:layout_toRightOf="@id/tagText"
android:background="@android:color/transparent"
android:src="@drawable/ic_tag_close" />
设置适配器
GridLayoutManager manager = new GridLayoutManager(getActivity(), tagList.size(), GridLayoutManager.VERTICAL, false);
RecyclerView tagView = (RecyclerView) v.findViewById(R.id.searchNeighborhoodsTagView);
tagView.setLayoutManager(manager);
tagView.setAdapter(adapter);
和适配器
public TagsAdapter(Context context, ArrayList<String> items) {
this.context = context;
this.items = items;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.tag_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.setupCard(items, position, context);
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public long getItemId(int position) {
return 0;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private final TextView tagText;
private final ImageButton tagDeleteButton;
public ViewHolder(View itemView) {
super(itemView);
tagText = (TextView) itemView.findViewById(R.id.tagText);
tagDeleteButton = (ImageButton) itemView.findViewById(R.id.tagDeleteButton);
setFont();
}
private void setFont() {
tagText.setTypeface(LarSingleton.exoMedium);
}
public void setupCard(ArrayList<String> items, int position, Context context) {
tagText.setText(items.get(position).toString());
}
}
你们对如何解决这个问题有任何想法吗?或者为什么会这样?
答案 0 :(得分:0)
RecyclerView.LayoutManager layoutManager = new
GridLayoutManager(getApplicationContext(), 2,GridLayoutManager.HORIZONTAL, false);
建议你可以在不使用水平滚动视图的情况下实现它,因为recyclerview有自己的滚动