I want to make an endless list on Android.

Asked 2 years ago, Updated 2 years ago, 50 views

I'd like to show you a little bit of the items on the list and let the user scroll and get additional items when they reach the end of the list. What can I do?

list android listview

2022-09-21 18:29

1 Answers

onscrolllistener that implements the way.

public class Test extends ListActivity implements OnScrollListener {

    Aleph0 adapter = new Aleph0();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(adapter); 
        getListView().setOnScrollListener(this);
    }

    public void onScroll(AbsListView view,
        int firstVisible, int visibleCount, int totalCount) {

        Boolean loadMore = /* Whether to put padding */
            firstVisible + visibleCount >= totalCount;

        if(loadMore) {
            adapter.count += visibleCount;
            adapter.notifyDataSetChanged();
        }
    }

    public void onScrollStateChanged(AbsListView v, int s) { }    

    class Aleph0 extends BaseAdapter {
        int count = 40; /* Number of items seen for the first time */

        public int getCount() { return count; }
        public Object getItem(int pos) { return pos; }
        public long getItemId(int pos) { return pos; }

        public View getView(int pos, View v, ViewGroup p) {
                TextView view = new TextView(Test.this);
                view.setText("entry " + pos);
                return view;
        }
    }
}

ListActivity initially shows only 40 items and adds items when the user scrolls to the end. If these items take as long as they take web data, then you have to separate the threads.


2022-09-21 18:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.