Recyclerview 网格第一行尺寸错误

Wrong dimension on recyclerview grid first row

我正在尝试使用具有两列的网格布局管理器在我的回收站视图中列出项目。

但是,网格的第一行似乎存在宽度问题。

当我向下滚动直到第一行不可见,然后向上滚动时,它会自行修复并显示正确的宽度。

我的项目是从 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 布局的 activity:

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

有人知道导致此问题的原因以及如何解决吗? 谢谢!

我的 ConstraintLayout 托管 RecyclerView 似乎有问题,我将其更改为 FrameLayout,问题消失了。即使布局和 RecyclerView 都将其尺寸设置为 match_parent,但它无法检测到尺寸。

注意:将尺寸更改为绝对尺寸(例如 100dp)也解决了问题,但它会使布局无响应。

我在 Recyclerview 周围使用了 FrameLayout 但没有用,我使用了这样的变通解决方案,对于前 2 个项目,我添加了虚拟项目并隐藏了视图,这对我有用, 虽然这不是一个完美的解决方案