Update notification fragment

This commit is contained in:
stom79 2017-10-24 14:30:07 +02:00
parent c0122eef63
commit 979423818b
1 changed files with 31 additions and 28 deletions

View File

@ -19,12 +19,11 @@ import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.Toast; import android.widget.Toast;
import java.util.ArrayList; import java.util.ArrayList;
@ -60,9 +59,10 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
private boolean firstLoad; private boolean firstLoad;
private SwipeRefreshLayout swipeRefreshLayout; private SwipeRefreshLayout swipeRefreshLayout;
private boolean swiped; private boolean swiped;
private ListView lv_notifications; private RecyclerView lv_notifications;
private String lastReadNotifications; private String lastReadNotifications;
private String userId; private String userId;
LinearLayoutManager mLayoutManager;
public DisplayNotificationsFragment(){ public DisplayNotificationsFragment(){
} }
@ -78,12 +78,12 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
notifications = new ArrayList<>(); notifications = new ArrayList<>();
swiped = false; swiped = false;
swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeContainer); swipeRefreshLayout = rootView.findViewById(R.id.swipeContainer);
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
lv_notifications = (ListView) rootView.findViewById(R.id.lv_notifications); lv_notifications = rootView.findViewById(R.id.lv_notifications);
mainLoader = (RelativeLayout) rootView.findViewById(R.id.loader); mainLoader = rootView.findViewById(R.id.loader);
nextElementLoader = (RelativeLayout) rootView.findViewById(R.id.loading_next_notifications); nextElementLoader = rootView.findViewById(R.id.loading_next_notifications);
textviewNoAction = (RelativeLayout) rootView.findViewById(R.id.no_action); textviewNoAction = rootView.findViewById(R.id.no_action);
mainLoader.setVisibility(View.VISIBLE); mainLoader.setVisibility(View.VISIBLE);
nextElementLoader.setVisibility(View.GONE); nextElementLoader.setVisibility(View.GONE);
boolean isOnWifi = Helper.isOnWIFI(context); boolean isOnWifi = Helper.isOnWIFI(context);
@ -92,21 +92,24 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
lastReadNotifications = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId, null); lastReadNotifications = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId, null);
notificationsListAdapter = new NotificationsListAdapter(context,isOnWifi, behaviorWithAttachments,this.notifications); notificationsListAdapter = new NotificationsListAdapter(context,isOnWifi, behaviorWithAttachments,this.notifications);
lv_notifications.setAdapter(notificationsListAdapter); lv_notifications.setAdapter(notificationsListAdapter);
lv_notifications.setOnScrollListener(new AbsListView.OnScrollListener() { mLayoutManager = new LinearLayoutManager(context);
@Override lv_notifications.setLayoutManager(mLayoutManager);
public void onScrollStateChanged(AbsListView view, int scrollState) { lv_notifications.addOnScrollListener(new RecyclerView.OnScrollListener() {
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
} {
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if(dy > 0) {
int visibleItemCount = mLayoutManager.getChildCount();
if(firstVisibleItem + visibleItemCount == totalItemCount ) { int totalItemCount = mLayoutManager.getItemCount();
if(!flag_loading ) { int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
flag_loading = true; if (firstVisibleItem + visibleItemCount == totalItemCount) {
asyncTask = new RetrieveNotificationsAsyncTask(context, null, null, max_id, null, null,DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); if (!flag_loading) {
nextElementLoader.setVisibility(View.VISIBLE); flag_loading = true;
asyncTask = new RetrieveNotificationsAsyncTask(context, null, null, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
nextElementLoader.setVisibility(View.VISIBLE);
}
} else {
nextElementLoader.setVisibility(View.GONE);
} }
} else {
nextElementLoader.setVisibility(View.GONE);
} }
} }
}); });
@ -256,13 +259,13 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
if( notification != null){ if( notification != null){
//Update the id of the last notification retrieved //Update the id of the last notification retrieved
MainActivity.lastNotificationId = notification.getId(); MainActivity.lastNotificationId = notification.getId();
int index = lv_notifications.getFirstVisiblePosition() + 1;
View v = lv_notifications.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
notifications.add(0, notification); notifications.add(0, notification);
MainActivity.countNewNotifications++; MainActivity.countNewNotifications++;
notificationsListAdapter.notifyDataSetChanged(); int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
lv_notifications.setSelectionFromTop(index, top); if( firstVisibleItem > 0)
notificationsListAdapter.notifyItemInserted(0);
else
notificationsListAdapter.notifyDataSetChanged();
if( textviewNoAction.getVisibility() == View.VISIBLE) if( textviewNoAction.getVisibility() == View.VISIBLE)
textviewNoAction.setVisibility(View.GONE); textviewNoAction.setVisibility(View.GONE);
} }