如何让项目的高度在ListView? [英] how to get height of the item in a listview?

查看:135
本文介绍了如何让项目的高度在ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是customadapter并重写getView()方法。在这种方法我称之为view.getHeight(),它在列表视图返回0,我想找出高度(像素)的每个项目。并找出它是在屏幕上。所以,如果我有在列表视图中10个项目,才有可能找出屏幕(像素)5号项目的位置,我想找出项目的y。左上角。

  CustomArrayAdapter适配器=新CustomArrayAdapter(这一点,R.layout.rowlayout,mydizi);
    查看标题=(查看)getLayoutInflater()膨胀(R.layout.listview_header_row,NULL);
    lv.addHeaderView(头);
        lv.setAdapter(适配器);
        视图Viewlist产品= lv.getChildAt(5 - lv.getFirstVisiblePosition());
        viewList.getLocationOnScreen(位置);


解决方案

有关在列表mList给定可见行nPosition,你可以通过视图的位置:

  //寻源的位置和目的地账户的ListView行
INT [] =位置{0,0};视图Viewlist产品= mList.getChildAt(nPosition - mList.getFirstVisiblePosition());
viewList.getLocationOnScreen(位置);

但你可能需要从getView(),即填充列表之后。

之外的做到这一点

I am using a customadapter and overriding the getView() method. In this method I call view.getHeight() which returns 0. I would like to find out the height (in pixel) of each item in the listview. And find out where it is on the screen. So If I have 10 items in the list view is it possible to find out where the 5th item position on the screen(in pixel) I would like to find out the y of the item. top left corner.

CustomArrayAdapter adapter = new CustomArrayAdapter(this,R.layout.rowlayout,mydizi);
    View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
    lv.addHeaderView(header);


        lv.setAdapter(adapter);
        View viewList = lv.getChildAt(5 - lv.getFirstVisiblePosition());
        viewList.getLocationOnScreen(location);

解决方案

For a given VISIBLE row nPosition in list mList, you can get the location of the view via:

// find the position of the source and destination accounts ListView rows
int[] location = {0,0};

View viewList = mList.getChildAt(nPosition - mList.getFirstVisiblePosition());
viewList.getLocationOnScreen(location);

but you'll likely need to do this from outside your getView(), i.e. after the list is populated.

这篇关于如何让项目的高度在ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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