instanceMaster
instanceMaster

Reputation: 491

Android: List view that loads data dynamically based on scroll position

I need to create the list view that loads data dynamically based on scroll position. Like in Facebook when user scrolls to the end it dynamically adds more rows. Is there standard solution it?

Upvotes: 1

Views: 4468

Answers (2)

Nikunj Patel
Nikunj Patel

Reputation: 22066

TRY this ::

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 = /* maybe add a padding */
            firstVisible + visibleCount >= totalCount;

        if(loadMore) {
            adapter.count += visibleCount; // or any other amount
            adapter.notifyDataSetChanged();
        }
    }

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

    class Aleph0 extends BaseAdapter {
        int count = 40; /* starting amount */

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

Upvotes: 4

Buda Gavril
Buda Gavril

Reputation: 21637

This is the default behavior for listView. It loads views in items only when items are visible.

Upvotes: -2

Related Questions