GridLayoutManager(RecyclerView)中的项目在滚动android上更改位置

时间:2016-02-17 08:16:16

标签: android scroll android-recyclerview gridlayoutmanager

我有什么

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

屏幕截图

滚动后

1 个答案:

答案 0 :(得分:4)

这是因为回收机制,在您使用可见性时,您还需要处理每个View中其他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块。