gpt4 book ai didi

android - 我想测量 ListView 的高度(getHight() = 0)

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:06:30 24 4
gpt4 key购买 nike

我不能自己决定任务会问

我想测量 ListView 的高度。无法捕捉到渲染 ListView 的时刻 (rssListView.getHight(); = 0 )

public class RSSactivity extends Activity {

public static RssItem selectedRssItem = null;
String feedUrl = "";
ListView rssListView = null;
ArrayList<RssItem> rssItems = new ArrayList<RssItem>();
ArrayAdapter<RssItem> aa = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rssListView = (ListView) findViewById(R.id.rssListView);
aa = new ArrayAdapter<RssItem>(this, R.layout.list_item, rssItems);
rssListView.setAdapter(aa);
feedUrl = rssURLTV.getText().toString();
refressRssList();
rssListView.getWidth(); // = 0 ,
}

private void refressRssList() {

ArrayList<RssItem> newItems = RssItem.getRssItems(feedUrl);
rssItems.clear();
rssItems.addAll(newItems);
aa.notifyDataSetChanged();

}

}

目标是计算 ListView 屏幕中的项目数量

最佳答案

在onCreate中:

rssListView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// If you need this to be called again, then run again addOnGlobalLayoutListener.
mainPanel.getViewTreeObserver().removeGlobalOnLayoutListener(this);
rssListView.getWidth(); // It's available here.
}
});

关于android - 我想测量 ListView 的高度(getHight() = 0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11795862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com