2017-07-10 26 views
0

我正在嘗試使用兩列網格佈局管理器在我的回收站視圖中列出項目。回收站視圖網格第一行上的錯誤維度

但是,網格的第一行似乎有寬度問題。

first row showing different width than the others

當我向下滾動,直到第一行是無形的,然後滾動回來了,它會自我修正,並顯示正確的寬度。

correct arrangement of the grid

我的項目是從themoviedb API檢索,

adapter.setMovieWrapper(body); // body contains data from the API  
recyclerView.setLayoutManager(new GridLayoutManager(this, 2)); 
recyclerView.setAdapter(adapter); 

下面是電網項目的佈局代碼:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:padding="2dp" 
    android:orientation="vertical"> 

    <ImageView 
     android:id="@+id/movie_thumbnail" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="centerInside" 
     android:contentDescription="@string/movie_thumbnail_description"/> 

    <TextView 
     android:background="@drawable/bg_shade" 
     android:id="@+id/movie_title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:textColor="#FFF" 
     android:ellipsize="end" 
     android:padding="8dp"/> 
</FrameLayout> 

下面是適配器代碼:

public class MovieGridAdapter extends RecyclerView.Adapter<MovieGridItemViewHolder> { 
    MovieWrapper movieWrapper; 
    private Context context; 

    public MovieGridAdapter(Context context) { 
     this.context = context; 
    } 

    public MovieWrapper getMovieWrapper() { 
     return movieWrapper; 
    } 

    public void setMovieWrapper(MovieWrapper movieWrapper) { 
     this.movieWrapper = movieWrapper; 
     this.notifyDataSetChanged(); 
    } 

    @Override 
    public MovieGridItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     return new MovieGridItemViewHolder(
       LayoutInflater.from(context) 
         .inflate(R.layout.movie_grid_item, parent, false) 
     ); 
    } 

    @Override 
    public void onBindViewHolder(MovieGridItemViewHolder holder, int position) { 
     Movie movie = movieWrapper.getResults().get(position); 
     Uri uri = Uri.parse("https://image.tmdb.org/t/p/w185"); 
     uri = uri.buildUpon().appendEncodedPath(movie.getPosterPath()).build(); 

     Picasso.with(context) 
       .load(uri) 
       .into(holder.getThumbnail()); 

     holder.getTitle().setText(movie.getTitle()); 
    } 

    @Override 
    public int getItemCount() { 
     if (movieWrapper != null && movieWrapper.getResults() != null) { 
      return movieWrapper.getResults().size(); 
     } 
     return 0; 
    } 
} 

這裏是舉辦回收視圖XML佈局的活動:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.adityapurwa.popularmovies.MainActivity"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/movie_grid" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"></android.support.v7.widget.RecyclerView> 

</android.support.constraint.ConstraintLayout> 

任何人都知道是什麼原因造成這個問題,以及如何解決它? 謝謝!

+0

您可以在您的recyclerView中共享1個物品的佈局? –

+0

我們需要更多的代碼。顯示你的列表項目的佈局,以及適配器的代碼。我確定有問題。 –

+0

@RoShanShan - 我更新了問題以包含更多代碼。 –

回答

0

我的ConstraintLayout承載RecyclerView似乎存在問題,我將其更改爲FrameLayout並且問題消失。儘管佈局和RecyclerView都將其尺寸設置爲match_parent,但它以某種方式無法檢測到尺寸。

注意:將維度更改爲絕對值(例如100dp)也解決了問題,但它使佈局無響應。