【问题标题】:RecyclerView GridLayoutManager first row bigger hight than other rowsRecyclerView GridLayoutManager 第一行比其他行高
【发布时间】:2016-01-18 19:59:48
【问题描述】:

我使用的是简单的RecyclerView - GridLayoutManager,第一行比其他行更大。

这是带有RecyclerView的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/single_gallery_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:layout_marginTop="50dp"/>

</RelativeLayout>

这里是gridLayoutManager的创建:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mParentView = inflater.inflate(R.layout.fra_gallery_single, null);

    mRecyclerView = (RecyclerView) mParentView.findViewById(R.id.single_gallery_list);

    mAdapter = new SingleGalleryAdapter(getActivity(), ((MainActivity)getActivity()).getSelectedGalleryData().getPhotos());

    mRecyclerView.setAdapter(mAdapter);
    mRecyclerView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.main_background_color));
    mRecyclerView.addItemDecoration(new AppItemDecorator(3, 7));

    mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));

    return mParentView;
}

这是我的项目布局的样子:

    <ImageView
        android:id="@+id/single_item_img"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/img_gallery_1"/>

</LinearLayout>

我有非常简单的适配器。 这是我的屏幕的图片:

有什么建议为什么我的第一行比其他人大吗?

【问题讨论】:

    标签: android android-recyclerview


    【解决方案1】:

    我知道为什么会这样:

    我正在以这种方式为每个项目加载所需的数据:

    这是我的信息对象。

    public class PhotoItemData {
    
    @Attribute
    private String url;
    
    public int getDrawableId(Context context){
        return context.getResources().getIdentifier(url, "drawable", context.getPackageName());
    }
    }
    

    这是我的适配器代码,我在其中获取每个项目的数据

       @Override
        public void onBindViewHolder(SingleGalleryViewHolder holder, int position) {
            PhotoItemData photoItemData = mData.get(position);
    
     holder.singleItemImg.setImageDrawable(ContextCompat.getDrawable(mContext, photoItemData.getDrawableId(mContext)));
    }
    

    每次我的第一张照片都比其他照片大。 我应该找到一种方法来加载具有适当大小的第一张图片。

    如果我像这样更改代码:

    holder.singleItemImg.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.thumb_gallery_3));
    

    工作正常。

    【讨论】:

    • 我通过添加 ImageView 的精确高度来解决这个问题:
    猜你喜欢
    • 2018-06-03
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多