我怎样才能prevent图像从这个转变,当我点击其他屏幕元素的Andr​​oid的ListView?当我、图像、元素、屏幕

2023-09-06 22:09:24 作者:情到深处竟是离别

我做了早些时候有关此主题的另一篇文章,但我既然改变了我的code了一下周围的基础上建议,但存在同样的问题。我的图片一直围绕转变如果我点击屏幕上的其他元素。

下面是我的code,我这个叫:

新thumbnailer的(image_main,image_table).execute(图像);

image_main是我的ImageView和image_table是持有该表。

 私有类thumbnailer的扩展的AsyncTask<弦乐,太虚,位图> {      私人ImageView的ImageView的;      私人TableLayout imageTable;        公共thumbnailer的(ImageView的ImageView的,TableLayout imageTable){            this.imageView = ImageView的;            this.imageTable = imageTable;        }  @覆盖  保护无效onPostExecute(位图结果){      imageView.setImageBitmap(结果);               imageView.setVisibility(View.VISIBLE);               imageTable.setVisibility(View.VISIBLE);  }  @覆盖      保护无效onProgressUpdate(无效...进度){      }  @覆盖  保护位图doInBackground(字符串... PARAMS){         BitmapFactory.Options O =新BitmapFactory.Options();          o.inJustDe codeBounds = TRUE;          BitmapFactory.de codeFILE(PARAMS [0],O);          最终诠释REQUIRED_SIZE = 70;          //找到正确的比例值。它应该是2的幂。          INT width_tmp = o.outWidth,height_tmp = o.outHeight;          int标= 1;          而(真){              如果(width_tmp / 2'; REQUIRED_SIZE || height_tmp / 2版; REQUIRED_SIZE)                  打破;              width_tmp / = 2;              height_tmp / = 2;              规模++;          }          //德code。与inSampleSize          BitmapFactory.Options O2 =新BitmapFactory.Options();          o2.inSampleSize =规模;          返回BitmapFactory.de codeFILE(PARAMS [0],O2);  } } 

解决方案

听起来像是你要通过视图回收咬伤。也就是说,当你在滚动Android的一个列表,它不会创建一个新的行,而是重用屏幕​​外采取了一行。在这种情况下,可能导致在多个任务试图将图像加载到单个ImageView的。您有效地需要以某种方式能够告诉它加载请求是最近一个行并取消比前者/忽略他们的结果,或只是使用为您处理此(像的 机器人福 )。

您也可以直接关掉你的适配器观点再循环(这是一个使用在 getView 法),但要注意这会使列表滚动缓慢而紧张不安,尤其是在旧设备。

I made another post earlier about this subject but I've since changed my code around a bit based on suggestions but the same problem exists. My images keep shifting around if I click other elements on screen.

Here is my code which I call by this:

new Thumbnailer(image_main, image_table).execute(image);

image_main is my imageView and image_table is the table that holds it.

 private class Thumbnailer extends AsyncTask<String, Void, Bitmap> {
      private ImageView imageView;
      private TableLayout imageTable;

        public Thumbnailer(ImageView imageView, TableLayout imageTable) {
            this.imageView = imageView;
            this.imageTable = imageTable;
        }

  @Override
  protected void onPostExecute(Bitmap result) {
      imageView.setImageBitmap(result);
               imageView.setVisibility(View.VISIBLE);
               imageTable.setVisibility(View.VISIBLE);
  }
  @Override
      protected void onProgressUpdate(Void... progress) {
      }

  @Override
  protected Bitmap doInBackground(String... params) {
         BitmapFactory.Options o = new BitmapFactory.Options();
          o.inJustDecodeBounds = true;
          BitmapFactory.decodeFile(params[0], o);
          final int REQUIRED_SIZE=70;

          //Find the correct scale value. It should be the power of 2.
          int width_tmp=o.outWidth, height_tmp=o.outHeight;
          int scale=1;
          while(true){
              if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                  break;
              width_tmp/=2;
              height_tmp/=2;
              scale++;
          }
          //Decode with inSampleSize
          BitmapFactory.Options o2 = new BitmapFactory.Options();
          o2.inSampleSize=scale;       
          return BitmapFactory.decodeFile(params[0], o2);
  }
 }

解决方案

Sounds like you're getting bitten by view recycling. That is, when you scroll a List in Android, it doesn't create a new row and instead reuses a row taken offscreen. In this case, that can result in multiple tasks trying to load an image into a single imageview. You effectively need to somehow be able to tell which load request is the most recent for a row and cancel the earlier ones/ignore their results, or just use a library that handles this for you (like droid-fu).

You could also just turn off view recycling in your Adapter (that's the part that uses the convertView argument in the getView method), but be aware this will make list scrolling slow and jittery, especially on older devices.