CardView - 向上滚动时会出现神秘的空白

时间:2016-09-05 22:15:01

标签: android android-layout android-recyclerview android-cardview

我终于设法让卡片视图看起来就像我想要的那样。 我使用带有两列的GridLayoutManager来显示带缩略图的卡片。一切都很好,到目前为止工作。

无论如何,一旦我向上滚动,一切都会爆炸。在每一行之间出现大的间隙(卡本身的大小),现在我每隔一行就是一个空的。我不明白这可能会发生什么。向下滚动时我没有任何问题,但即使只有一英寸的备份,一切都是碎片(可以这么说)。

也许有些人可以看到布局有什么问题。

actvivity_main.xml

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:titleTextColor="@android:color/primary_text_dark" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    tools:context=".view.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/series_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:scrollbars="vertical"
        />
</RelativeLayout>

card.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="160dp"
        android:layout_height="270dp"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:elevation="3dp"
        app:cardUseCompatPadding="true"
        app:cardCornerRadius="0dp">

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

            <ImageView
                android:id="@+id/thumbnail"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:clickable="true"
                 />

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/thumbnail"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:paddingTop="10dp"
                android:textColor="@color/colorSeriesTitle"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/rating"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/title"
                android:paddingBottom="5dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textSize="12dp" />

        </RelativeLayout>
    </android.support.v7.widget.CardView>

适配器

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{

private List<Stuff> stuff;
private Callback callback;
private Context context;

public MyAdapter(Context context) {
    this.context = context;
    stuff = Collections.emptyList();
}

public MyAdapter(List<Stuff> stuff) {
    this.stuff = stuff;
}

public void setStuff(List<Stuff> stuff) {
    this.stuff = stuff;
}

public void setCallback(Callback callback) {
    this.callback = callback;
}

public static class MyViewHolder extends RecyclerView.ViewHolder {
    public Stuff stuff;
    public View contentLayout;
    public ImageView thumbnailImageView;
    public TextView titleTextView;
    public TextView ratingTextView;

    public MyViewHolder(View itemView) {
        super(itemView);
        contentLayout = itemView.findViewById(R.id.layout_content);
        thumbnailImageView = (ImageView) itemView.findViewById(R.id.thumbnail);
        titleTextView = (TextView) itemView.findViewById(R.id.title);
        ratingTextView = (TextView) itemView.findViewById(R.id.rating);
    }
}

public interface Callback {
    void onItemClick(Series series);
}

@Override
public SeriesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    final View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.series_card, parent, false);
    final MyViewHolderviewHolder = new MyViewHolder(itemView);
    viewHolder.contentLayout.setOnClickListener(view -> {
        if (callback != null) {
            callback.onItemClick(viewHolder.series);
        }
    });
    return viewHolder;
}

@Override
public void onBindViewHolder(MyViewHolderholder, int position) {
    Stuff stuff = this.stuff.get(position);

    holder.stuff = stuff;

    holder.titleTextView.setText(stuff.getName())
    holder.ratingTextView.setText(stuff.getStatus());

    Picasso.with(context)
            .load(stuff.getSomeImageURL())
            .fit()
            .centerCrop()
            .into(holder.thumbnailImageView);
}



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

1 个答案:

答案 0 :(得分:0)

card.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

将layout_height更改为 wrap_content 修复了问题。