android custom listview,with interaction pattern load more and pull to refresh to load data dinamically

Overview

The first thing that i have to say is render thanks to johannilsson because all the part of pull to refresh listview is based in the code of his repository at https://github.com/johannilsson/android-pulltorefresh.

The target of this project is help other to apply the interaction pattern pull to refresh and load more on an android listview.

#Costum Load more listview for android Screenshot

More information about load more interaction pattern at http://www.androidpatterns.com/uap_pattern/dynamic-loading-of-a-list

#Costum Pull to refresh listview for android Screenshot

More information about pull to refresh interaction pattern at http://www.androidpatterns.com/uap_pattern/pull-to-refresh-2

Repository at https://github.com/shontauro/android-pulltorefresh-and-loadmore.

Usage

Layout for loadmore listview

    <!-- We have to indicate that the listview is now a LoadMoreListView -->

    <com.costum.android.widget.LoadMoreListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

Activity to LoadMoreListView

// set a listener to be invoked when the list reaches the end
		((LoadMoreListView) getListView())
				.setOnLoadMoreListener(new OnLoadMoreListener() {
					public void onLoadMore() {
						// Do the work to load more items at the end of list here
						new LoadDataTask().execute();
					}
				});


private class LoadDataTask extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... params) {

			if (isCancelled()) {
				return null;
			}

			// Simulates a background task
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}

			for (int i = 0; i < mNames.length; i++)
				mListItems.add(mNames[i]);

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			mListItems.add("Added after load more");

			// We need notify the adapter that the data have been changed
			((BaseAdapter) getListAdapter()).notifyDataSetChanged();

			// Call onLoadMoreComplete when the LoadMore task, has finished
			((LoadMoreListView) getListView()).onLoadMoreComplete();

			super.onPostExecute(result);
		}

		@Override
		protected void onCancelled() {
			// Notify the loading more operation has finished
			((LoadMoreListView) getListView()).onLoadMoreComplete();
		}
	}

Layout for pullandload listview

      <!-- We have to indicate that the listview is now a PullAndLoadListView -->

    <com.costum.android.widget.PullAndLoadListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

Activity to PullAndLoadListView

Here we have to pass two listeners one to pull operation and the other to load operation

// Set a listener to be invoked when the list should be refreshed.
		((PullAndLoadListView) getListView())
				.setOnRefreshListener(new OnRefreshListener() {

					public void onRefresh() {
						// Do work to refresh the list here.
						new PullToRefreshDataTask().execute();
					}
				});

		// set a listener to be invoked when the list reaches the end
		((PullAndLoadListView) getListView())
				.setOnLoadMoreListener(new OnLoadMoreListener() {
					
					public void onLoadMore() {
						// Do the work to load more items at the end of list
						// here
						new LoadMoreDataTask().execute();
					}
				});


private class LoadMoreDataTask extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... params) {

			if (isCancelled()) {
				return null;
			}

			// Simulates a background task
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}

			for (int i = 0; i < mNames.length; i++)
				mListItems.add(mNames[i]);

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			mListItems.add("Added after load more");

			// We need notify the adapter that the data have been changed
			((BaseAdapter) getListAdapter()).notifyDataSetChanged();

			// Call onLoadMoreComplete when the LoadMore task, has finished
			((PullAndLoadListView) getListView()).onLoadMoreComplete();

			super.onPostExecute(result);
		}

		@Override
		protected void onCancelled() {
			// Notify the loading more operation has finished
			((PullAndLoadListView) getListView()).onLoadMoreComplete();
		}
	}

private class PullToRefreshDataTask extends AsyncTask<Void, Void, Void> {
	
		@Override
		protected Void doInBackground(Void... params) {

			if (isCancelled()) {
				return null;
			}

			// Simulates a background task
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}

			for (int i = 0; i < mAnimals.length; i++)
				mListItems.addFirst(mAnimals[i]);

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			mListItems.addFirst("Added after pull to refresh");

			// We need notify the adapter that the data have been changed
			((BaseAdapter) getListAdapter()).notifyDataSetChanged();

			// Call onLoadMoreComplete when the LoadMore task, has finished
			((PullAndLoadListView) getListView()).onRefreshComplete();

			super.onPostExecute(result);
		}

		@Override
		protected void onCancelled() {
			// Notify the loading more operation has finished
			((PullAndLoadListView) getListView()).onLoadMoreComplete();
		}
	}

License

Licensed under the Apache License, Version 2.0

Copyright (c) 2012 Fabian Leon

Comments
  • List view Vibrates

    List view Vibrates

    Yea I know thats not a right word to use, but after load more gets completed when I want to go up in the listview it doesn't allow me to go up in one shot it just bounce itself and i have to scroll slowly to get up

    opened by DrAndro 3
  • Removing the OnLoadMoreListener

    Removing the OnLoadMoreListener

    Hello,

    Great lib! I have just a question, after loading all my data, I would like to remove the OnLoadMoreListener of the listview as there are no more items to load.

    Is that possible to achieve that?

    opened by ChristopheVersieux 3
  • Crash at Samsung Galaxy S III and similar

    Crash at Samsung Galaxy S III and similar

    There is a mistake in PullToRefreshListView.java class which makes list unscrollable on Samsung Galaxy SIII and some other phones.

    The problem lies in the following method:

    @Override
    protected void onAttachedToWindow() {
        setSelection(1);
    }
    

    You forgot to add super.onAttachedToWindow() call which leads to the erroneous behavour. Please fix and update.

    opened by ASemeniuk 1
  • This project does not need activity.

    This project does not need activity.

    Hi.

    I would like to use this library. But i found bug.

    Library project setted listview as activity on Manifest file. This project does not need activity. So i deleted application tag. Then i could run sample project and my project.

    I want you to merge my fix.

    Best regards.

    Shunsuke.

    opened by shiratsu 0
  • "Pull to refresh" banner disappears if I scroll to the bottom of the list

    I am able to implement pull to refresh, however sometimes if I navigate to the bottom of the list (or sometimes if I scroll really slowly towards the bottom) and then come back to the top I can no longer pull to refresh, the "pull to refresh" banner doesnt show up even if I pull down.

    opened by Akshat-Agarwal 0
  • listview was not clickable after scroll

    listview was not clickable after scroll

    random records on listview were not clickable after scroll. See: http://stackoverflow.com/questions/12824042/cannot-click-on-item-of-listview-after-scrolling

    opened by SA010 0
  • ScrollView + ListView , will it work ?

    ScrollView + ListView , will it work ?

    if i put listview inside scollview like,

    <ScrollView>
      <RelativeLayout>
         <NestedScrollview>
            <RelativeLayout>
               //some other views are here...
            </RelativeLayout>
         </NestedScrollview>
     
         <ListView>
         </ListView>
     </RelativeLayout>
    </ScrollView>
    
    opened by samirmangroliya 0
  • Error no constructor

    Error no constructor

    11-27 17:43:19.458 7095-7095/br.com.belaapp.cliente E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{br.com.belaapp.cliente/com.costum.android.widget.LoadMoreListView}: java.lang.InstantiationException: can't instantiate class com.costum.android.widget.LoadMoreListView; no empty constructor at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2016) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117) at android.app.ActivityThread.access$700(ActivityThread.java:134) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4867) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.InstantiationException: can't instantiate class com.costum.android.widget.LoadMoreListView; no empty constructor at java.lang.Class.newInstanceImpl(Native Method) at java.lang.Class.newInstance(Class.java:1319) at android.app.Instrumentation.newActivity(Instrumentation.java:1068) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2007)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)             at android.app.ActivityThread.access$700(ActivityThread.java:134)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)             at android.os.Handler.dispatchMessage(Handler.java:99)             at android.os.Looper.loop(Looper.java:137)             at android.app.ActivityThread.main(ActivityThread.java:4867)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:511)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)             at dalvik.system.NativeStart.main(Native Method)

    opened by hellaesir 1
  • Not working with empty data list

    Not working with empty data list

    For the first time the app cannot reach the backend because of network failure, hence the data list for the adapter is empty. The listview is not shown up in the screen so there's no "pull to refresh" nor "tap to refresh" view. As consequence users have no way to refresh the listview to get data again.

    opened by lat233 1
  • BAD listview scroll position

    BAD listview scroll position

    I user your library with both pull to refresh and load more (PullAndLoadListView). I have a listview with base adapter and i use notifydatasetchanged. I make this action 1- scroll with loadmore and the listview remain to the bottom
    2- i scroll with pull to refresh the listview scroll to top , 3- scroll with loadmore but listview always scroll to top.

    why ?

    opened by robertoant 2
Owner
Fabian Leon
CTO at SunDevs.com, Software Engineer, Entrepreneur, I like to learn by sharing!
Fabian Leon
A generic, customizable, open source Android ListView implementation that has 'Pull to Refresh' functionality.

Android 'Pull to Refresh' ListView library Demo video: http://www.youtube.com/watch?v=VjmdELnm3GI Project A generic, customizable, open source Android

Erik Wallentinsen 639 Nov 17, 2022
An Android custom ListView and ScrollView with pull to zoom-in.

PullZoomView An Android custom ListView and ScrollView with pull to zoom-in. Features Set ZoomView enable Add HeaderView Custom ZoomView Parallax or N

Frank-Zhu 2.3k Dec 26, 2022
This project aims to provide a reusable pull to refresh widget for Android.

Pull To Refresh for Android Note This library is deprecated, a swipe refresh layout is available in the v4 support library. This project aims to provi

Johan Berg 2.5k Jan 2, 2023
Phoenix Pull-to-Refresh

Phoenix Pull-to-Refresh This project aims to provide a simple and customizable pull to refresh implementation. Made in [Yalantis] (https://yalantis.co

Yalantis 4k Dec 30, 2022
Android library to display a ListView whose cells are not rigid but flabby and react to ListView scroll.

FlabbyListView This library is not maintained anymore and there will be no further releases Android library to display a ListView which cells are not

JPARDOGO 762 Nov 23, 2022
HorizontalListView is an Android ListView widget which scrolls in a horizontal manner (in contrast with the SDK-provided ListView which scrolls vertically).

HorizontalListView HorizontalListView is an Android ListView widget which scrolls in a horizontal manner (in contrast with the SDK-provided ListView w

MeetMe 722 Nov 10, 2022
Android ListView that implements the QuickReturn UI pattern. Written from scratch with focus on performance.

QuickReturn Android ListView that implements the QuickReturn UI pattern. Written from scratch with focus on performance. Demo Usage In your build.grad

Felipe Lima 191 Nov 25, 2022
Lazy load of images in Android

LazyList A simple library to display images in Android ListView. Images are being downloaded asynchronously in the background. Images are being cached

Fedor Vlasov 1.2k Nov 15, 2022
Parallax ScrollView and ListView for Android

Parallax Scrolls Parallax ListView and ScrollView for Android This project includes ScrollView with one or more parallaxed views ListView with paralla

Nir Hartmann 851 Dec 3, 2022
Bringing extended scrolling features to Android's native ListView and ExpandableListView.

About QuickScroll QuickScroll is a library aimed at creating similar scrolling experiences to the native Contacts app's People view. It has the same s

Andras Kindler 461 Nov 11, 2022
An easy to use Drag & Drop List for Android. Direct replacement of the android ListView.

DragNDropListView DragNDropListView is a direct replacement for the stock Android ListView. If you know how to use ListView, you already know how to u

null 187 Dec 22, 2022
Easy to use ListView with pinned sections for Android.

Easy to use ListView with pinned sections for Android 2.1 and higher. Pinned section is a header view which sticks to the top of the list until at lea

Sergej Shafarenka 2.6k Dec 21, 2022
A ListView with pinned section headers for Android

PinnedHeaderListView This library provides a sectioned ListView with pinned headers. It looks and feels much like the default contacts app does on And

James Smith 665 Nov 29, 2022
Awesome Listview filter functionality in Android.

About Awesome Listview filter functionality in Android. See it in Action: https://www.youtube.com/watch?v=RO54U1ES5CA Listview with beautiful transpar

Bhavya  Mehta 446 Nov 20, 2022
Android ListView with sticky headers

DEPRECATED HeaderListView is deprecated. No new development will be taking place. Quickstart Import the HeaderListView module in your Android Studio p

Applidium 314 Nov 10, 2022
Simple ListView based Android AccordionView

Android Accordion View Example git pull import to eclipse properties->android uncheck is library run as android application See main.xml. Screenshot L

Maciej Lopacinski 164 Nov 28, 2022
AssignmentListView is an Android ListView widget which calculate image loading times.

AssignmentListView is an Android ListView widget which calculate image loading times.

null 5 Sep 9, 2022
[] A swipe menu for ListView.

SwipeMenuListView A swipe menu for ListView. Demo Usage Add dependency dependencies { compile 'com.baoyz.swipemenulistview:library:1.3.0' } Step 1

星一 3.5k Dec 16, 2022
StackExpandableView - A custom view that resembles the iOS notification group behavior

StackExpandableView - A custom view that resembles the iOS notification group behavior

Fabio Sassu 155 Dec 15, 2022