代码之家  ›  专栏  ›  技术社区  ›  Jonathon

更改RecycleView项目宽度?

  •  0
  • Jonathon  · 技术社区  · 6 年前

    我知道可能有一个很简单的解决办法,但对我来说,我似乎找不到。如何更改RecycleView项目的宽度?出于某种原因,它显示的是父项的第一个项目的全宽…它在每个项目之间显示了很多空白。

    enter image description here

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/main_wrapper"
            android:background="@color/colorPrimary"
            android:orientation="vertical">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.2"
                android:padding="20dp"
                android:orientation="vertical">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Categories"
                    android:textColor="#ffffff"
                    android:layout_marginBottom="10dp"/>
    
                <android.support.v7.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/cats"
                    ></android.support.v7.widget.RecyclerView>
    
            </LinearLayout>
    </LinearLayout>
    

    类别适配器:

    public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {
    
    private ArrayList<Category> categories;
    
    public CategoryAdapter(ArrayList<Category> categories) {
        this.categories = categories;
    }
    
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_cat, parent, false);
        return new ViewHolder(v);
    }
    
    @Override public void onBindViewHolder(ViewHolder holder, int position) {
        Category cat = this.categories.get(position);
        holder.text.setText(cat.getText().toString());
    }
    
    @Override public int getItemCount() {
        return categories.size();
    }
    
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView text;
    
        public ViewHolder(View itemView) {
            super(itemView);
            text = (TextView) itemView.findViewById(R.id.cat_title);
        }
    }
    

    }

    单个目录.xml

    <TextView
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:text="Cat"
        android:background="@drawable/cat_single_shape"
        android:padding="10dp"
        android:gravity="center"
        android:id="@+id/cat_title"
        android:textSize="13sp"
        android:fontFamily="@font/open_sans_bold"
        android:textColor="@color/colorPrimary"/>
    
    3 回复  |  直到 6 年前
        1
  •  0
  •   th3matr1x    6 年前

    我会把一只猫换成

    <TextView
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@drawable/cat_single_shape"
    android:padding="10dp"
    android:gravity="center"
    android:id="@+id/cat_title"
    android:textSize="13sp"
    android:fontFamily="@font/open_sans_bold"
    android:textColor="@color/colorPrimary"/>
    
        2
  •  0
  •   Ali Reza    6 年前

    1)在你的片段中:

    recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, true));
    

    2)对于recyclerview single item.xml do:

    <?xml version="1.0" encoding="utf-8" ?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="100dp"
    
    android:layout_height="50dp"
    
    android:layout_margin="4dp">
    
    
    <TextView
    
    android:layout_width="match_parent"
    
    android:layout_height="match_parent"
    
    android:gravity="center" />
    
    </LinearLayout>
    

    3)并将RecyclerView更改为:

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/main_wrapper"
            android:background="@color/colorPrimary"
            android:orientation="vertical">
    
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="0dp"
                    android:layout_weight="0.2"
                    android:text="Categories"
                    android:textColor="#ffffff"
                    android:layout_marginBottom="10dp"/>
    
                <android.support.v7.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="0.8"
                    android:id="@+id/cats"/>
    
    </LinearLayout>
    
        3
  •  0
  •   Jonathon    6 年前

    最后,经过两天的浏览,我自己想出来了。

    确保你的 约束布局 包装内容 . 这一直是个问题!