diff --git a/ParseListLoaderExample/AndroidManifest.xml b/ParseListLoaderExample/AndroidManifest.xml
new file mode 100644
index 0000000..3f05666
--- /dev/null
+++ b/ParseListLoaderExample/AndroidManifest.xml
@@ -0,0 +1,40 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/ParseListLoaderExample/build.gradle b/ParseListLoaderExample/build.gradle
new file mode 100644
index 0000000..fb5b762
--- /dev/null
+++ b/ParseListLoaderExample/build.gradle
@@ -0,0 +1,28 @@
+apply plugin: 'com.android.application'
+
+dependencies {
+ compile 'com.android.support:recyclerview-v7:21.+'
+ compile project(':ParseLoginUI')
+ compile rootProject.ext.androidSupport
+ compile files(rootProject.ext.parsePath)
+}
+
+
+android {
+ compileSdkVersion rootProject.ext.compileSdkVersion
+ buildToolsVersion rootProject.ext.buildToolsVersion
+
+ defaultConfig {
+ minSdkVersion rootProject.ext.minSdkVersion
+ targetSdkVersion rootProject.ext.targetSdkVersion
+ }
+
+ sourceSets {
+ main {
+ manifest.srcFile 'AndroidManifest.xml'
+ java.srcDirs = ['src']
+ res.srcDirs = ['res']
+ }
+ }
+}
+
diff --git a/ParseListLoaderExample/res/drawable-xxhdpi/ic_launcher.png b/ParseListLoaderExample/res/drawable-xxhdpi/ic_launcher.png
new file mode 100644
index 0000000..324e72c
Binary files /dev/null and b/ParseListLoaderExample/res/drawable-xxhdpi/ic_launcher.png differ
diff --git a/ParseListLoaderExample/res/layout/activity_list_view.xml b/ParseListLoaderExample/res/layout/activity_list_view.xml
new file mode 100644
index 0000000..64af1ec
--- /dev/null
+++ b/ParseListLoaderExample/res/layout/activity_list_view.xml
@@ -0,0 +1,11 @@
+
+
+
+
\ No newline at end of file
diff --git a/ParseListLoaderExample/res/layout/activity_main.xml b/ParseListLoaderExample/res/layout/activity_main.xml
new file mode 100644
index 0000000..ded4c24
--- /dev/null
+++ b/ParseListLoaderExample/res/layout/activity_main.xml
@@ -0,0 +1,40 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/ParseListLoaderExample/res/layout/activity_recycler_view.xml b/ParseListLoaderExample/res/layout/activity_recycler_view.xml
new file mode 100644
index 0000000..4b9af6c
--- /dev/null
+++ b/ParseListLoaderExample/res/layout/activity_recycler_view.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/ParseListLoaderExample/res/layout/activity_view_pager.xml b/ParseListLoaderExample/res/layout/activity_view_pager.xml
new file mode 100644
index 0000000..c2809f4
--- /dev/null
+++ b/ParseListLoaderExample/res/layout/activity_view_pager.xml
@@ -0,0 +1,11 @@
+
+
+
+
\ No newline at end of file
diff --git a/ParseListLoaderExample/res/layout/item_layout.xml b/ParseListLoaderExample/res/layout/item_layout.xml
new file mode 100644
index 0000000..ccab963
--- /dev/null
+++ b/ParseListLoaderExample/res/layout/item_layout.xml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/ParseListLoaderExample/res/layout/item_layout_2.xml b/ParseListLoaderExample/res/layout/item_layout_2.xml
new file mode 100644
index 0000000..1e910fc
--- /dev/null
+++ b/ParseListLoaderExample/res/layout/item_layout_2.xml
@@ -0,0 +1,19 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/ParseListLoaderExample/res/layout/item_waiting.xml b/ParseListLoaderExample/res/layout/item_waiting.xml
new file mode 100644
index 0000000..bd75d1c
--- /dev/null
+++ b/ParseListLoaderExample/res/layout/item_waiting.xml
@@ -0,0 +1,15 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/ParseListLoaderExample/res/values/strings.xml b/ParseListLoaderExample/res/values/strings.xml
new file mode 100644
index 0000000..e093e21
--- /dev/null
+++ b/ParseListLoaderExample/res/values/strings.xml
@@ -0,0 +1,5 @@
+
+
+ YOUR_PARSE_APP_ID
+ YOUR_PARSE_CLIENT_KEY
+
diff --git a/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/ListLoaderApplication.java b/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/ListLoaderApplication.java
new file mode 100644
index 0000000..332ccf3
--- /dev/null
+++ b/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/ListLoaderApplication.java
@@ -0,0 +1,21 @@
+package com.example.tuanchauict.parselistloaderexample;
+
+import android.app.Application;
+
+import com.parse.Parse;
+import com.parse.ParseObject;
+
+/**
+ * Created by tuanchauict on 10/25/15.
+ */
+public class ListLoaderApplication extends Application {
+ @Override
+ public void onCreate() {
+ super.onCreate();
+
+ // Required - Initialize the Parse SDK
+ Parse.initialize(this);
+ ParseObject.registerSubclass(ParseDemoObject.class);
+
+ }
+}
diff --git a/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/MainActivity.java b/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/MainActivity.java
new file mode 100644
index 0000000..c88eae3
--- /dev/null
+++ b/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/MainActivity.java
@@ -0,0 +1,62 @@
+package com.example.tuanchauict.parselistloaderexample;
+
+import android.app.Activity;
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.View;
+import android.widget.Toast;
+
+import com.example.tuanchauict.parselistloaderexample.listview.ListViewDemoActivity;
+import com.example.tuanchauict.parselistloaderexample.recycler.RecyclerViewDemoActivity;
+import com.example.tuanchauict.parselistloaderexample.viewpager.ViewPagerDemoActivity;
+
+
+public class MainActivity extends Activity {
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_main);
+
+ findViewById(R.id.create_1000_objects).setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ create1000Objects();
+ }
+ });
+
+
+ findViewById(R.id.goto_list_view).setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ Intent intent = new Intent(MainActivity.this, ListViewDemoActivity.class);
+ startActivity(intent);
+ }
+ });
+
+ findViewById(R.id.goto_viewpager).setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ Intent intent = new Intent(MainActivity.this, ViewPagerDemoActivity.class);
+ startActivity(intent);
+ }
+ });
+
+ findViewById(R.id.goto_recycler_view).setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ Intent intent = new Intent(MainActivity.this, RecyclerViewDemoActivity.class);
+ startActivity(intent);
+ }
+ });
+ }
+
+ public void create1000Objects() {
+
+ for (int i = 0; i < 1000; i++) {
+ ParseDemoObject object = ParseDemoObject.getObject(i);
+ object.saveEventually();
+ }
+ Toast.makeText(this, "Check on your app's data", Toast.LENGTH_LONG).show();
+ }
+}
diff --git a/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/ParseDemoObject.java b/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/ParseDemoObject.java
new file mode 100644
index 0000000..754f9e2
--- /dev/null
+++ b/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/ParseDemoObject.java
@@ -0,0 +1,39 @@
+package com.example.tuanchauict.parselistloaderexample;
+
+import com.parse.ParseClassName;
+import com.parse.ParseObject;
+
+/**
+ * Created by tuanchauict on 10/25/15.
+ */
+@ParseClassName("ListLoaderItem")
+public class ParseDemoObject extends ParseObject {
+ public static final String ATTR_SSD = "ssd";
+ public static final String ATTR_FIRST_NAME = "firstName";
+ public static final String ATTR_LAST_NAME = "lastName";
+
+
+ public static ParseDemoObject getObject(int ssd){
+ ParseDemoObject object = new ParseDemoObject();
+ object.put(ATTR_SSD, ssd);
+ object.put(ATTR_FIRST_NAME, "FirstName " + ssd);
+ object.put(ATTR_LAST_NAME, "LastName " + ssd);
+ return object;
+ }
+
+
+ public int getSSD(){
+ return getInt(ATTR_SSD);
+ }
+
+ public String getFirstname(){
+ return getString(ATTR_FIRST_NAME);
+ }
+
+
+ public String getLastName(){
+ return getString(ATTR_LAST_NAME);
+ }
+
+
+}
diff --git a/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/listview/ListViewAdapter.java b/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/listview/ListViewAdapter.java
new file mode 100644
index 0000000..6296115
--- /dev/null
+++ b/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/listview/ListViewAdapter.java
@@ -0,0 +1,79 @@
+package com.example.tuanchauict.parselistloaderexample.listview;
+
+import android.content.Context;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.TextView;
+
+import com.example.tuanchauict.parselistloaderexample.ParseDemoObject;
+import com.example.tuanchauict.parselistloaderexample.R;
+import com.parse.ParseListLoader;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Created by tuanchauict on 10/25/15.
+ */
+public class ListViewAdapter extends ArrayAdapter
+ implements ParseListLoader.LoaderTarget {
+ private List mObjects;
+ private boolean mHasNextPage;
+
+ private LayoutInflater mInflater;
+
+ public ListViewAdapter(Context context) {
+ super(context, 0);
+ mInflater = LayoutInflater.from(context);
+ mObjects = new ArrayList<>();
+ mHasNextPage = true;
+ }
+
+ public boolean hasNextPage() {
+ return mHasNextPage;
+ }
+
+ public void setHasNextPage(boolean hasNextPage) {
+ mHasNextPage = hasNextPage;
+ }
+
+ @Override
+ public int getCount() {
+ return mObjects.isEmpty() ? 0 : mHasNextPage ? mObjects.size() + 1 : mObjects.size();
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ if(position >= mObjects.size()){
+ return mInflater.inflate(R.layout.item_waiting, parent, false);
+ }
+
+ if (convertView == null || convertView.getTag() != Boolean.TRUE) {
+ convertView = mInflater.inflate(R.layout.item_layout, parent, false);
+ convertView.setTag(Boolean.TRUE);
+ }
+ ParseDemoObject item = mObjects.get(position);
+
+ ((TextView) convertView.findViewById(R.id.txt_name))
+ .setText(item.getFirstname() + " " + item.getLastName());
+
+ return convertView;
+ }
+
+ @Override
+ public void appendList(List sublist) {
+ mObjects.addAll(sublist);
+ }
+
+ @Override
+ public void clearList() {
+ mObjects.clear();
+ }
+
+ @Override
+ public void notifyDataChanged() {
+ notifyDataSetChanged();
+ }
+}
diff --git a/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/listview/ListViewDemoActivity.java b/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/listview/ListViewDemoActivity.java
new file mode 100644
index 0000000..8786753
--- /dev/null
+++ b/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/listview/ListViewDemoActivity.java
@@ -0,0 +1,84 @@
+package com.example.tuanchauict.parselistloaderexample.listview;
+
+import android.os.Bundle;
+import android.support.v4.app.FragmentActivity;
+import android.view.MenuItem;
+import android.widget.AbsListView;
+import android.widget.ListView;
+
+import com.example.tuanchauict.parselistloaderexample.ParseDemoObject;
+import com.example.tuanchauict.parselistloaderexample.R;
+import com.parse.ParseException;
+import com.parse.ParseListLoader;
+import com.parse.ParseQuery;
+
+import java.util.List;
+
+/**
+ * Created by tuanchauict on 10/25/15.
+ */
+public class ListViewDemoActivity extends FragmentActivity
+ implements ParseListLoader.OnQueryLoadListener{
+
+
+ private ListView mListView;
+ private ListViewAdapter mAdapter;
+ private ParseListLoader mLoader;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_list_view);
+
+ getActionBar().setDisplayHomeAsUpEnabled(true);
+
+ mListView = (ListView) findViewById(R.id.list_view);
+ mAdapter = new ListViewAdapter(this);
+ mLoader = new ParseListLoader<>(mAdapter,
+ new ParseListLoader.QueryFactory() {
+ @Override
+ public ParseQuery create() {
+ ParseQuery query = ParseQuery.getQuery(ParseDemoObject.class);
+ query.addAscendingOrder(ParseDemoObject.ATTR_SSD);
+ return query;
+ }
+ });
+ mLoader.loadObjects();
+
+ mListView.setAdapter(mAdapter);
+
+ mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
+ @Override
+ public void onScrollStateChanged(AbsListView view, int scrollState) {
+
+ }
+
+ @Override
+ public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
+ int totalItemCount) {
+ if(firstVisibleItem + visibleItemCount > totalItemCount -5
+ && mAdapter.hasNextPage()){
+ mLoader.loadNextPage();
+ }
+ }
+ });
+ }
+
+ @Override
+ public boolean onMenuItemSelected(int featureId, MenuItem item) {
+ if(item.getItemId() == android.R.id.home){
+ onBackPressed();
+ }
+ return super.onMenuItemSelected(featureId, item);
+ }
+
+ @Override
+ public void onLoading() {
+
+ }
+
+ @Override
+ public void onLoaded(List list, boolean hasNextPage, ParseException e) {
+ mAdapter.setHasNextPage(hasNextPage);
+ }
+}
diff --git a/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/recycler/RecyclerAdapter.java b/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/recycler/RecyclerAdapter.java
new file mode 100644
index 0000000..d87d34f
--- /dev/null
+++ b/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/recycler/RecyclerAdapter.java
@@ -0,0 +1,94 @@
+package com.example.tuanchauict.parselistloaderexample.recycler;
+
+import android.content.Context;
+import android.support.v7.widget.RecyclerView;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.TextView;
+
+import com.example.tuanchauict.parselistloaderexample.ParseDemoObject;
+import com.example.tuanchauict.parselistloaderexample.R;
+import com.parse.ParseListLoader;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Created by tuanchauict on 10/26/15.
+ */
+public class RecyclerAdapter extends RecyclerView.Adapter implements ParseListLoader.LoaderTarget {
+
+ static class Holder extends RecyclerView.ViewHolder {
+ View mView;
+ public Holder(View itemView) {
+ super(itemView);
+ mView = itemView;
+ }
+ }
+
+ private List mObjects;
+ private boolean mHasNextPage;
+ private LayoutInflater mInflater;
+
+ public RecyclerAdapter(Context context) {
+ mInflater = LayoutInflater.from(context);
+
+ mObjects = new ArrayList<>();
+ mHasNextPage = true;
+ }
+
+ @Override
+ public int getItemViewType(int position) {
+ return mObjects.size() <= position ? 1 : 0;
+ }
+
+ @Override
+ public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
+ if(viewType == 0){
+ return new Holder(mInflater.inflate(R.layout.item_layout, parent, false));
+ }
+ else{
+ return new Holder(mInflater.inflate(R.layout.item_waiting, parent, false));
+ }
+ }
+
+ @Override
+ public void onBindViewHolder(Holder holder, int position) {
+ if(getItemViewType(position) > 0){
+ return;
+ }
+
+ ParseDemoObject object = mObjects.get(position);
+ TextView txt = (TextView) holder.mView.findViewById(R.id.txt_name);
+ txt.setText(object.getFirstname() + " " + object.getLastName());
+ }
+
+ @Override
+ public int getItemCount() {
+ return mObjects.isEmpty() ? 0 : mHasNextPage ? mObjects.size() + 1 : mObjects.size();
+ }
+
+ @Override
+ public void appendList(List sublist) {
+ mObjects.addAll(sublist);
+ }
+
+ @Override
+ public void clearList() {
+ mObjects.clear();
+ }
+
+ @Override
+ public void notifyDataChanged() {
+ notifyDataSetChanged();
+ }
+
+ public boolean hasNextPage() {
+ return mHasNextPage;
+ }
+
+ public void setHasNextPage(boolean hasNextPage) {
+ mHasNextPage = hasNextPage;
+ }
+}
diff --git a/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/recycler/RecyclerViewDemoActivity.java b/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/recycler/RecyclerViewDemoActivity.java
new file mode 100644
index 0000000..3252d06
--- /dev/null
+++ b/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/recycler/RecyclerViewDemoActivity.java
@@ -0,0 +1,89 @@
+package com.example.tuanchauict.parselistloaderexample.recycler;
+
+import android.os.Bundle;
+import android.support.v4.app.FragmentActivity;
+import android.support.v7.widget.LinearLayoutManager;
+import android.support.v7.widget.RecyclerView;
+import android.view.MenuItem;
+
+import com.example.tuanchauict.parselistloaderexample.ParseDemoObject;
+import com.example.tuanchauict.parselistloaderexample.R;
+import com.parse.ParseException;
+import com.parse.ParseListLoader;
+import com.parse.ParseQuery;
+
+import java.util.List;
+
+/**
+ * Created by tuanchauict on 10/26/15.
+ */
+public class RecyclerViewDemoActivity extends FragmentActivity {
+
+ RecyclerView mRecyclerView;
+ RecyclerAdapter mAdapter;
+
+ ParseListLoader mLoader;
+
+ LinearLayoutManager mLayoutManager;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_recycler_view);
+ getActionBar().setDisplayHomeAsUpEnabled(true);
+ mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
+
+ mAdapter = new RecyclerAdapter(this);
+ mLoader = new ParseListLoader<>(mAdapter, new ParseListLoader.QueryFactory() {
+ @Override
+ public ParseQuery create() {
+ ParseQuery query = ParseQuery.getQuery(ParseDemoObject.class);
+ query.addAscendingOrder(ParseDemoObject.ATTR_SSD);
+ return query;
+ }
+ });
+
+ mLayoutManager = new LinearLayoutManager(this);
+
+ mRecyclerView.setAdapter(mAdapter);
+ mRecyclerView.setLayoutManager(mLayoutManager);
+ mLoader.loadObjects();
+
+ mLoader.addOnQueryLoadListener(new ParseListLoader.OnQueryLoadListener() {
+ @Override
+ public void onLoading() {
+
+ }
+
+ @Override
+ public void onLoaded(List list, boolean hasNextPage, ParseException e) {
+ mAdapter.setHasNextPage(hasNextPage);
+ }
+ });
+
+ mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
+ @Override
+ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
+ super.onScrolled(recyclerView, dx, dy);
+
+ int pastVisibleItems, visibleItemCount, totalItemCount;
+
+ visibleItemCount = mLayoutManager.getChildCount();
+ totalItemCount = mLayoutManager.getItemCount();
+ pastVisibleItems = mLayoutManager.findFirstVisibleItemPosition();
+
+ if(mAdapter.hasNextPage() && visibleItemCount + pastVisibleItems > totalItemCount - 5){
+ mLoader.loadNextPage();
+ }
+ }
+ });
+ }
+
+ @Override
+ public boolean onMenuItemSelected(int featureId, MenuItem item) {
+ if(item.getItemId() == android.R.id.home){
+ onBackPressed();
+ }
+ return super.onMenuItemSelected(featureId, item);
+ }
+}
diff --git a/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/viewpager/ViewPagerAdapter.java b/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/viewpager/ViewPagerAdapter.java
new file mode 100644
index 0000000..bfa19c1
--- /dev/null
+++ b/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/viewpager/ViewPagerAdapter.java
@@ -0,0 +1,99 @@
+package com.example.tuanchauict.parselistloaderexample.viewpager;
+
+import android.content.Context;
+import android.support.v4.view.PagerAdapter;
+import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.TextView;
+
+import com.example.tuanchauict.parselistloaderexample.ParseDemoObject;
+import com.example.tuanchauict.parselistloaderexample.R;
+import com.parse.ParseListLoader;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Created by tuanchauict on 10/25/15.
+ */
+public class ViewPagerAdapter extends PagerAdapter
+ implements ParseListLoader.LoaderTarget {
+ private List mObjects;
+ private boolean mHasNextPage;
+
+ private List mPool;
+
+ LayoutInflater mInflater;
+
+ public ViewPagerAdapter(Context context) {
+ mInflater = LayoutInflater.from(context);
+ mObjects = new ArrayList<>();
+ mHasNextPage = true;
+
+ mPool = new ArrayList<>();
+ }
+
+ @Override
+ public int getCount() {
+ return mObjects.isEmpty() ? 0 : mHasNextPage ? mObjects.size() + 1 : mObjects.size();
+ }
+
+ @Override
+ public boolean isViewFromObject(View view, Object object) {
+ return view == object;
+ }
+
+ @Override
+ public Object instantiateItem(ViewGroup container, int position) {
+ if (position >= mObjects.size()) {
+ return mInflater.inflate(R.layout.item_waiting, container, true);
+ }
+
+ View view;
+ if (mPool.isEmpty()) {
+ view = mInflater.inflate(R.layout.item_layout_2, container, false);
+ } else {
+ view = mPool.remove(0);
+ }
+
+ ParseDemoObject object = mObjects.get(position);
+
+ TextView txt = (TextView) view.findViewById(R.id.txt_name);
+ txt.setText(object.getFirstname() + " " + object.getLastName());
+ container.addView(view);
+ return view;
+ }
+
+ @Override
+ public void destroyItem(ViewGroup container, int position, Object object) {
+ View view = (View) object;
+ container.removeView(view);
+ if (position < mObjects.size())
+ mPool.add((View) object);
+ }
+
+ public boolean hasNextPage() {
+ return mHasNextPage;
+ }
+
+ public void setHasNextPage(boolean hasNextPage) {
+ mHasNextPage = hasNextPage;
+ }
+
+ @Override
+ public void appendList(List sublist) {
+ mObjects.addAll(sublist);
+ }
+
+ @Override
+ public void clearList() {
+ mObjects.clear();
+ }
+
+ @Override
+ public void notifyDataChanged() {
+ notifyDataSetChanged();
+ }
+}
diff --git a/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/viewpager/ViewPagerDemoActivity.java b/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/viewpager/ViewPagerDemoActivity.java
new file mode 100644
index 0000000..0b5794d
--- /dev/null
+++ b/ParseListLoaderExample/src/com/example/tuanchauict/parselistloaderexample/viewpager/ViewPagerDemoActivity.java
@@ -0,0 +1,84 @@
+package com.example.tuanchauict.parselistloaderexample.viewpager;
+
+import android.os.Bundle;
+import android.support.v4.app.FragmentActivity;
+import android.support.v4.view.ViewPager;
+import android.util.Log;
+import android.view.MenuItem;
+
+import com.example.tuanchauict.parselistloaderexample.ParseDemoObject;
+import com.example.tuanchauict.parselistloaderexample.R;
+import com.parse.ParseException;
+import com.parse.ParseListLoader;
+import com.parse.ParseQuery;
+
+import java.util.List;
+
+/**
+ * Created by tuanchauict on 10/25/15.
+ */
+public class ViewPagerDemoActivity extends FragmentActivity {
+ ViewPager mViewPager;
+ ViewPagerAdapter mAdapter;
+ ParseListLoader mLoader;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_view_pager);
+ getActionBar().setDisplayHomeAsUpEnabled(true);
+
+ mViewPager = (ViewPager) findViewById(R.id.view_pager);
+ mAdapter = new ViewPagerAdapter(this);
+ mLoader = new ParseListLoader<>(mAdapter, new ParseListLoader.QueryFactory() {
+ @Override
+ public ParseQuery create() {
+ ParseQuery query = ParseQuery.getQuery(ParseDemoObject.class);
+ query.addAscendingOrder(ParseDemoObject.ATTR_SSD);
+ return query;
+ }
+ });
+ mViewPager.setAdapter(mAdapter);
+ mLoader.loadObjects();
+
+
+ mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
+ @Override
+ public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
+
+ }
+
+ @Override
+ public void onPageSelected(int position) {
+ if(position + 5 > mAdapter.getCount() && mAdapter.hasNextPage()){
+ mLoader.loadNextPage();
+ }
+ }
+
+ @Override
+ public void onPageScrollStateChanged(int state) {
+
+ }
+ });
+
+ mLoader.addOnQueryLoadListener(new ParseListLoader.OnQueryLoadListener() {
+ @Override
+ public void onLoading() {
+
+ }
+
+ @Override
+ public void onLoaded(List list, boolean hasNextPage, ParseException e) {
+ mAdapter.setHasNextPage(hasNextPage);
+ }
+ });
+ }
+
+ @Override
+ public boolean onMenuItemSelected(int featureId, MenuItem item) {
+ if(item.getItemId() == android.R.id.home){
+ onBackPressed();
+ }
+ return super.onMenuItemSelected(featureId, item);
+ }
+}
diff --git a/ParseLoginUI/src/com/parse/ParseListLoader.java b/ParseLoginUI/src/com/parse/ParseListLoader.java
new file mode 100644
index 0000000..7dad847
--- /dev/null
+++ b/ParseLoginUI/src/com/parse/ParseListLoader.java
@@ -0,0 +1,187 @@
+package com.parse;
+
+import android.support.annotation.NonNull;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import bolts.Capture;
+
+public class ParseListLoader {
+ private List> mObjectPages;
+ private int mCurrentPage;
+ private boolean mHasNextPage;
+ private LoaderTarget mTarget;
+ private QueryFactory mQueryFactory;
+ private List> mOnQueryLoadListeners;
+ private int mObjectsPerPage;
+ private List mRunningQueries;
+
+
+ public ParseListLoader(@NonNull LoaderTarget target, @NonNull QueryFactory factory) {
+ mTarget = target;
+ mQueryFactory = factory;
+ mObjectPages = new ArrayList<>();
+ mCurrentPage = -1; // init -1 so that we don't care about call loadObjects or loadNext first
+ mHasNextPage = true;
+ mOnQueryLoadListeners = new ArrayList<>();
+ mObjectsPerPage = 25;
+ mRunningQueries = new ArrayList<>();
+ }
+
+ public void setObjectsPerPage(int objectsPerPage) {
+ mObjectsPerPage = objectsPerPage;
+ }
+
+ public int getObjectsPerPage() {
+ return mObjectsPerPage;
+ }
+
+ public void addOnQueryLoadListener(OnQueryLoadListener onQueryLoadListener) {
+ mOnQueryLoadListeners.add(onQueryLoadListener);
+ }
+
+ public void clearOnQueryLoadListeners() {
+ mOnQueryLoadListeners.clear();
+ }
+
+ public boolean hasOnQueryLoadListeners() {
+ return !mOnQueryLoadListeners.isEmpty();
+ }
+
+ public boolean hasNextPage() {
+ return mHasNextPage;
+ }
+
+ public void loadObjects() {
+ this.loadObjects(0, true);
+ }
+
+ public void loadNextPage() {
+ loadObjects(mCurrentPage + 1, false);
+ }
+
+ private void loadObjects(final int page, final boolean shouldClear) {
+
+ if (mQueryFactory == null) {
+ return;
+ }
+ final ParseQuery query = mQueryFactory.create();
+ if (query == null) {
+ return;
+ }
+
+ mRunningQueries.add(query);
+
+ notifyOnLoadingListeners();
+ if (page >= mObjectPages.size()) {
+ for (int i = mObjectPages.size(); i <= page; i++) {
+ mObjectPages.add(new ArrayList());
+ }
+ }
+ setPageOnQuery(page, query);
+
+ final int objectsPerPage = mObjectsPerPage;
+
+ final Capture firstCallback = new Capture<>(Boolean.TRUE);
+ query.findInBackground(new FindCallback() {
+ @Override
+ public void done(List foundObjects, ParseException e) {
+ if (Parse.isLocalDatastoreEnabled()
+ || query.getCachePolicy() != ParseQuery.CachePolicy.CACHE_ONLY
+ || e == null
+ || e.getCode() != ParseException.CACHE_MISS) {
+ if (e != null && (e.getCode() == ParseException.CONNECTION_FAILED
+ || e.getCode() != ParseException.CACHE_MISS)) {
+ } else if (foundObjects != null) {
+ if (shouldClear && firstCallback.get()) {
+ mObjectPages.clear();
+ mObjectPages.add(new ArrayList());
+ mCurrentPage = page;
+ firstCallback.set(Boolean.FALSE);
+ }
+
+ if (page >= mCurrentPage) {
+ mCurrentPage = page;
+ mHasNextPage = foundObjects.size() > objectsPerPage;
+ }
+
+ if (mHasNextPage) {
+ foundObjects.remove(objectsPerPage);
+ }
+
+ List currentPage = mObjectPages.get(page);
+ currentPage.clear();
+ currentPage.addAll(foundObjects);
+ syncTargetWithPages();
+ mTarget.notifyDataChanged();
+ }
+ }
+ notifyOnLoadedListeners(foundObjects, mHasNextPage, e);
+ mRunningQueries.remove(query);
+ }
+ });
+
+ }
+
+
+ private void notifyOnLoadingListeners() {
+ for (OnQueryLoadListener listener : mOnQueryLoadListeners) {
+ listener.onLoading();
+ }
+ }
+
+ private void notifyOnLoadedListeners(List list, boolean hasNextPage, ParseException e) {
+ for (OnQueryLoadListener listener : mOnQueryLoadListeners) {
+ listener.onLoaded(list, hasNextPage, e);
+ }
+ }
+
+ private void syncTargetWithPages() {
+ mTarget.clearList();
+ for (List list : mObjectPages) {
+ mTarget.appendList(list);
+ }
+ }
+
+ private void setPageOnQuery(int page, ParseQuery query) {
+ query.setLimit(mObjectsPerPage + 1);
+ // This code helps to fix bug on changing mObjectsPerPage on the fly
+ int skip = 0;
+ for(int i = 0; i < page; i++){
+ skip += mObjectPages.get(i).size();
+ }
+
+ query.setSkip(skip);
+ }
+
+ public void cancelAllRunningQueries() {
+ for (ParseQuery query : mRunningQueries) {
+ query.cancel();
+ }
+
+ mRunningQueries.clear();
+ }
+
+ public boolean isLoading(){
+ return !mRunningQueries.isEmpty();
+ }
+
+ public interface LoaderTarget {
+ void appendList(List sublist);
+
+ void clearList();
+
+ void notifyDataChanged();
+ }
+
+ public interface QueryFactory {
+ ParseQuery create();
+ }
+
+ public interface OnQueryLoadListener {
+ void onLoading();
+
+ void onLoaded(List list, boolean hasNextPage, ParseException e);
+ }
+}
diff --git a/ParseLoginUI/src/com/parse/ParseQueryAdapter.java b/ParseLoginUI/src/com/parse/ParseQueryAdapter.java
index 82f57ab..2ef5719 100644
--- a/ParseLoginUI/src/com/parse/ParseQueryAdapter.java
+++ b/ParseLoginUI/src/com/parse/ParseQueryAdapter.java
@@ -705,4 +705,4 @@ private void notifyOnLoadedListeners(List objects, Exception e) {
listener.onLoaded(objects, e);
}
}
-}
\ No newline at end of file
+}
diff --git a/build.gradle b/build.gradle
index 3ef1a06..98fd9f7 100644
--- a/build.gradle
+++ b/build.gradle
@@ -10,7 +10,7 @@ buildscript {
mavenCentral()
}
dependencies {
- classpath 'com.android.tools.build:gradle:1.0.0'
+ classpath 'com.android.tools.build:gradle:1.3.0'
}
}
diff --git a/settings.gradle b/settings.gradle
index 036e858..7124f9a 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -1,7 +1,7 @@
rootProject.name = 'ParseAndroidUI'
// Parse Login UI Library
-include ':ParseLoginUI'
+include ':ParseLoginUI', ':ParseListLoaderExample'
// Sample Projects
include ':ParseLoginSampleBasic'