4

I use custom CursorAdapter with custom items. I need height of view to resize Bitmap from assets folder and set this resized bitmap to ImegeView in list item;

 @Override
public void bindView(View view, final Context context, final Cursor cursor) {
    final ViewHolder holder = (ViewHolder) view.getTag();

    final int imgCol = cursor.getColumnIndex(TableOdelice.COLUMN_URL);
    int titleCol = cursor.getColumnIndex(TableOdelice.COLUMN_TITRE);
    final int themeCol = cursor.getColumnIndex(TableOdelice.COLUMN_THEME);

    String tempPath = getPath(cursor.getString(themeCol), cursor.getString(imgCol));
    final String path = tempPath.replace(".", "c.");
    String[] arr = cursor.getString(titleCol).split("\\*");
    holder.title.setText(arr[0]);
    holder.subTitle.setText(arr[1]);

    if (itemHeight > 0) {
        showThumb(itemHeight, holder.img, path);
    } else {
        final ImageView v = holder.mainImage;
        v.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                itemHeight = v.getHeight();
                showThumb(itemHeight, holder.img, path);
            }
        });
    }
}

  private void showThumb(int height, final ImageView iv, final String path) {
    if (thumbs.containsKey(path)) {
        iv.setImageBitmap(thumbs.get(path));
    } else {
        InputStream is = null;
        try {
            is = context.getAssets().open(path);
        } catch (IOException e) {
            Log.d("no file at path", path);
            e.printStackTrace();
        }
        if (is != null) {
                Bitmap btm = btmHelper.scaleToHeight(BitmapFactory.decodeStream(is);, height);
            thumbs.put(path, btm);
            iv.setImageBitmap(btm);

        }
    }

}

For getting view height I use OnGlobalLayoutListener() of view.

But it's very slow ... Any ideas?

Pavel Shysh
  • 196
  • 2
  • 13

3 Answers3

12

I find answer for my question. Using this construction I get a correct width or height of view inside adapter for each view.

final ImageView v = holder.mainImage;
            v.post(new Runnable() {
            @Override
            public void run() {
                itemHeight = v.getHeight();
                Log.d("Height", "" + itemHeight);
            }

        });
    }

Maybe it will help somebody :)

Pavel Shysh
  • 196
  • 2
  • 13
0

Just give it a try may be it will be faster

 v.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            itemHeight = v.getHeight();
            showThumb(itemHeight, holder.img, path);
            v.getViewTreeObserver().removeGlobalOnLayoutListener( ActivityInsatance);
        }
    });
DropAndTrap
  • 1,538
  • 16
  • 24
  • Ali4Android, I need get view height inside adapter and iv.getHeight() return always 0. Thanks for your reply and see this http://stackoverflow.com/a/11169602/1180477 – Pavel Shysh Oct 03 '13 at 11:25
  • 1
    r u getting exact hight of your imageview using v.getViewTreeObserver m – DropAndTrap Oct 03 '13 at 11:29
  • yes v.getViewTreeObserver get correct height, but after some time after view causes onGlobalLayout() method and it's looks ugly – Pavel Shysh Oct 03 '13 at 11:37
0

If you have top or bottom margins, a bug will appear. When you will scroll this listview, a height of each row will increase. In case of v.getViewTreeObserver().addOnGlobalLayoutListener it will grow infinitely. So, my solution follows.

holder.layout.post(new Runnable() {
    @Override
    public void run() {
        int height = holder.layout.getMeasuredHeight(); // Total row height.
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.textView.getLayoutParams();
        holder.textView.setHeight(height - lp.topMargin - lp.bottomMargin);
});

where holder.layout is a link to root RelativeLayout of an adapter's row.

CoolMind
  • 26,736
  • 15
  • 188
  • 224