Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to measure the height of the ListView (getHight() = 0)

I can not decide for himself the task will be to ask

I want to measure the height of the ListView. Can not catch the moment of rendering 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();

    }

}

target to calculate the number of items intermeddle ListView screen

like image 972
Max Usanin Avatar asked Aug 03 '12 12:08

Max Usanin


1 Answers

In 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.
      }
    });
like image 74
aleb Avatar answered Nov 15 '22 05:11

aleb