GridLayoutManager (RecyclerView) 中的项目在滚动时改变位置 android

items in GridLayoutManager (RecyclerView) changes position on scroll android

我有什么

我有一个 GridLayoutManager (RecyclerView) 位置 0,2,5,7 固定了自定义图像(相对布局转换为位图),其余位置填充了服务器图像

我的问题

当我滚动布局中的项目时,它的位置会发生变化

我的代码

@Override
    public void onBindViewHolder(ProfilePhotosViewHolder ProfilePhotosViewHolder, final int position) {
        model = list.get(position);

        if (position == 0 || position == 2 || position == 5 || position == 7) {
            if (position == 0) {
                ProfilePhotosViewHolder.relativeBucket.setVisibility(View.VISIBLE);
                ProfilePhotosViewHolder.relativeBucket.setDrawingCacheEnabled(true);
                ProfilePhotosViewHolder.relativeBucket.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                ProfilePhotosViewHolder.relativeBucket.layout(0, 0, ProfilePhotosViewHolder.relativeBucket.getMeasuredWidth(), ProfilePhotosViewHolder.relativeBucket.getMeasuredHeight());
                ProfilePhotosViewHolder.relativeBucket.buildDrawingCache(true);
                Bitmap b = Bitmap.createBitmap(ProfilePhotosViewHolder.relativeBucket.getDrawingCache());
                ProfilePhotosViewHolder.relativeBucket.setDrawingCacheEnabled(false);
                ProfilePhotosViewHolder.imgProfilePhotos.setImageBitmap(b);
            }

            if (position == 2) {
                ProfilePhotosViewHolder.relativeFollowers.setVisibility(View.VISIBLE);
                ......

            }

            if (position == 5) {
                ProfilePhotosViewHolder.relativeFollowing.setVisibility(View.VISIBLE);
               ......
            }

            if (position == 7) {
                ProfilePhotosViewHolder.relativePosts.setVisibility(View.VISIBLE);
                ......
            }

        } else {

            ProfilePhotosViewHolder.relativeBucket.setVisibility(View.GONE);
            ProfilePhotosViewHolder.relativeFollowers.setVisibility(View.GONE);
            ProfilePhotosViewHolder.relativeFollowing.setVisibility(View.GONE);
            ProfilePhotosViewHolder.relativePosts.setVisibility(View.GONE);

            ImageLoader imageLoader = AppController.getInstance().getImageLoader();
            ProfilePhotosViewHolder.imgProfilePhotos.setImageUrl(model.ImagesUri, imageLoader);
            ProfilePhotosViewHolder.imgProfilePhotos.setDefaultImageResId(R.drawable.im_profile_monuments);
            ProfilePhotosViewHolder.imgProfilePhotos.setErrorImageResId(R.drawable.im_profile_monuments);
        }

屏幕截图

滚动后

这是因为 回收机制,当你在玩可见性时,你还需要处理每个 View 中其他 View 的可见性=12=]块。

例如位置 2:

if (position == 2) {

    ProfilePhotosViewHolder.relativeFollowers.setVisibility(View.VISIBLE);

    ProfilePhotosViewHolder.relativeBucket.setVisibility(View.GONE);
    ProfilePhotosViewHolder.relativeFollowing.setVisibility(View.GONE);
    ProfilePhotosViewHolder.relativePosts.setVisibility(View.GONE);
}

PS: 我建议你使用 switch(position) 而不是嵌套的 if 块。