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 pull to zoom-in RecyclerView for android

PullZoomRecyclerView Using RecyclerView requires three steps: Step one: use the PullZoomRecyclerView in XML Step two: call the function setAdapter and

dinus_developer 433 Nov 10, 2022
Examples of custom recycler view items. Automatically detecting a dominant color of an image using Picasso and Palette libraries

custom-image-list-item Examples of custom RecyclerView items using Data Binding Features: Loading images urls with the help of a Picasso library Autom

Alina Stepanova 2 Sep 12, 2022
Simple plug and play custom RecyclerView

InfiniteScrollRecyclerView Pros: Simple plug and play custom RecyclerView. Easy to use Built on top of RecyclerView, hence it is performant as it work

Frontier 61 Dec 28, 2022
A Common RecyclerView.Adapter implementation which supports all kind of items and has useful data operating APIs such as remove,add,etc.

##PowerfulRecyclerViewAdapter A Common RecyclerView.Adapter implementation which supports any kind of items and has useful data operating APIs such as

null 313 Nov 12, 2022
Add RecyclerView, use Adapter class and ViewHolder to display data.

فكرة المشروع في هذا المشروع سنقوم بعرض قائمة من البيانات للطلاب على واجهة تطبيق Android بإستخدام: مفهوم RecyclerView مفهوم Adapter مفهوم ViewModel محت

Shaima Alghamdi 3 Nov 18, 2021
ANDROID. ChipsLayoutManager (SpanLayoutManager, FlowLayoutManager). A custom layout manager for RecyclerView which mimicric TextView span behaviour, flow layouts behaviour with support of amazing recyclerView features

ChipsLayoutManager This is ChipsLayoutManager - custom Recycler View's LayoutManager which moves item to the next line when no space left on the curre

Oleg Beloy 3.2k Dec 25, 2022
Custom Layout Manager for Recycler View

Circular Layout Manager Overview A library for Android which essentially contains a Custom Layout Manager for Recycler View which lays out its child v

Kapilesh Iyer 181 Dec 2, 2022
A custom recycler view with shimmer views to indicate that views are loading.

ShimmerRecyclerView Intro A custom recycler view with shimmer views to indicate that views are loading. The recycler view has a built-in adapter to co

Harish Sridharan 3.8k Dec 31, 2022
Carousel Recyclerview let's you create carousel layout with the power of recyclerview by creating custom layout manager.

Carousel Recyclerview let's you create carousel layout with the power of recyclerview by creating custom layout manager.

Jack and phantom 504 Dec 25, 2022
ItemDecorator - Custom item decorator for adding divider for only the first item of a RecyclerView

ItemDecorator Custom item decorator for adding divider for only the first item o

null 1 Apr 1, 2022
RetroDialer - Custom view like a retro telephone dialer

RetroDialer Custom view like a retro telephone dialer Demo

Kshitij Kumar 7 Feb 5, 2022
Using RecyclerView to display data instead of ScrollView or lInearLayout for a strong app. It replaces the ScrollView used in trackMySleep app.

RecyclerView - SleepQualityTracker with RecyclerView app This is the toy app for Lesson 7 of the Android App Development in Kotlin course on Udacity.

Espérant GADA 0 Oct 18, 2021
Sort & Filter Data RecyclerView

Sort-Filter-RecyclerView Sort & Filter Data RecyclerView Tutorial Build with Android Studio

Azhar Rivaldi 12 Dec 9, 2022
[] Super fast and easy way to create header for Android RecyclerView

DEPRECATED I created this library back in the day when I thought RecyclerView was all new and difficult. Writing an adapter that could inflate multipl

Bartek Lipinski 1.3k Dec 28, 2022
Android Library to provide swipe, click and other functionality to RecyclerView

RecyclerViewEnhanced Android Library to provide swipe, click and other functionality to RecyclerView Usage Add this to your build.gradle file dependen

Nikhil Panju 1k Dec 29, 2022
An adapter to create Android RecyclerViews with sections, providing headers and footers.

⚠ This library is no longer maintained ⚠️ SectionedRecyclerView An adapter to create Android RecyclerViews with sections, providing headers and footer

Tomás Ruiz-López 809 Dec 21, 2022
Android library for RecyclerView to manage order of items and multiple view types.

recyclerview-binder Android Library for RecyclerView to manage order of items and multiple view types. Features Insert any items to wherever you want

Satoru Fujiwara 185 Nov 15, 2022
Android Library to provide swipe, click and other functionality to RecyclerView

RecyclerViewEnhanced Android Library to provide swipe, click and other functionality to RecyclerView Usage Add this to your build.gradle file dependen

Nikhil Panju 1k Dec 29, 2022