Android ListView: get data index of visible item

前端 未结 3 959
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 01:37

I have an Android ListView created with a SimpleAdapter that has more items in it than fit in the screen. After the list has been scrolled, I need

相关标签:
3条回答
  • 2020-11-30 02:29

    Simply use the getPositionForView(View) (see documentation). The main advantage of this method is it also works with descendant Views of an item.

    0 讨论(0)
  • 2020-11-30 02:36

    It's very easy. Just use ListView.getFirstVisiblePosition() + indexYouWant. For instance, to get the position in the adapter of the 2nd child displayed in the ListView, just use getFirstVisiblePosition() + 1.

    No need for all the scary stuff shown in the reply above :)

    0 讨论(0)
  • 2020-11-30 02:36
    listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        public void onItemSelected(AdapterView<?> parent,View view, int pos, long id) 
        {
            AisleId= parent.getSelectedItemId();
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });
    

    In this we will get list item Id parent.getSelectedItemId();

    0 讨论(0)
提交回复
热议问题