Android Update ListView向下滚动

时间:2019-02-04 07:28:48

标签: android listview android-listview onscrolllistener

我想在向下滚动时更新我的​​listView:

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;
        }
    });

但是它不能正确地工作,当我向下滚动时会更新,而当我向上滚动时会更新

我也尝试执行此操作,但它也不能正常工作:

        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();
                }


            }
        });

2 个答案:

答案 0 :(得分:0)

尝试使用此方法向下滚动列表视图并选择最后一行,这样它将  滚动到视图。

 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;
}

});

答案 1 :(得分:0)

您应该尝试这样。

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);
        }
    }
});

使用此方法,您可以检测滚动方向,然后更新列表 希望对您有帮助。