This commit is contained in:
Christian Schabesberger 2016-01-05 21:23:16 +01:00
parent eb0df2b101
commit 7f32857e00
1 changed files with 14 additions and 13 deletions

View File

@ -66,6 +66,9 @@ public class VideoItemListFragment extends ListFragment {
private View footer; private View footer;
// used to suppress request for loading a new page while another page is already loading.
private boolean loadingNextPage = true;
private class ResultRunnable implements Runnable { private class ResultRunnable implements Runnable {
private final SearchEngine.Result result; private final SearchEngine.Result result;
private final int requestId; private final int requestId;
@ -76,6 +79,9 @@ public class VideoItemListFragment extends ListFragment {
@Override @Override
public void run() { public void run() {
updateListOnResult(result, requestId); updateListOnResult(result, requestId);
if (android.os.Build.VERSION.SDK_INT >= 19) {
getListView().removeFooterView(footer);
}
} }
} }
@ -84,7 +90,7 @@ public class VideoItemListFragment extends ListFragment {
private final String query; private final String query;
private final int page; private final int page;
final Handler h = new Handler(); final Handler h = new Handler();
private volatile boolean run = true; private volatile boolean runs = true;
private final int requestId; private final int requestId;
public SearchRunnable(SearchEngine engine, String query, int page, int requestId) { public SearchRunnable(SearchEngine engine, String query, int page, int requestId) {
this.engine = engine; this.engine = engine;
@ -93,7 +99,7 @@ public class VideoItemListFragment extends ListFragment {
this.requestId = requestId; this.requestId = requestId;
} }
void terminate() { void terminate() {
run = false; runs = false;
} }
@Override @Override
public void run() { public void run() {
@ -104,7 +110,7 @@ public class VideoItemListFragment extends ListFragment {
getString(R.string.defaultLanguageItem)); getString(R.string.defaultLanguageItem));
SearchEngine.Result result = engine.search(query, page, searchLanguage); SearchEngine.Result result = engine.search(query, page, searchLanguage);
Log.i(TAG, "language code passed:\""+searchLanguage+"\""); Log.i(TAG, "language code passed:\""+searchLanguage+"\"");
if(run) { if(runs) {
h.post(new ResultRunnable(result, requestId)); h.post(new ResultRunnable(result, requestId));
} }
} catch(Exception e) { } catch(Exception e) {
@ -116,15 +122,6 @@ public class VideoItemListFragment extends ListFragment {
} }
}); });
} }
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (android.os.Build.VERSION.SDK_INT >= 19) {
getListView().removeFooterView(footer);
}
}
});
} }
} }
@ -204,6 +201,7 @@ public class VideoItemListFragment extends ListFragment {
} }
private void nextPage() { private void nextPage() {
loadingNextPage = true;
lastPage++; lastPage++;
Log.d(TAG, getString(R.string.searchPage) + Integer.toString(lastPage)); Log.d(TAG, getString(R.string.searchPage) + Integer.toString(lastPage));
startSearch(query, lastPage); startSearch(query, lastPage);
@ -249,6 +247,8 @@ public class VideoItemListFragment extends ListFragment {
Log.w(TAG, "Trying to set value while activity doesn't exist anymore."); Log.w(TAG, "Trying to set value while activity doesn't exist anymore.");
} catch(Exception e) { } catch(Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally {
loadingNextPage = false;
} }
} }
@ -325,7 +325,8 @@ public class VideoItemListFragment extends ListFragment {
&& list.getLastVisiblePosition() == list.getAdapter().getCount() - 1 && list.getLastVisiblePosition() == list.getAdapter().getCount() - 1
&& list.getChildAt(list.getChildCount() - 1).getBottom() <= list.getHeight()) { && list.getChildAt(list.getChildCount() - 1).getBottom() <= list.getHeight()) {
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
if ((time - lastScrollDate) > 200) { if ((time - lastScrollDate) > 200
&& !loadingNextPage) {
lastScrollDate = time; lastScrollDate = time;
getListView().addFooterView(footer); getListView().addFooterView(footer);
nextPage(); nextPage();