Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView postion in list after scrolling

Tags:

android

How can I detect listView item to change or make some actions when it was scrolled above the center of screen?

UPD Solution

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {

        int half = visibleItemCount/2;
        int to = firstVisibleItem+half;

        for (int i=firstVisibleItem; i<to; i++) {
                          // actions for item above center 
            Item item = (Item)adapter.getItem(i);
            item.read = true;
        }
        adapter.notifyDataSetChanged();
    }
like image 797
Gorets Avatar asked Dec 29 '25 20:12

Gorets


1 Answers

You can achieve this using a combination of methods. If your ListView stretches the whole screen (i.e., height is set to MATCH_PARENT), you can determine the center of your screen as the total number of children / 2. Use the getChildCount() method for this job.

As Ayoub said, you can also use a ListView.OnScrollListener to listen for scroll changes in your ListView.

In case your ListView's height does not match the available window screen, you can use this to find the available window height:

 DisplayMetrics metrics = getResources().getDisplayMetrics();
 int windowWidth = metrics.widthPixels;
 int windowHeight = metrics.heightPixels
like image 145
Saket Avatar answered Dec 31 '25 10:12

Saket