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位置,它也会显示(技术上在我的自定义图像后面)

4

3 回答 3

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);
   }
于 2019-03-26T10:36:33.513 回答
0

您的最后一个 if 语句始终为 true

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

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

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

如果您希望图像加载器仅发生在非 0非 2以及非 5非 7 的情况下,则需要 && 条件...

else if's 对你的条件句来说是一个更好的选择。

但是如果你使用 switch 语句,你的代码会更简单、更快、更容易阅读。

switch(position) {
   case 0:
      ...
      break;
   case 2:
      ...
      break;
   case 5:
      ...
      break;
   case 7:
      ...
      break;
   default:
      ...
      break;
}
于 2016-01-22T08:46:01.413 回答
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);
        }
    }
于 2016-01-22T08:48:39.447 回答