From 130a5a19ecb527e614f6bfad6b146bd9d863a999 Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Mon, 8 Oct 2012 01:38:50 -0400 Subject: [PATCH] Implemented Android-PullToRefresh Project home: https://github.com/chrisbanes/Android-PullToRefresh Signed-off-by: Ricky Barrette --- Android-PullToRefresh | 1 + OSJ Forum/AndroidManifest.xml | 2 +- OSJ Forum/assets/legal.html | 3 +- OSJ Forum/project.properties | 1 + .../PullToRefreshListFragment.java | 88 +++++++++++++++++++ .../osj/forum/ForumDetailFragment.java | 40 ++++++--- .../osj/forum/ForumListFragment.java | 42 +++++---- .../osj/forum/TopicDetailFragment.java | 40 ++++++--- 8 files changed, 172 insertions(+), 45 deletions(-) create mode 160000 Android-PullToRefresh create mode 100644 OSJ Forum/src/com/handmark/pulltorefresh/extras/listfragment/PullToRefreshListFragment.java diff --git a/Android-PullToRefresh b/Android-PullToRefresh new file mode 160000 index 0000000..a38ed2f --- /dev/null +++ b/Android-PullToRefresh @@ -0,0 +1 @@ +Subproject commit a38ed2fc7ff34785636ee1b3e353ebbf1f446820 diff --git a/OSJ Forum/AndroidManifest.xml b/OSJ Forum/AndroidManifest.xml index 146ba9a..4c30200 100644 --- a/OSJ Forum/AndroidManifest.xml +++ b/OSJ Forum/AndroidManifest.xml @@ -1,5 +1,5 @@ OJS Forum uses the following open source libraries licensed under the Apache Software License 2.0.
diff --git a/OSJ Forum/project.properties b/OSJ Forum/project.properties index 188d381..3865f70 100644 --- a/OSJ Forum/project.properties +++ b/OSJ Forum/project.properties @@ -13,3 +13,4 @@ # Project target. target=android-16 android.library.reference.1=../../exception_handler_library/ExceptionHandlerLib +android.library.reference.2=../../Android-PullToRefresh/library diff --git a/OSJ Forum/src/com/handmark/pulltorefresh/extras/listfragment/PullToRefreshListFragment.java b/OSJ Forum/src/com/handmark/pulltorefresh/extras/listfragment/PullToRefreshListFragment.java new file mode 100644 index 0000000..251eb91 --- /dev/null +++ b/OSJ Forum/src/com/handmark/pulltorefresh/extras/listfragment/PullToRefreshListFragment.java @@ -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. setListShown(...)). + * + * The new PullToRefreshListView is created in the method + * onCreatePullToRefreshListView(). 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 { + + 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; + } + +} \ No newline at end of file diff --git a/OSJ Forum/src/org/RickBarrette/osj/forum/ForumDetailFragment.java b/OSJ Forum/src/org/RickBarrette/osj/forum/ForumDetailFragment.java index 5a9ccf1..8a39554 100644 --- a/OSJ Forum/src/org/RickBarrette/osj/forum/ForumDetailFragment.java +++ b/OSJ Forum/src/org/RickBarrette/osj/forum/ForumDetailFragment.java @@ -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 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(); } }); } diff --git a/OSJ Forum/src/org/RickBarrette/osj/forum/ForumListFragment.java b/OSJ Forum/src/org/RickBarrette/osj/forum/ForumListFragment.java index fa792ef..4d4b04d 100644 --- a/OSJ Forum/src/org/RickBarrette/osj/forum/ForumListFragment.java +++ b/OSJ Forum/src/org/RickBarrette/osj/forum/ForumListFragment.java @@ -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 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); } -} +} \ No newline at end of file diff --git a/OSJ Forum/src/org/RickBarrette/osj/forum/TopicDetailFragment.java b/OSJ Forum/src/org/RickBarrette/osj/forum/TopicDetailFragment.java index b90d452..7c44fd1 100644 --- a/OSJ Forum/src/org/RickBarrette/osj/forum/TopicDetailFragment.java +++ b/OSJ Forum/src/org/RickBarrette/osj/forum/TopicDetailFragment.java @@ -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 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(); } }); }