RecyclerView 项目之间额外的空隙

RecyclerView extra empty gap between items

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

截图如下: 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. 第二个问题是我在嵌套滚动视图中使用了一些回收视图来防止滚动问题。一切都很好,但在 android 的较低版本(如 kitkat)中,我在滚动时遇到问题(因为嵌套在 sdk<21 上不起作用)。我可以使用在两个版本上都适用的普通滚动视图,但普通滚动视图不适用于协调器布局,因此应用栏布局隐藏行为不起作用!

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

这是适配器 class:

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);
    }
}

}

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

如果可能,请提供适配器 class,因为使用 LayoutInflater 的 onCreateViewHolder 可能存在一些问题。

终于知道问题出在哪里了! 额外的 space 是因为
" .placeholder(R.drawable.background_card) " !

删除占位符后问题解决。

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