How to get height of view inside adapter for creating sized bitmap?

偶尔善良 提交于 2019-12-05 15:56:14

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 :)

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!