【问题标题】:Cells not taking desired height, when using GridLayoutManager with RecyclerView将 GridLayoutManager 与 RecyclerView 一起使用时,单元格没有达到所需的高度
【发布时间】:2018-05-27 11:21:41
【问题描述】:

我创建了一个RecyclerView(在一个片段中),我在其中使用GridLayoutManager 动态添加项目。在RecyclerView 的每个单元格中,我正在使用毕加索从服务器加载图像。 现在的问题是,当第一次加载图像时,RecyclerView 单元格的高度很奇怪,如下所示:

但是当我改变标签并再次来到主页标签时,一切都是我想要的方式,如下所示:

我需要的是,让单元格在添加到回收站视图时具有所需的宽度和高度。为此,我尝试过:

  1. 获取屏幕宽度并设置单元格的高度和宽度=屏幕尺寸/2。
  2. RecyclerView的宽度,使单元格的宽度和高度等于它的一半。
  3. 我在适配器的onCreateViewHolder 方法中为单元格分配尺寸。

但无论如何,我需要更改选项卡(再次加载片段)以使单元格具有所需的高度。

我已在其布局文件中将单元格的宽度和高度指定为换行内容,因为我希望它们根据屏幕大小进行设置。我不想硬编码任何值。

我做错了什么?我错过了什么吗?

详细解释,代码如下:

单元格布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/category_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@android:color/darker_gray"
    android:orientation="vertical"
    android:weightSum="1">

    <ImageView
        android:id="@+id/categoryImage"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginTop="0dp"
        android:layout_weight="0.8"
        android:src="@drawable/icon" />

    <TextView
        android:id="@+id/categoryName"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="0dp"
        android:layout_weight="0.2"
        android:text="ORAL CARE"
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textSize="15dp" />

</LinearLayout> 

片段布局文件中的RecyclerView

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerViewCategories"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView11" />

RecyclerView 适配器:

public class CategoryRecyclerViewAdapter extends RecyclerView.Adapter<CategoryRecyclerViewAdapter.ViewHolder> {
    private List<MedicineCategoryDataModel> categoryData = new ArrayList<MedicineCategoryDataModel>();
    private LayoutInflater layoutInflater;
    private ItemClickListener itemClickListener;
    private Context context;
    private int itemWidth;

    public CategoryRecyclerViewAdapter(Context context, List<MedicineCategoryDataModel> data, int width) {
        this.layoutInflater = LayoutInflater.from(context);
        this.categoryData = data;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.layout_category_item, parent, false);
//        this is the point where I was trying to set cell's width and height. 
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String categoryName = categoryData.get(position).categoryName;
        holder.textViewCategoryName.setText(categoryName);
        String iconUrl = categoryData.get(position).iconUrl;
        EPClientLayer.getInstance().getImageHandler().loadImage(context,iconUrl, holder.imageViewCategoryIcon);
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView textViewCategoryName;
        ImageView imageViewCategoryIcon;

        ViewHolder(View itemView) {
            super(itemView);
            textViewCategoryName = (TextView) itemView.findViewById(R.id.categoryName);
            imageViewCategoryIcon = (ImageView) itemView.findViewById(R.id.categoryImage);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (itemClickListener != null) itemClickListener.onItemClick(view, getAdapterPosition());
        }
    }

    public String getItem(int id) {
        return categoryData.get(id).categoryName;
    }

    public void setClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface ItemClickListener {
        void onItemClick(View view, int position);
    }
}

【问题讨论】:

    标签: java android android-recyclerview picasso gridlayoutmanager


    【解决方案1】:

    我认为您需要将单元格项目的ImageViewheight 设置为wrap_content,而不是在使用weightSum 时提供高度0dp。我想建议您像这样实现您的单元格项目。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/category_item"
        android:layout_width="match_parent"
        android:layout_height="260dp"
        android:background="@android:color/darker_gray">
    
        <ImageView
            android:id="@+id/categoryImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/icon" />
    
        <TextView
            android:id="@+id/categoryName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/categoryImage"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="16dp"
            android:text="ORAL CARE"
            android:textColor="@android:color/black"
            android:textSize="15sp" />
    </RelativeLayout>
    

    RecyclerView 将高度和宽度设置为match_parent

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerViewCategories"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView11" />
    

    您不想硬编码任何高度,这很好。但是,我认为您可能会考虑对单元格项目的根RelativeLayout 的优化高度进行硬编码,以在所有屏幕尺寸下获得所需的行为。

    【讨论】:

      【解决方案2】:

      将 SquareImageView 设为:

      public class SquareImageView extends AppCompatImageView {
      public SquareImageView(Context context) {
          super(context);
      }
      
      public SquareImageView(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
      
      public SquareImageView(Context context, AttributeSet attrs, int defStyleAttr) {
          super(context, attrs, defStyleAttr);
      }
      
      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
          super.onMeasure(widthMeasureSpec, widthMeasureSpec);//replace heightMeasureSpec with widthMeasureSpec in this line 
      }
      }
      

      在 recyclerView 项目中使用上面的 imageView 来使用 Piccasa 加载图像。

      在 xml 中使用 recyclerView 为:

      <android.support.v7.widget.RecyclerView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:spanCount="2"
          tools:listitem="@layout/recycler_view_item"
          app:layoutManager="GridLayoutManager">
       </android.support.v7.widget.RecyclerView>
      

      如果这能解决您的问题,请告诉我

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-20
        • 1970-01-01
        • 1970-01-01
        • 2016-09-27
        相关资源
        最近更新 更多