【问题标题】:ListView cannot get height. ImageView image is height uncertainListView 无法获得高度。 ImageView 图像高度不确定
【发布时间】:2018-10-09 16:41:39
【问题描述】:

Imageview来自网络图片,他的高度随宽度缩放

图像视图

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
    android:id="@+id/iv_intro_img"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="fitCenter"
    android:src="@drawable/product_top"
    android:divider="@null"
    android:adjustViewBounds="true"/>
</LinearLayout>

列表视图

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="10dp"
        android:background="#FFFFFF">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:id="@+id/lv_intro"
            android:divider="@null"></ListView>
    </LinearLayout>

设置ListView高度

public static void setListViewHeightBasedOnChildren(ListView listView, int basicsHeight,int plusItem) {
    int totalHeight = basicsHeight;
    ListAdapter adapter = listView.getAdapter();

    for (int i = 0; i < adapter.getCount(); i++) {
        View listItem = adapter.getView(i, null, listView);
        listItem.measure(0, 0);
        int itemheight=listItem.getMeasuredHeight();
        Log.e("ItemView"+i, "height" + itemheight);
        totalHeight += itemheight + plusItem;
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
    listView.setLayoutParams(params);
}

但是这个日志一直打印高度为0,

又想了想,加个ImageView监听事件,还是什么都得不到 我仍然无法获得 ImageView 的高度

@Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
    if (convertView == null) {
        convertView = View.inflate(mContext, R.layout.product_item_intro, null);
        iv_intro_img = convertView.findViewById(R.id.iv_intro_img);
    }
    String imgurl = datas.get(position); //+ "_500x500.jpg";
    RequestOptions myOptions = new RequestOptions()
        .fitCenter();

    Glide.with(mContext).load(imgurl).apply(myOptions).into(iv_intro_img);

    iv_intro_img.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int itemtHeight = iv_intro_img.getHeight();
            int itemtHeight2= iv_intro_img.getMeasuredHeight();
            Log.e("itemimageview" + position, "height" + itemtHeight + "," + itemtHeight2);
            iv_intro_img.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });
    return convertView;
}

但还没有。希望大家帮我设置ListView的高度。

【问题讨论】:

    标签: android listview imageview height


    【解决方案1】:

    试试这个它对我有用

    列出您的适配器数据

    lvMedical 是您的列表视图

       int totalHeight = 0;
            for (int i = 0; i < list.size(); i++) {
                View listItem = listAdapter.getView(i, null, lvMedical);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
            ViewGroup.LayoutParams params = lvMedical.getLayoutParams();
            params.height = totalHeight + (lvMedical.getDividerHeight() * (lvMedical.getCount() - 1));
            lvMedical.setLayoutParams(params);
            lvMedical.requestLayout();
    

    【讨论】:

    • 执行该方法时,图片还在加载中,无法获取当前高度。
    • listItem.getMeasuredHeight();它的值始终为 0
    • 使您的 ImageView 高度固定,因为您必须先等待加载所有图片!
    • 如果图片是高度固定的,那就是图片不好,我在加载网络图片,高度不一样,我尝试加载高度后调用图片,但是好像没有什么可使用的。
    • iv_intro_img.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { int itemtHeight = iv_intro_img.getHeight(); int itemtHeight2= iv_intro_img.getMeasuredHeight(); Log.e("itemimageview" + position, "height" + itemtHeight + "," + itemtHeight2); iv_intro_img.getViewTreeObserver().removeOnGlobalLayoutListener(this); } });
    猜你喜欢
    • 2017-04-11
    • 1970-01-01
    • 2015-08-16
    • 2018-10-09
    • 2015-10-04
    • 1970-01-01
    • 2017-08-22
    • 2012-01-03
    • 2012-10-07
    相关资源
    最近更新 更多