【问题标题】:RecyclerView extra empty gap between itemsRecyclerView 项目之间的额外空白
【发布时间】:2016-07-27 08:51:01
【问题描述】:

1.我刚刚实现了一些回收视图,他们的一些项目有额外的空白空间。这个空间有时会在滚动后消失。我已经尝试了你能想到的所有方法!我已经改变了高度来包装内容,但仍然存在问题!

这里是截图: Recyclerview empty space

这里是项目 xml:

<android.support.v7.widget.CardView
    android:id="@+id/btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:gravity="center"
    android:orientation="horizontal"
    android:adjustViewBounds="true"
    android:padding="8dp"

    android:layout_margin="6dp"

    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/banner_image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/no_image_banner" />

        <TextView
            android:id="@+id/txtHey"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:gravity="center"
            android:maxLength="20"
            android:text="sadas"
            android:textColor="#4d4a46"
            android:textSize="11dp"

            android:textStyle="bold" />

        <TextView
            android:id="@+id/txtHi"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp"
            android:gravity="center"
            android:text="sadas"
            android:textColor="#999999"
            android:textSize="9dp" />

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center_vertical">

            <TextView
                android:id="@+id/txtPrice"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="sadas"
                android:textColor="#888888"
                android:textSize="9dp" />

            <LinearLayout
                android:id="@+id/price_cancel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="6dp"
                android:background="#888888"
                android:orientation="horizontal" />
        </FrameLayout>

        <TextView
            android:id="@+id/txtFinalPrice"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="sadas"

            android:textColor="#3dc24d"
            android:textSize="9dp" />

        <TextView
            android:id="@+id/txtHello"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="hey"
            android:textSize="9dp"
            android:visibility="gone" />
    </LinearLayout>
</android.support.v7.widget.CardView>

主页xml:

<android.support.design.widget.AppBarLayout
    android:id="@+id/topBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <include
        layout="@layout/toolbar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.design.widget.AppBarLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/product_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="6dp"
        >

    </android.support.v7.widget.RecyclerView>

    <TextView
        android:id="@+id/request_results"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="21dp"
        android:textStyle="bold" />
</RelativeLayout>

  1. 第二个问题是我在嵌套滚动视图中使用了一些回收视图来防止滚动问题。一切都很好,但在像 kitkat 这样的较低版本的 android 中,我有滚动问题(因为嵌套在 sdk

你能帮我解决这些问题吗?

这是适配器类:

private ArrayList<Struct> structs;
OnItemListener onItemListener;
private boolean isGrid;
private int Tab;

public Adapter_Recycler(ArrayList<Struct> structs, OnItemListener onItemListener, int Tab, boolean isGrid) {
    this.structs = structs;
    this.onItemListener = onItemListener;
    this.Tab = Tab;
    this.isGrid = isGrid;
}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = null;


    if (Tab == 2) {
        view = inflater.inflate(R.layout.item_product_color, parent,   false);
    }
    if (Tab == 1 && isGrid == false) {
        view = inflater.inflate(R.layout.item_product_list, parent, false);
    }
    if (Tab == 1 && isGrid) {
        view = inflater.inflate(R.layout.item_product_list, parent, false);
    }

    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    if (Tab == 2) {
        holder.txtHey.setText(structs.get(position).hey);
        holder.txtPrice.setText(structs.get(position).hi);
        holder.txtHi.setVisibility(View.GONE);
        holder.PriceCancel.setVisibility(View.GONE);
        holder.txtFinalPrice.setVisibility(View.GONE);
        holder.txtHey.setTypeface(Activity_Main.SANS);
        holder.txtPrice.setTypeface(Activity_Main.SANS);
        Glide
                .with(Activity_Main.CONTEXT)
                .load(structs.get(position).image)
                .placeholder(R.drawable.background_card)
                .fitCenter()
                .into(holder.banner_image);
    }

    if (Tab == 1) {
        holder.txtHey.setText(structs.get(position).hey);
        holder.txtHi.setText(structs.get(position).hi);
        holder.txtPrice.setText(structs.get(position).price);
        holder.txtFinalPrice.setText(structs.get(position).final_price);
        if (holder.txtFinalPrice.getText().toString() == "") {
            holder.PriceCancel.setVisibility(View.GONE);
        } else {
            holder.PriceCancel.setVisibility(View.VISIBLE);
        }
        holder.txtHey.setTypeface(Activity_Main.SANS);
        holder.txtHi.setTypeface(Activity_Main.SANS);
        holder.txtPrice.setTypeface(Activity_Main.SANS);
        holder.txtFinalPrice.setTypeface(Activity_Main.SANS);
        Glide
                .with(Activity_Main.CONTEXT)
                .load(structs.get(position).image)
                .placeholder(R.drawable.background_card)
                .fitCenter()
                .into(holder.banner_image);
    }
    holder.linearLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (onItemListener != null) {
                onItemListener.onItemSelect(position);
            }
        }
    });

    holder.txtHello.setText(structs.get(position).hello);
    holder.txtHello.setTypeface(Activity_Main.SANS);


}


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


public static class ViewHolder extends RecyclerView.ViewHolder {

    CardView linearLayout;
    ImageView banner_image;
    TextView txtHey;
    TextView txtHi;
    TextView txtHello;
    TextView txtPrice;
    TextView txtFinalPrice;
    LinearLayout PriceCancel;

    public ViewHolder(View itemView) {
        super(itemView);
        linearLayout = (CardView) itemView.findViewById(R.id.btn);
        banner_image = (ImageView) itemView.findViewById(R.id.banner_image);
        txtHey = (TextView) itemView.findViewById(R.id.txtHey);
        txtHi = (TextView) itemView.findViewById(R.id.txtHi);
        txtHello = (TextView) itemView.findViewById(R.id.txtHello);
        txtPrice = (TextView) itemView.findViewById(R.id.txtPrice);
        txtFinalPrice = (TextView) itemView.findViewById(R.id.txtFinalPrice);
        PriceCancel = (LinearLayout) itemView.findViewById(R.id.price_cancel);
    }
}

}

更新: 占位符纵横比不得与图像纵横比相差太大。它们应该是一样的。

【问题讨论】:

  • 我看到您已经为回收站项目添加了 xml,请添加 RecyclerView 的添加位置和适配器。还展示如何初始化RecyclerView,即在哪里设置LayoutManagerAdapter等。并尝试分别说明您面临的问题和可取的结果。
  • 仅供参考:这些属性不适用于cardview android:orientation="horizontal" android:adjustViewBounds="true" android:padding="8dp" android:background="#ffffff"
  • 好的,但这不是问题!我只是将cardview更改为线性布局,反之亦然,因此这些额外的属性就在那里! @k0sh
  • 你的问题是因为 padding 和 margin
  • @PiyushGupta 你确定吗?我到底应该怎么做?

标签: android android-layout android-studio android-recyclerview android-nestedscrollview


【解决方案1】:

如果可能,请提供适配器类,因为 onCreateViewHolder 和 LayoutInflater 可能会出现问题。

【讨论】:

  • 我添加了适配器类
【解决方案2】:

我终于弄清楚问题所在了! 额外的空间是因为
" .placeholder(R.drawable.background_card) " !

删除占位符后问题解决。

当然问题是图像!不是占位符。

【讨论】:

  • 编辑问题是图片和占位符的纵横比不同