Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Implementing progressbar and "loading..." for Endless List like Android Market

Taking inspiration from Android Market, i have implemented a Endless List which loads more data from the server when we reach the end of the List.

Now, i need to implement the progressbar & "Loading.." text as shownalt text

Sample code to take inspiration from would be great.

like image 341
Faheem Kalsekar Avatar asked Jan 12 '11 09:01

Faheem Kalsekar


2 Answers

Here is a solution that also makes it easy to show a loading view in the end of the ListView while it's loading.

You can see the classes here:

https://github.com/CyberEagle/OpenProjects/blob/master/android-projects/widgets/src/main/java/br/com/cybereagle/androidwidgets/helper/ListViewWithLoadingIndicatorHelper.java - Helper to make it possible to use the features without extending from SimpleListViewWithLoadingIndicator.

https://github.com/CyberEagle/OpenProjects/blob/master/android-projects/widgets/src/main/java/br/com/cybereagle/androidwidgets/listener/EndlessScrollListener.java - Listener that starts loading data when the user is about to reach the bottom of the ListView.

https://github.com/CyberEagle/OpenProjects/blob/master/android-projects/widgets/src/main/java/br/com/cybereagle/androidwidgets/view/SimpleListViewWithLoadingIndicator.java - The EndlessListView. You can use this class directly or extend from it.

like image 200
Fernando Camargo Avatar answered Oct 11 '22 15:10

Fernando Camargo


Add an onScrollListener to the ListView. When the user scrolls, check if the ListView is nearing its end. If yes, then fetch more data. As an example :

public abstract class LazyLoader implements AbsListView.OnScrollListener {      private static final int DEFAULT_THRESHOLD = 10 ;      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) {     }      @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); } 

Activity :

public class MainActivity extends AppCompatActivity {          @Override         protected void onCreate(Bundle savedInstanceState) {             super.onCreate(savedInstanceState);              setContentView(R.layout.main_layout);             ListView listView = (ListView) findViewById(R.id.listView);              listView.setOnScrollListener(new LazyLoader() {                 @Override                 public void loadMore(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {                     // Fetch your data here !!!                 }             });         }     } 

You can find the complete implementation at this link

like image 34
Neeraj Avatar answered Oct 11 '22 16:10

Neeraj