drz49609 2019-02-21 10:36
浏览 58

即使用户不这样做,LazyLoad ListView也会触发onScroll

I have a dynamic data fetching VolleyRequest which is called onCreate with LazyLoading attached to the listview. But listview onScroll event is also fired after I fetch few records using the Volley request.

I tried setOnTouchListener of the listview and set a boolean variable but it didn't help.

setOnTouchListener Code

lstViewPlayers.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if(view == lstViewPlayers && motionEvent.getAction() == MotionEvent.ACTION_SCROLL) {
                userScrolled=true;
            }
            return false;
        }
    });

onCreate Code

lstViewPlayers=findViewById(R.id.lstViewPlayers);
customAdapterViewUserTeamNew=new CustomAdapterViewUserTeamNew(ViewUsersTeamLive.this,arr_name,arr_playername);
    lstViewPlayers.setAdapter(customAdapterViewUserTeamNew);
if(new ConnectionDetector(ViewUsersTeamLive.this).is_internet_connected()){
        // Index is required to fetch the next set of items
        int startIndex = customAdapterViewUserTeamNew.getCount();
        getUserTeams(startIndex);
    }
    else{
        Toast.makeText(ViewUsersTeamLive.this,"Please connect to working internet",Toast.LENGTH_SHORT).show();
    }

lstViewPlayers.setOnScrollListener(new LazyLoader() {
        @Override
        public void loadMore(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            // Fetch your data here !!!
            if(new ConnectionDetector(ViewUsersTeamLive.this).is_internet_connected()) {
                if(userScrolled==true) {
                    int startIndex = customAdapterViewUserTeamNew.getCount();
                    getUserTeams(startIndex);
               }
            }
            else{
                Toast.makeText(ViewUsersTeamLive.this,"Please Connect to Working Internet",Toast.LENGTH_SHORT).show();
            }
        }
    });

getUserTeam function is a volley request

LazyLoader class

public abstract class LazyLoader implements AbsListView.OnScrollListener {

private static final int DEFAULT_THRESHOLD = 15 ;

//public static boolean userScrolled=false;

private boolean loading = true  ;
private int previousTotal = 0 ;
private int threshold = DEFAULT_THRESHOLD ;

public LazyLoader() {}

public LazyLoader(int threshold) {
    this.threshold = threshold;
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    /*if(scrollState==AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL){
        userScrolled=true;
    }
    else{
        userScrolled=false;
    }*/
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
                     int visibleItemCount, int totalItemCount) {
    if(loading) {
        if(totalItemCount > previousTotal) {
            // the loading has finished
            loading = false ;
            previousTotal = totalItemCount ;
        }
    }

    // check if the List needs more data
    if(!loading && ((firstVisibleItem + visibleItemCount ) >= (totalItemCount - threshold))) {
        loading = true ;

        // List needs more data. Go fetch !!
        loadMore(view, firstVisibleItem,
                visibleItemCount, totalItemCount);
    }
}

// Called when the user is nearing the end of the ListView
// and the ListView is ready to add more items.
public abstract void loadMore(AbsListView view, int firstVisibleItem,
                              int visibleItemCount, int totalItemCount);

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料