跳过GridLayoutManager android中的单元格

问题描述 投票:3回答:3

我有一个RecycleViewer(GridLayoutManager),其中0,2,5,7个位置用Custom images (relativeLayout converted to bitmap)固定

我想跳过这些0,2,5,7个位置,因为他们已经有Custom images我想用服务器图像填补其余位置

在onBindViewHolder中

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) {
            ...
        }
        if (position == 5) {
            ...
        }
        if (position == 7) {
            ...
        }

        if (position!=0 || position!= 2 || position!=5 || position!=7){
            ImageLoader imageLoader = AppController.getInstance().getImageLoader();
            ProfilePhotosViewHolder.imgProfilePhotos.setImageUrl(model.Images, imageLoader);
            ProfilePhotosViewHolder.imgProfilePhotos.setDefaultImageResId(R.drawable.im_profile_monuments);
            ProfilePhotosViewHolder.imgProfilePhotos.setErrorImageResId(R.drawable.bt_profile_addphoto);
       }

但服务器图像不会跳过0,2,5,7位置,它也会显示(技术上落后于我的自定义图像)

android android-recyclerview gridlayoutmanager
3个回答
0
投票

我假设您有List引用变量,您从服务器获取的数据填充,而不是设置为Adapter。 (假设您从服务器获得了6个项目)。

所以你的列表中的元素如下所示

list[ 0= "data",1= "data",2= "data",3= "data",4= "data",5= "data",6= "data"]

问题是你的适配器填充gidview与你提供的列表相对应,不会跳过元素,

你可以做的是,在制作List时你可以跳过你希望适配器跳过的位置,这样

list.add(0,null);

list.add(1, "add your data from the server for this position");

list.add(2,null);

list.add(3,"add your data from the server for this position");

list.add(4,"add your data from the server for this position");

list.add(5,null);

list.add(6,"add your data from the server for this position");

list.add(7,null);

list.add(8,"add your data from the server for this position");

并在你的onBindViewHolder做一些更改,当你从列表中得到一个空对象时,你可以为该项设置这些值

ProfilePhotosViewHolder.imgProfilePhotos.setImageUrl(model.Images, imageLoader);
ProfilePhotosViewHolder.imgProfilePhotos.setDefaultImageResId(R.drawable.im_profile_monuments);
ProfilePhotosViewHolder.imgProfilePhotos.setErrorImageResId(R.drawable.bt_profile_addphoto);

更新

    for (int i = 0; i < mDefaultImages.length; i++) {

        photosModel = new ProfilePhotosModel(); 
        photosModel.Images = mDefaultImages[i];
        if (profilePhotosListData.size() == 0 || profilePhotosListData.size() == 1||profilePhotosListData.size() == 4||profilePhotosListData.size() == 6) {
            profilePhotosListData.add(null);
            profilePhotosListData.add(photosModel);
        }else {
            profilePhotosListData.add(photosModel);
        }
    }

0
投票

你的最后一个if语句总是正确的。

以下是您的错误陈述对位置0的作用:

if (position!=0 || position!= 2 || position!=5 || position!=7) { 

如果position!= 0(false)OR position!= 2(true) - 因为语句为true它会执行 - 在这种情况下你不希望它为真,因为如果position为0或者你不想执行2或5或7。

你需要&&条件,如果你想图像加载器只发生不是0而不是2而不是5而不是7 ...

否则,如果你的条件更好的选择。

但是如果使用switch语句,您的代码将更简单,更快速,更容易阅读。

switch(position) {
   case 0:
      ...
      break;
   case 2:
      ...
      break;
   case 5:
      ...
      break;
   case 7:
      ...
      break;
   default:
      ...
      break;
}

0
投票

将您的代码更改为:

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);
    }
    else if (position == 2) {
        ...
    }
    else if (position == 5) {
        ...
    }
    else if (position == 7) {
        ...
    }

    else {
        ImageLoader imageLoader = AppController.getInstance().getImageLoader();
        ProfilePhotosViewHolder.imgProfilePhotos.setImageUrl(model.Images, imageLoader);
        ProfilePhotosViewHolder.imgProfilePhotos.setDefaultImageResId(R.drawable.im_profile_monuments);
        ProfilePhotosViewHolder.imgProfilePhotos.setErrorImageResId(R.drawable.bt_profile_addphoto);
   }
© www.soinside.com 2019 - 2024. All rights reserved.