2017-07-10 10 views
0

2つの列を持つグリッドレイアウトマネージャを使用してリサイクルビューにアイテムを表示しようとしています。recyclerviewグリッドの最初の行の寸法が正しくありません

ただし、グリッドの最初の行には幅の問題があるようです。最初の行が見えなくなるまで、私はスクロールダウンすると

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

RecyclerViewをホストしているConstraintLayoutに問題があるようですが、それをFrameLayoutに変更して問題が解決しました。レイアウトとRecyclerViewの両方の寸法がmatch_parentに設定されていても、何とか寸法を検出できません。

注:次元を絶対(たとえば100dp)に変更しても問題は解決しますが、レイアウトが応答しなくなります。

関連する問題