Friday, August 23, 2013

Pull to Refresh ListView in Android.

No comments :

Hi,

Pull to Refresh ListView

It will allow to user pulling down  and releasing from the top of the list.
Then come to basic coding part PulltoRefresh.
Here you can fine some better example for pulltorefresh. https://github.com/chrisbanes/Android-PullToRefresh
Custem ListView


pull_to_refresh_listview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
       <com.pulltorefresh.mad.PullToRefreshView
        xmlns:ptr="http://schemas.android.com/apk/res/com.pulltorefresh.mad"
        android:id="@+id/pull_refresh_list"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        ptr:ptrMode="both"
       
        />
</LinearLayout>


package com.example.pulltorefresh

import java.util.Arrays;
import java.util.LinkedList;

import com.pulltorefresh.mad.PullToRefreshBase.Mode;
import com.pulltorefresh.mad.PullToRefreshBase.OnRefreshListener;

import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class PullToRefreshListActivity extends ListActivity {

static final int MENU_MANUAL_REFRESH = 0;
static final int MENU_DISABLE_SCROLL = 1;
static final int MENU_SET_MODE = 2;

private LinkedList<String> mListItems;
private PullToRefreshView mPullRefreshListView;
private ArrayAdapter<String> mAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pull_to_refresh_listview);

mPullRefreshListView = (PullToRefreshView) findViewById(R.id.pull_refresh_list);

// Set a listener to be invoked when the list should be refreshed.
mPullRefreshListView.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
mPullRefreshListView.setLastUpdatedLabel(DateUtils.formatDateTime(getApplicationContext(),
System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL));

// Do work to refresh the list here.
new GetDataTask().execute();
}
});

ListView actualListView = mPullRefreshListView.getRefreshableView();

mListItems = new LinkedList<String>();
mListItems.addAll(Arrays.asList(mStrings));

mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);

// You can also just use setListAdapter(mAdapter)
actualListView.setAdapter(mAdapter);
}

private class GetDataTask extends AsyncTask<Void, Void, String[]> {

@Override
protected String[] doInBackground(Void... params) {
// Simulates a background job.
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
return mStrings;
}

@Override
protected void onPostExecute(String[] result) {
mListItems.addFirst("Added after refresh..New Version Coming soon J-elly Bean");
mAdapter.notifyDataSetChanged();

// Call onRefreshComplete when the list has been refreshed.
mPullRefreshListView.onRefreshComplete();

super.onPostExecute(result);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, MENU_MANUAL_REFRESH, 0, "Manual Refresh");
menu.add(0, MENU_DISABLE_SCROLL, 1,
mPullRefreshListView.isDisableScrollingWhileRefreshing() ? "Enable Scrolling while Refreshing"
: "Disable Scrolling while Refreshing");
menu.add(0, MENU_SET_MODE, 0,
mPullRefreshListView.getMode() == Mode.BOTH ? "Change to MODE_PULL_DOWN"
: "Change to MODE_PULL_BOTH");
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem disableItem = menu.findItem(MENU_DISABLE_SCROLL);
disableItem
.setTitle(mPullRefreshListView.isDisableScrollingWhileRefreshing() ? "Enable Scrolling while Refreshing"
: "Disable Scrolling while Refreshing");

MenuItem setModeItem = menu.findItem(MENU_SET_MODE);
setModeItem.setTitle(mPullRefreshListView.getMode() == Mode.BOTH ? "Change to MODE_PULL_DOWN"
: "Change to MODE_PULL_BOTH");

return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
case MENU_MANUAL_REFRESH:
new GetDataTask().execute();
mPullRefreshListView.setRefreshing(false);
break;
case MENU_DISABLE_SCROLL:
mPullRefreshListView.setDisableScrollingWhileRefreshing(!mPullRefreshListView
.isDisableScrollingWhileRefreshing());
break;
case MENU_SET_MODE:
mPullRefreshListView
.setMode(mPullRefreshListView.getMode() == Mode.BOTH ? Mode.PULL_DOWN_TO_REFRESH
: Mode.BOTH);
break;
}

return super.onOptionsItemSelected(item);
}

private String[] mStrings = { "A-stro", "B-ender", "C-upcake", "E-clair", "F-royo",
"G-ingerbread", "H-oneycomb", "I-ce Cream Sandwich ","jelly bean" };
}

Screen Shot






No comments :

Post a Comment