Implemented Android-PullToRefresh

Project home: https://github.com/chrisbanes/Android-PullToRefresh

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-10-08 01:38:50 -04:00
parent bf1d079155
commit 130a5a19ec
8 changed files with 172 additions and 45 deletions

View File

@@ -0,0 +1,88 @@
package com.handmark.pulltorefresh.extras.listfragment;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
/**
* A sample implementation of how to the PullToRefreshListView with
* ListFragment. This implementation simply replaces the ListView that
* ListFragment creates with a new PullToRefreshListView. This means that
* ListFragment still works 100% (e.g. <code>setListShown(...)</code>).
*
* The new PullToRefreshListView is created in the method
* <code>onCreatePullToRefreshListView()</code>. If you wish to customise the
* PullToRefreshListView then override this method and return your customised
* instance.
*
* @author Chris Banes
*
*/
public abstract class PullToRefreshListFragment extends ListFragment implements OnRefreshListener<ListView> {
private PullToRefreshListView mPullToRefreshListView;
/**
* @return The {@link PullToRefreshListView} attached to this ListFragment.
*/
public final PullToRefreshListView getPullToRefreshListView() {
return mPullToRefreshListView;
}
@Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getPullToRefreshListView().setOnRefreshListener(this);
}
/**
* Returns the {@link PullToRefreshListView} which will replace the ListView
* created from ListFragment. You should override this method if you wish to
* customise the {@link PullToRefreshListView} from the default.
*
* @param inflater
* - LayoutInflater which can be used to inflate from XML.
* @param savedInstanceState
* - Bundle passed through from
* {@link ListFragment#onCreateView(LayoutInflater, ViewGroup, Bundle)
* onCreateView(...)}
* @return The {@link PullToRefreshListView} which will replace the
* ListView.
*/
protected PullToRefreshListView onCreatePullToRefreshListView(final LayoutInflater inflater, final Bundle savedInstanceState) {
return new PullToRefreshListView(getActivity());
}
@Override
public final View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
final View layout = super.onCreateView(inflater, container, savedInstanceState);
final ListView lv = (ListView) layout.findViewById(android.R.id.list);
final ViewGroup parent = (ViewGroup) lv.getParent();
// Iterate through parent's children until we find the ListView, we need
// to do it this way as we need to find out the child index
for (int i = 0, z = parent.getChildCount(); i < z; i++) {
final View child = parent.getChildAt(i);
if (child == lv) {
// Remove the ListView first
parent.removeViewAt(i);
// Now create ListView, and add it in it's place...
mPullToRefreshListView = onCreatePullToRefreshListView(inflater, savedInstanceState);
parent.addView(mPullToRefreshListView, i, lv.getLayoutParams());
break;
}
}
return layout;
}
}