简体   繁体   中英

android listview setOnScrollListener not working

I'm working BaseAdapter.i have custom ArrayList.i successfully adaptered my custom listview in my listview this is a my baseadapter source

public class TransactionAdapter extends BaseAdapter {
private Context context;
private  List<Transaction> transactionList;

public TransactionAdapter(Context context, List<Transaction> values) {

    this.context=context;
    this.transactionList=values;
}

@Override
public int getCount() {
    return transactionList.size();
}

@Override
public Object getItem(int position) {
    return transactionList.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View v = LayoutInflater.from(context).inflate(R.layout.view_transaction_item, parent, false);

    Transaction transaction = transactionList.get(position);


    if (transaction != null) {
        final TextView transactionName=(TextView)v.findViewById(R.id.u_transaction_name);
        transaction.setAmount(transaction.getAmount().replace("-", ""));
        transactionName.setText(transaction.getName());
        ((TextView) v.findViewById(R.id.u_transaction_status)).setText(transaction.getStatus());

        ((TextView) v.findViewById(R.id.u_transaction_ago)).setText(transaction.getPassedTime());
        TextView date = (TextView) v.findViewById(R.id.u_transaction_date);
        date.setText(UniPAYDateUtils.formatToDayMonthInWordAndYear(transaction.getDate()));

    }
    return v;
}

}

 <ListView
        android:id="@+id/u_dashboard_transactions_list"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:divider="@null"
        />

now i want to wrote setOnScrollListener method and i want to check scroll last position(like load more) but i can't check last position this is a my setOnScrollListener method

 customListview.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            currentScrollState = scrollState;
            if (currentVisibleItemCount > 0 && currentScrollState == SCROLL_STATE_IDLE && totalItemCount == (currentFirstVisibleItem + currentVisibleItemCount)) {

                if (!loadingMore) {
                    loadingMore = true;
                    Log.e("TAGGGGGGGGGGGGGGGGGGGGGGGGGGG","Visible Load More");
                }
            }
        }

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

            currentFirstVisibleItem = firstVisibleItem;
            currentVisibleItemCount = visibleItemCount;


        }
    });

what's is a wrong in my setOnScrollListener method? if anyone knows solution please help me thanks everyone

Update your scroll listener to this:

customListview.setOnScrollListener(new AbsListView.OnScrollListener() { 
    private int visibleThreshold = 5;
    private int currentPage = 0;
    private int previousTotal = 0;
    private boolean loading = true;

 @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        if (loading) {
            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
                currentPage++;
            }
        }
        if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
            // load more items
             loading = true;
        }
    }

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

    }); 

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