简体   繁体   中英

Android Update ListView on scrolling down

I want ot update my listView when I scroll down I do this :

mConnectionList.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            int action = event.getAction();
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    if( mConnectionList.getLastVisiblePosition() == connectionList.size()-1 ){
                        getConnections();
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    isCrollingDown.set(false);
                    break;
                default:
            }
            return false;
        }
    });

But it does not work corretlly it update when I scroll down and update when I scroll up

Also I try do this but it doesn not work coretly too :

        mConnectionList.setOnScrollListener(new AbsListView.OnScrollListener() {



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

            }

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

//                Log.e("a0","a " + mConnectionList.getLastVisiblePosition());
//                if(mConnectionList.getLastVisiblePosition() == connectionList.size()-1 ){
                    getConnections();
                }


            }
        });

Try this method for scroll down listview and select the last row so it will scroll into view.

 private void scrollListBottom() {
        myListView.post(new Runnable() {
            @Override
            public void run() {
                listView.setSelection(listAdapter.getCount() - 1);
            }
        });
    }

myListView.setOnTouchListener(new OnTouchListener() 
  @Override
    public boolean onTouch(View v, MotionEvent event) {
     v.getParent().requestDisallowInterceptTouchEvent(true);
     return false;
}

});

you shuld try like this.

scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
    @Override
    public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {                  
        if (scrollY > oldScrollY) {
            Log.e("SCROLL", "Scroll DOWN"+scrollY);
        }
        if (scrollY < oldScrollY) {
            Log.e("SCROLL", "Scroll UP"+scrollY);
        }
        if (scrollY == 0) {
            Log.e("SCROLL", "TOP SCROLL"+scrollY);
        }
        if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
            Log.e("SCROLL", "BOTTOM SCROLL"+scrollY);
        }
    }
});

using this you can detect your scrolling direction and then update your list hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM