如何计算在运行时ListView项的高度并设置在机器人与同一高度的所有项目? [英] How to calculate the listview Item height at run time and set all items with same height in android?

查看:208
本文介绍了如何计算在运行时ListView项的高度并设置在机器人与同一高度的所有项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我有一个列表视图,我膨胀Web视图到列表视图。我能数据和图像加载到列表视图项。问题是列表项目是不相同的高度,由于不同的内容。我想使具有相同高度的所有列表项

In my application i have a list view and i am inflating a web view into the list view. I am able to load data and images into the list view item. The problem is the list items are not of same height due to different contents. I want to make all list items having same height

如何计算具有最大数据列表项的高度和高度设置为在列表中列表项的其余部分。

How to calculate the height of the list item that has the maximum data and set that height to rest of the list items in the list.

我会怎么做..我有没有线索..说,如果第10个项目有我想要的高度适用于其他地区的最大高度。帮我实现这个请和谢谢您的支持

How would i do that.. i have no clue on that.. Say if the 10th item has the maximum height i want to apply that height to the rest. help me implement this please and thank you for your support

<?xml version="1.0" encoding="utf-8"?>

<WebView
    android:id="@+id/wv1" 
    android:layout_width="fill_parent" android:paddingRight="20dp"
    android:layout_height="wrap_content" />

推荐答案

有标志的数组,说

boolean[] flags=new boolean[szieOfList];

填补这一阵列为false;

fill this array to false;

在有你的活动或适配器变量最大高度,最大说,在onCreate方法0初始化它,现在在getView方法,你在哪里充气XML,编写如下code:

Have a variable max height in your activity or adapter, say max, initialize it with 0 in onCreate method, now in getView method where you are inflating xml, write the following code:

LayoutInflater layoutInflater = (LayoutInflater) getActivity()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view= (WebView) layoutInflater
            .inflate(R.layout.web_view, null);
if(flag[position])
{
      LayoutParams params = view.getLayoutParams();
              params.height = max;
              view.setLayoutParams(params);
}     
ViewTreeObserver observer = view.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        //in here, place the code that requires you to know the dimensions.

         flag[position]=true;
         int height= view.getHeight();
         if(max<height)
         {
              max=height;
              notifyDataSetInvalidated();
         }
          else
         {
               LayoutParams params = view.getLayoutParams();
              params.height = max;
              view.setLayoutParams(params);
         }
         //this will be called as the layout is finished, prior to displaying.
    }
}

这篇关于如何计算在运行时ListView项的高度并设置在机器人与同一高度的所有项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆