0

是)我有的

我有一个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);
        }

截屏

滚动后

4

1 回答 1

4

这是因为回收机制,在玩可见性的同时,还需要处理每个块中其他Views的可见性。if

例如位置 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块。

于 2016-02-17T08:22:23.030 回答