1

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?

instanceMaster
  • 456
  • 3
  • 12

2 Answers2

4

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;
        }
    }
}
Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
  • Thanks Dr.nik! I've found an article here http://codinglines.frankiv.me/post/14552677846/android-implementing-a-dynamically-loading-adapter – instanceMaster Dec 21 '11 at 22:40
-2

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

Buda Gavril
  • 21,409
  • 40
  • 127
  • 196