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

@@ -1,5 +1,5 @@
<manifest package="org.RickBarrette.osj.forum"
android:versionCode="364"
android:versionCode="1230"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto">
<uses-sdk

View File

@@ -37,7 +37,8 @@
<strong>OJS Forum</strong> uses the following open source libraries licensed under the <a href="#apachelicense">Apache Software License 2.0</a>.
<ul>
<li> <a href="http://code.google.com/p/android-xmlrpc/">android-xmlrpc</a> - Very thin xmlrpc client library for Android platform
<li> <a href="http://code.google.com/p/android-xmlrpc/">android-xmlrpc</a> - Very thin xmlrpc client library for Android platform</li>
<li> <a href="https://github.com/chrisbanes/Android-PullToRefresh"/>Android-PullToRefresh</a> - A project to provide a reusable Pull to Refresh widget for Android.</li>
</ul>
<hr/>

View File

@@ -13,3 +13,4 @@
# Project target.
target=android-16
android.library.reference.1=../../exception_handler_library/ExceptionHandlerLib
android.library.reference.2=../../Android-PullToRefresh/library

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;
}
}

View File

@@ -30,22 +30,25 @@ import org.xmlrpc.android.XMLRPCClient;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import com.handmark.pulltorefresh.extras.listfragment.PullToRefreshListFragment;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
/**
* This fragment will be used to display information about the forum
*
* @author ricky barrette
*/
public class ForumDetailFragment extends ListFragment implements DatabaseListener {
public class ForumDetailFragment extends PullToRefreshListFragment implements DatabaseListener {
public static final String ARG_ITEM_ID = "item_id";
private int mActivatedPosition = ListView.INVALID_POSITION;
private static final String STATE_ACTIVATED_POSITION = "activated_position";
private static final String TAG = "ForumDetailFragment";
ForumItem mItem;
private OnItemSelectedListener mCallbacks;
@@ -59,18 +62,15 @@ public class ForumDetailFragment extends ListFragment implements DatabaseListene
super.onActivityCreated(savedInstanceState);
mDb = new ForumDatabase(getActivity(), this);
new Thread(new Runnable() {
@Override
public void run() {
mDb.saveTopics(XMLRPCClient.getClient(getActivity()), mItem.content.get("forum_id"), false);
}
}).start();
refreshList();
if (TopicContent.ITEMS.size() == 0) {
TopicContent.getTopics((String) mItem.content.get("forum_id"), mDb);
if (TopicContent.ITEMS.size() != 0)
setListAdapter(new TopicAdapter(getActivity()));
}
if (TopicContent.ITEMS.size() == 0)
new Thread(new Runnable() {
@Override
public void run() {
mDb.saveTopics(XMLRPCClient.getClient(getActivity()), mItem.content.get("forum_id"), false);
}
}).start();
}
/**
@@ -125,7 +125,18 @@ public class ForumDetailFragment extends ListFragment implements DatabaseListene
@Override
public void onListItemClick(final ListView l, final View v, final int position, final long id) {
super.onListItemClick(l, v, position, id);
mCallbacks.onItemSelected(this, TopicContent.ITEMS.get(position).id);
Log.v(TAG, "onListItemClick");
mCallbacks.onItemSelected(this, TopicContent.ITEMS.get(position - 1).id);
}
@Override
public void onRefresh(final PullToRefreshBase<ListView> refreshView) {
new Thread(new Runnable() {
@Override
public void run() {
mDb.saveTopics(XMLRPCClient.getClient(getActivity()), mItem.content.get("forum_id"), false);
}
}).start();
}
@Override
@@ -163,6 +174,7 @@ public class ForumDetailFragment extends ListFragment implements DatabaseListene
public void run() {
TopicContent.getTopics((String) mItem.content.get("forum_id"), mDb);
setListAdapter(new TopicAdapter(getActivity()));
getPullToRefreshListView().onRefreshComplete();
}
});
}

View File

@@ -28,21 +28,24 @@ import org.RickBarrette.osj.forum.database.ForumDatabase;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import com.handmark.pulltorefresh.extras.listfragment.PullToRefreshListFragment;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
/**
* This fragment will be used to display a list of forums to the user
*
* @author ricky barrette
*/
public class ForumListFragment extends ListFragment implements DatabaseListener {
public class ForumListFragment extends PullToRefreshListFragment implements DatabaseListener {
private ForumDatabase mDb;
private static final String STATE_ACTIVATED_POSITION = "activated_position";
private static final String TAG = "ForumListFragment";
private OnItemSelectedListener mCallbacks = sForumCallbacks;
private int mActivatedPosition = ListView.INVALID_POSITION;
@@ -61,18 +64,15 @@ public class ForumListFragment extends ListFragment implements DatabaseListener
super.onActivityCreated(savedInstanceState);
mDb = new ForumDatabase(getActivity(), this);
new Thread(new Runnable() {
@Override
public void run() {
mDb.saveForums(false);
}
}).start();
refreshList();
if (ForumContent.ITEMS.size() == 0) {
ForumContent.getForum(mDb);
if (ForumContent.ITEMS.size() != 0)
setListAdapter(new ForumAdapter(getActivity()));
}
if (ForumContent.ITEMS.size() == 0)
new Thread(new Runnable() {
@Override
public void run() {
mDb.saveForums(false);
}
}).start();
}
@Override
@@ -116,7 +116,18 @@ public class ForumListFragment extends ListFragment implements DatabaseListener
@Override
public void onListItemClick(final ListView listView, final View view, final int position, final long id) {
super.onListItemClick(listView, view, position, id);
mCallbacks.onItemSelected(this, ForumContent.ITEMS.get(position).id);
Log.v(TAG, "onListItemClick");
mCallbacks.onItemSelected(this, ForumContent.ITEMS.get(position - 1).id);
}
@Override
public void onRefresh(final PullToRefreshBase<ListView> refreshView) {
new Thread(new Runnable() {
@Override
public void run() {
mDb.saveForums(false);
}
}).start();
}
@Override
@@ -154,6 +165,7 @@ public class ForumListFragment extends ListFragment implements DatabaseListener
public void run() {
ForumContent.getForum(mDb);
setListAdapter(new ForumAdapter(getActivity()));
getPullToRefreshListView().onRefreshComplete();
}
});
}
@@ -170,4 +182,4 @@ public class ForumListFragment extends ListFragment implements DatabaseListener
public void setActivateOnItemClick(final boolean activateOnItemClick) {
getListView().setChoiceMode(activateOnItemClick ? AbsListView.CHOICE_MODE_SINGLE : AbsListView.CHOICE_MODE_NONE);
}
}
}

View File

@@ -30,17 +30,20 @@ import org.xmlrpc.android.XMLRPCClient;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
public class TopicDetailFragment extends ListFragment implements DatabaseListener {
import com.handmark.pulltorefresh.extras.listfragment.PullToRefreshListFragment;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
public class TopicDetailFragment extends PullToRefreshListFragment implements DatabaseListener {
public static final String ARG_ITEM_ID = "item_id";
private int mActivatedPosition = ListView.INVALID_POSITION;
private static final String STATE_ACTIVATED_POSITION = "activated_position";
private static final String TAG = "TopicDetailFragment";
TopicItem mItem;
private OnItemSelectedListener mCallbacks;
@@ -54,18 +57,15 @@ public class TopicDetailFragment extends ListFragment implements DatabaseListene
super.onActivityCreated(savedInstanceState);
mDb = new ForumDatabase(getActivity(), this);
new Thread(new Runnable() {
@Override
public void run() {
mDb.saveThreads(XMLRPCClient.getClient(getActivity()), mItem.content.get("forum_id"), mItem.content.get("topic_id"));
}
}).start();
refreshList();
if (ThreadContent.ITEMS.size() == 0) {
ThreadContent.getThread((String) mItem.content.get("topic_id"), mDb);
if (ThreadContent.ITEMS.size() != 0)
setListAdapter(new ThreadAdapter(getActivity()));
}
if (ThreadContent.ITEMS.size() == 0)
new Thread(new Runnable() {
@Override
public void run() {
mDb.saveThreads(XMLRPCClient.getClient(getActivity()), mItem.content.get("forum_id"), mItem.content.get("topic_id"));
}
}).start();
}
/**
@@ -123,7 +123,18 @@ public class TopicDetailFragment extends ListFragment implements DatabaseListene
@Override
public void onListItemClick(final ListView l, final View v, final int position, final long id) {
super.onListItemClick(l, v, position, id);
mCallbacks.onItemSelected(this, TopicContent.ITEMS.get(position).id);
Log.v(TAG, "onListItemClick");
mCallbacks.onItemSelected(this, TopicContent.ITEMS.get(position - 1).id);
}
@Override
public void onRefresh(final PullToRefreshBase<ListView> refreshView) {
new Thread(new Runnable() {
@Override
public void run() {
mDb.saveThreads(XMLRPCClient.getClient(getActivity()), mItem.content.get("forum_id"), mItem.content.get("topic_id"));
}
}).start();
}
@Override
@@ -161,6 +172,7 @@ public class TopicDetailFragment extends ListFragment implements DatabaseListene
public void run() {
ThreadContent.getThread((String) mItem.content.get("topic_id"), mDb);
setListAdapter(new ThreadAdapter(getActivity()));
getPullToRefreshListView().onRefreshComplete();
}
});
}