rework article pager lazyload to be more similar to how headlines fragment does it
run scroll-related refresh requests using runnable
This commit is contained in:
parent
2e5d9bea0a
commit
174fa8c2af
@ -7,6 +7,7 @@ import android.os.BadParcelableException;
|
|||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
|
import android.support.design.widget.Snackbar;
|
||||||
import android.support.v4.app.ClassloaderWorkaroundFragmentStatePagerAdapter;
|
import android.support.v4.app.ClassloaderWorkaroundFragmentStatePagerAdapter;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.support.v4.app.FragmentManager;
|
import android.support.v4.app.FragmentManager;
|
||||||
@ -38,6 +39,8 @@ public class ArticlePager extends Fragment {
|
|||||||
private Feed m_feed;
|
private Feed m_feed;
|
||||||
private SharedPreferences m_prefs;
|
private SharedPreferences m_prefs;
|
||||||
private int m_firstId = 0;
|
private int m_firstId = 0;
|
||||||
|
private boolean m_refreshInProgress;
|
||||||
|
private boolean m_lazyLoadDisabled;
|
||||||
|
|
||||||
private class PagerAdapter extends ClassloaderWorkaroundFragmentStatePagerAdapter {
|
private class PagerAdapter extends ClassloaderWorkaroundFragmentStatePagerAdapter {
|
||||||
|
|
||||||
@ -155,9 +158,15 @@ public class ArticlePager extends Fragment {
|
|||||||
|
|
||||||
//Log.d(TAG, "Page #" + position + "/" + m_adapter.getCount());
|
//Log.d(TAG, "Page #" + position + "/" + m_adapter.getCount());
|
||||||
|
|
||||||
if ((m_activity.isSmallScreen() || m_activity.isPortrait()) && position == m_adapter.getCount() - 15) {
|
if (!m_refreshInProgress && !m_lazyLoadDisabled && (m_activity.isSmallScreen() || m_activity.isPortrait()) && position >= m_adapter.getCount() - 5) {
|
||||||
Log.d(TAG, "loading more articles...");
|
Log.d(TAG, "loading more articles...");
|
||||||
refresh(true);
|
|
||||||
|
new Handler().postDelayed(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
refresh(true);
|
||||||
|
}
|
||||||
|
}, 100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -169,10 +178,20 @@ public class ArticlePager extends Fragment {
|
|||||||
@SuppressWarnings({ "serial" })
|
@SuppressWarnings({ "serial" })
|
||||||
protected void refresh(boolean append) {
|
protected void refresh(boolean append) {
|
||||||
|
|
||||||
/* if (!m_feed.equals(Application.getInstance().m_activeFeed)) {
|
// viewpager doesn't like it when there's no data to display so we can't really allow not-appending here
|
||||||
append = false;
|
// luckily this fragment doesn't call for not-appending updates
|
||||||
|
// TODO: maybe later implement some placeholder fragment to show if there's no data
|
||||||
|
|
||||||
|
/* if (!append) {
|
||||||
|
m_lazyLoadDisabled = false;
|
||||||
|
|
||||||
|
// this won't ever work
|
||||||
|
m_articles.clear();
|
||||||
|
m_adapter.notifyDataSetChanged();
|
||||||
} */
|
} */
|
||||||
|
|
||||||
|
m_refreshInProgress = true;
|
||||||
|
|
||||||
HeadlinesRequest req = new HeadlinesRequest(getActivity().getApplicationContext(), m_activity, m_feed, m_articles) {
|
HeadlinesRequest req = new HeadlinesRequest(getActivity().getApplicationContext(), m_activity, m_feed, m_articles) {
|
||||||
@Override
|
@Override
|
||||||
protected void onProgressUpdate(Integer... progress) {
|
protected void onProgressUpdate(Integer... progress) {
|
||||||
@ -184,16 +203,23 @@ public class ArticlePager extends Fragment {
|
|||||||
if (isDetached() || !isAdded()) return;
|
if (isDetached() || !isAdded()) return;
|
||||||
|
|
||||||
super.onPostExecute(result);
|
super.onPostExecute(result);
|
||||||
|
|
||||||
|
m_refreshInProgress = false;
|
||||||
|
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
|
|
||||||
if (m_firstIdChanged && !(m_activity instanceof DetailActivity && !m_activity.isPortrait())) {
|
if (m_firstIdChanged) {
|
||||||
// TODO: show an information message in viewpager without modifying m_articles
|
m_lazyLoadDisabled = true;
|
||||||
//m_articles.add(new Article(HeadlinesFragment.ARTICLE_SPECIAL_TOP_CHANGED));
|
}
|
||||||
|
|
||||||
|
if (m_firstIdChanged && !(m_activity instanceof DetailActivity && !m_activity.isPortrait())) {
|
||||||
m_activity.toast(R.string.headlines_row_top_changed);
|
m_activity.toast(R.string.headlines_row_top_changed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (m_amountLoaded < HeadlinesFragment.HEADLINES_REQUEST_SIZE) {
|
||||||
|
m_lazyLoadDisabled = true;
|
||||||
|
}
|
||||||
|
|
||||||
ArticlePager.this.m_firstId = m_firstId;
|
ArticlePager.this.m_firstId = m_firstId;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -323,13 +349,8 @@ public class ArticlePager extends Fragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
|
||||||
/* if (m_articles.size() == 0 || !m_feed.equals(Application.getInstance().m_activeFeed)) {
|
|
||||||
refresh(false);
|
|
||||||
Application.getInstance().m_activeFeed = m_feed;
|
|
||||||
} */
|
|
||||||
|
|
||||||
if (m_adapter != null) m_adapter.notifyDataSetChanged();
|
//if (m_adapter != null) m_adapter.notifyDataSetChanged();
|
||||||
|
|
||||||
m_activity.invalidateOptionsMenu();
|
m_activity.invalidateOptionsMenu();
|
||||||
}
|
}
|
||||||
|
@ -488,8 +488,6 @@ public class HeadlinesFragment extends Fragment {
|
|||||||
}, 100);
|
}, 100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -529,7 +527,13 @@ public class HeadlinesFragment extends Fragment {
|
|||||||
//Log.d(TAG, "onScrolled: " + m_refreshInProgress + " " + m_lazyLoadDisabled + " " + lastVisibleItem + " " + m_articles.size());
|
//Log.d(TAG, "onScrolled: " + m_refreshInProgress + " " + m_lazyLoadDisabled + " " + lastVisibleItem + " " + m_articles.size());
|
||||||
|
|
||||||
if (!m_refreshInProgress && !m_lazyLoadDisabled && lastVisibleItem >= m_articles.size() - 5) {
|
if (!m_refreshInProgress && !m_lazyLoadDisabled && lastVisibleItem >= m_articles.size() - 5) {
|
||||||
refresh(true);
|
new Handler().postDelayed(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
refresh(true);
|
||||||
|
}
|
||||||
|
}, 100);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -615,9 +619,9 @@ public class HeadlinesFragment extends Fragment {
|
|||||||
if (m_swipeLayout != null) m_swipeLayout.setRefreshing(false);
|
if (m_swipeLayout != null) m_swipeLayout.setRefreshing(false);
|
||||||
|
|
||||||
//m_adapter.removeAllFooterViews();
|
//m_adapter.removeAllFooterViews();
|
||||||
|
m_refreshInProgress = false;
|
||||||
|
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
m_refreshInProgress = false;
|
|
||||||
|
|
||||||
if (m_activeArticle != null && !m_articles.containsId(m_activeArticle.id)) {
|
if (m_activeArticle != null && !m_articles.containsId(m_activeArticle.id)) {
|
||||||
m_activeArticle = null;
|
m_activeArticle = null;
|
||||||
|
Loading…
Reference in New Issue
Block a user