scroll to newest message

This commit is contained in:
Mike Schwörer 2018-11-17 00:07:41 +01:00
parent 0f38ad6e5c
commit 87a3d34315
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
4 changed files with 23 additions and 6 deletions

View File

@ -103,7 +103,6 @@ public class SCNApp extends Application implements LifecycleObserver
==TODO==
[ ] - Delete single message (swipe right)
[ ] - scroll to newest message (armin says it doesnt)
[ ] - Android O repeat sound
[ ] - Query non-ack-ed messages in app
[ ] - notifications: how does WA do it??? - there you can change shit in-app

View File

@ -73,6 +73,7 @@ public class CMessageList
MessageAdapter a = ref.get();
if (a == null) continue;
a.customNotifyItemInserted(count);
a.scrollToTop();
}
CleanUpListener();
});
@ -127,6 +128,11 @@ public class CMessageList
return Messages.get(pos);
}
public CMessage tryGetFromBack(int pos)
{
return tryGet(Messages.size() - pos - 1);
}
public int size()
{
return Messages.size();

View File

@ -11,15 +11,20 @@ import com.blackforestbytes.simplecloudnotifier.model.CMessage;
import com.blackforestbytes.simplecloudnotifier.model.CMessageList;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class MessageAdapter extends RecyclerView.Adapter
{
private final View vNoElements;
private final LinearLayoutManager manLayout;
private final RecyclerView viewRecycler;
public MessageAdapter(View noElementsView)
public MessageAdapter(View noElementsView, LinearLayoutManager layout, RecyclerView recycler)
{
vNoElements = noElementsView;
vNoElements = noElementsView;
manLayout = layout;
viewRecycler = recycler;
CMessageList.inst().register(this);
vNoElements.setVisibility(getItemCount()>0 ? View.GONE : View.VISIBLE);
@ -36,7 +41,7 @@ public class MessageAdapter extends RecyclerView.Adapter
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position)
{
CMessage msg = CMessageList.inst().tryGet(position);
CMessage msg = CMessageList.inst().tryGetFromBack(position);
MessagePresenter view = (MessagePresenter) holder;
view.setMessage(msg);
}
@ -59,6 +64,11 @@ public class MessageAdapter extends RecyclerView.Adapter
vNoElements.setVisibility(getItemCount()>0 ? View.GONE : View.VISIBLE);
}
public void scrollToTop()
{
manLayout.smoothScrollToPosition(viewRecycler, null, 0);
}
private class MessagePresenter extends RecyclerView.ViewHolder implements View.OnClickListener
{
private TextView tvTimestamp;

View File

@ -31,8 +31,10 @@ public class NotificationsFragment extends Fragment
View v = inflater.inflate(R.layout.fragment_notifications, container, false);
RecyclerView rvMessages = v.findViewById(R.id.rvMessages);
rvMessages.setLayoutManager(new LinearLayoutManager(this.getContext(), RecyclerView.VERTICAL, true));
rvMessages.setAdapter(new MessageAdapter(v.findViewById(R.id.tvNoElements)));
LinearLayoutManager lman = new LinearLayoutManager(this.getContext(), RecyclerView.VERTICAL, false);
rvMessages.setLayoutManager(lman);
rvMessages.setAdapter(new MessageAdapter(v.findViewById(R.id.tvNoElements), lman, rvMessages));
//lman.scrollToPosition(0);
adView = v.findViewById(R.id.adBanner);
PublisherAdRequest adRequest = new PublisherAdRequest.Builder().build();