android-pulltorefresh-and-loadmore

by shontauro

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

449 Stars 196 Forks Last release: Not found 21 Commits 0 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

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

    

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

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 {

    @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 &lt; 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

      

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

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 {

    @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 &lt; 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 {

    @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 &lt; 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

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.