0

カードビューを最終的に私が望むように見せることができました。 2つの列を持つGridLayoutManagerを使用して、サムネイル付きのカードを表示します。今のところすごくうまくいっています。CardView - 上向きスクロール時に不思議な隙間が出る

とにかく、一度スクロールするとすべてが爆発します。大きな行(カード自体のサイズ)が各行の間に現れます。今度は、2行おきに空白になります。私はそれがどうして起こるのか理解していない。私はスクロールダウン中に何の問題もありませんが、1インチのバックアップでさえ、すべてが断片的になっています。

多分、レイアウトによって何が間違っているかを知る人もいます。

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(); 
} 
+0

あなたのアダプタの 'getView'メソッドでもコードを投稿してください。 –

+0

@ x-codeこのようなメソッドはありません、アダプタ全体が追加されました。 – Zackline

答えて

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"> 

wrap_contentにlayout_heightを変更するには、問題を修正しました。

関連する問題