Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ListView get current position in visible items

If the ListView only shows 5 items, and the all data is 40 items. How can I get the current selection is which position in visible items (1~5)? I have tried getSelectedItemPosition but it will return the position in all data. I want the position number in visible items shows on each item.

    class MyAdapter extends BaseAdapter {
        @Override 
        public int getCount(){
         ...
        }
        @Override 
        public Object getItem(int position){
         ...
        }
        @Override    
        public View getView(int position, View convertView, ViewGroup parent)
        {  
           // I want to get the position number in visible items here
        }
    }
like image 667
Andy Avatar asked Oct 07 '15 03:10

Andy


2 Answers

getFirstVisiblePosition() can get first visible position in all items,so you can use getSelectedItemPosition()-getFirstVisiblePosition() to make it.

like image 100
starkshang Avatar answered Nov 04 '22 07:11

starkshang


getFirstVisiblePosition() can get first visible position and getLastVisiblePosition() can get last visible position. So getLastVisiblePosition()-getFirstVisiblePosition() returns the number of visible item.

like image 23
Avijit ballav Avatar answered Nov 04 '22 07:11

Avijit ballav