diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-04-24 21:25:14 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-04-24 21:25:14 +0200 |
commit | 00439c9923911cafb0ed06398c1a65a1871e921e (patch) | |
tree | 6b133dc8ac4678fb3d8ee647d3d5a4f85938baef /src/main/java/de/pixart/messenger/ui | |
parent | db4ab7177d5e550dcd04ea3617cdd231d4923aa9 (diff) |
migrate the RecyclerView
Diffstat (limited to 'src/main/java/de/pixart/messenger/ui')
4 files changed, 108 insertions, 48 deletions
diff --git a/src/main/java/de/pixart/messenger/ui/ConversationsOverviewFragment.java b/src/main/java/de/pixart/messenger/ui/ConversationsOverviewFragment.java index 8ee743320..6c478e92f 100644 --- a/src/main/java/de/pixart/messenger/ui/ConversationsOverviewFragment.java +++ b/src/main/java/de/pixart/messenger/ui/ConversationsOverviewFragment.java @@ -33,6 +33,7 @@ import android.app.Activity; import android.app.Fragment; import android.databinding.DataBindingUtil; import android.os.Bundle; +import android.support.v7.widget.LinearLayoutManager; import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -47,6 +48,7 @@ import de.pixart.messenger.databinding.FragmentConversationsOverviewBinding; import de.pixart.messenger.entities.Conversation; import de.pixart.messenger.ui.adapter.ConversationAdapter; import de.pixart.messenger.ui.interfaces.OnConversationSelected; +import de.pixart.messenger.ui.util.PendingActionHelper; import de.pixart.messenger.ui.util.PendingItem; import de.pixart.messenger.ui.util.ScrollState; @@ -60,6 +62,7 @@ public class ConversationsOverviewFragment extends XmppFragment { private FragmentConversationsOverviewBinding binding; private ConversationAdapter conversationsAdapter; private XmppActivity activity; + private PendingActionHelper pendingActionHelper = new PendingActionHelper(); public static Conversation getSuggestion(Activity activity) { final Conversation exception; @@ -111,6 +114,13 @@ public class ConversationsOverviewFragment extends XmppFragment { } @Override + public void onPause() { + Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onPause()"); + pendingActionHelper.execute(); + super.onPause(); + } + + @Override public void onDetach() { super.onDetach(); this.activity = null; @@ -123,9 +133,7 @@ public class ConversationsOverviewFragment extends XmppFragment { this.binding.fab.setOnClickListener((view) -> StartConversationActivity.launch(getActivity())); this.conversationsAdapter = new ConversationAdapter(this.activity, this.conversations); - this.binding.list.setAdapter(this.conversationsAdapter); - this.binding.list.setOnItemClickListener((parent, view, position, id) -> { - Conversation conversation = this.conversations.get(position); + this.conversationsAdapter.setConversationClickListener((view, conversation) -> { if (activity instanceof OnConversationSelected) { ((OnConversationSelected) activity).onConversationSelected(conversation); } else { @@ -133,6 +141,8 @@ public class ConversationsOverviewFragment extends XmppFragment { } }); + this.binding.list.setAdapter(this.conversationsAdapter); + this.binding.list.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false)); return binding.getRoot(); } @@ -154,7 +164,8 @@ public class ConversationsOverviewFragment extends XmppFragment { if (this.binding == null) { return null; } - int position = this.binding.list.getFirstVisiblePosition(); + LinearLayoutManager layoutManager = (LinearLayoutManager) this.binding.list.getLayoutManager(); + int position = layoutManager.findFirstVisibleItemPosition(); final View view = this.binding.list.getChildAt(0); if (view != null) { return new ScrollState(position, view.getTop()); @@ -194,7 +205,8 @@ public class ConversationsOverviewFragment extends XmppFragment { private void setScrollPosition(ScrollState scrollPosition) { if (scrollPosition != null) { - this.binding.list.setSelectionFromTop(scrollPosition.position, scrollPosition.offset); + LinearLayoutManager layoutManager = (LinearLayoutManager) binding.list.getLayoutManager(); + layoutManager.scrollToPositionWithOffset(scrollPosition.position, scrollPosition.offset); } } }
\ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/ui/ShareWithActivity.java b/src/main/java/de/pixart/messenger/ui/ShareWithActivity.java index 55b392e97..7c4b33027 100644 --- a/src/main/java/de/pixart/messenger/ui/ShareWithActivity.java +++ b/src/main/java/de/pixart/messenger/ui/ShareWithActivity.java @@ -5,10 +5,10 @@ import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; +import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.Menu; import android.view.MenuItem; -import android.widget.ListView; import android.widget.Toast; import java.net.URLConnection; @@ -62,7 +62,7 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer private Share share; private static final int REQUEST_START_NEW_CONVERSATION = 0x0501; - private ListView mListView; + private RecyclerView mListView; private ConversationAdapter mAdapter; private List<Conversation> mConversations = new ArrayList<>(); private Toast mToast; @@ -176,7 +176,7 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer mListView = findViewById(R.id.choose_conversation_list); mAdapter = new ConversationAdapter(this, this.mConversations); mListView.setAdapter(mAdapter); - mListView.setOnItemClickListener((arg0, arg1, position, arg3) -> share(mConversations.get(position))); + mAdapter.setConversationClickListener((view, conversation) -> share(conversation)); if (savedInstanceState != null) { SharingIsRunning = savedInstanceState.getBoolean(STATE_SHARING_IS_RUNNING, false); diff --git a/src/main/java/de/pixart/messenger/ui/adapter/ConversationAdapter.java b/src/main/java/de/pixart/messenger/ui/adapter/ConversationAdapter.java index 802ee0187..804e71eaf 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/ConversationAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/ConversationAdapter.java @@ -11,11 +11,11 @@ import android.os.AsyncTask; import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.v4.content.ContextCompat; +import android.support.v7.widget.RecyclerView; import android.util.Pair; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; @@ -28,7 +28,6 @@ import de.pixart.messenger.entities.Conversation; import de.pixart.messenger.entities.Message; import de.pixart.messenger.entities.MucOptions; import de.pixart.messenger.entities.Transferable; -import de.pixart.messenger.ui.ConversationFragment; import de.pixart.messenger.ui.XmppActivity; import de.pixart.messenger.ui.util.Color; import de.pixart.messenger.ui.widget.UnreadCountCustomView; @@ -38,15 +37,15 @@ import de.pixart.messenger.utils.UIHelper; import de.pixart.messenger.xmpp.chatstate.ChatState; import rocks.xmpp.addr.Jid; -public class ConversationAdapter extends ArrayAdapter<Conversation> { +public class ConversationAdapter extends RecyclerView.Adapter<ConversationAdapter.ConversationViewHolder> { private XmppActivity activity; - private Conversation selectedConversation = null; + private List<Conversation> conversations; + private OnConversationClickListener listener; - public ConversationAdapter(XmppActivity activity, - List<Conversation> conversations) { - super(activity, 0, conversations); + public ConversationAdapter(XmppActivity activity, List<Conversation> conversations) { this.activity = activity; + this.conversations = conversations; } private static boolean cancelPotentialWork(Conversation conversation, ImageView imageView) { @@ -74,17 +73,20 @@ public class ConversationAdapter extends ArrayAdapter<Conversation> { return null; } + @NonNull @Override - public @NonNull - View getView(int position, View view, @NonNull ViewGroup parent) { - if (view == null) { - LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - view = inflater.inflate(R.layout.conversation_list_row, parent, false); - } - ViewHolder viewHolder = ViewHolder.get(view); - Conversation conversation = getItem(position); + public ConversationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + View view = inflater.inflate(R.layout.conversation_list_row, parent, false); + ConversationViewHolder conversationViewHolder = ConversationViewHolder.get(view); + return conversationViewHolder; + } + + @Override + public void onBindViewHolder(@NonNull ConversationViewHolder viewHolder, int position) { + Conversation conversation = conversations.get(position); if (conversation == null) { - return view; + return; } if (conversation.getMode() == Conversation.MODE_SINGLE || activity.useSubjectToIdentifyConference()) { CharSequence name = conversation.getName(); @@ -300,13 +302,16 @@ public class ConversationAdapter extends ArrayAdapter<Conversation> { } } } - return view; + viewHolder.itemView.setOnClickListener(v -> listener.onConversationClick(v, conversation)); } @Override - public void notifyDataSetChanged() { - this.selectedConversation = ConversationFragment.getConversation(activity); - super.notifyDataSetChanged(); + public int getItemCount() { + return conversations.size(); + } + + public void setConversationClickListener(OnConversationClickListener listener) { + this.listener = listener; } private void loadAvatar(Conversation conversation, ImageView imageView) { @@ -330,7 +335,17 @@ public class ConversationAdapter extends ArrayAdapter<Conversation> { } } - public static class ViewHolder { + public void insert(Conversation c, int position) { + conversations.add(position, c); + notifyDataSetChanged(); + } + + public void remove(Conversation conversation, int position) { + conversations.remove(conversation); + notifyItemRemoved(position); + } + + public static class ConversationViewHolder extends RecyclerView.ViewHolder { private TextView name; private TextView lastMessage; private ImageView lastMessageIcon; @@ -343,28 +358,28 @@ public class ConversationAdapter extends ArrayAdapter<Conversation> { private ImageView readStatus; private ImageView avatar; - private ViewHolder() { - + private ConversationViewHolder(View view) { + super(view); } - public static ViewHolder get(View layout) { - ViewHolder viewHolder = (ViewHolder) layout.getTag(); - if (viewHolder == null) { - viewHolder = new ViewHolder(); - viewHolder.name = layout.findViewById(R.id.conversation_name); - viewHolder.lastMessage = layout.findViewById(R.id.conversation_lastmsg); - viewHolder.lastMessageIcon = layout.findViewById(R.id.conversation_lastmsg_img); - viewHolder.timestamp = layout.findViewById(R.id.conversation_lastupdate); - viewHolder.avatar = layout.findViewById(R.id.conversation_image); - viewHolder.sender = layout.findViewById(R.id.sender_name); - viewHolder.notificationIcon = layout.findViewById(R.id.notification_status); - viewHolder.unreadCount = layout.findViewById(R.id.conversation_unread); - viewHolder.failedCount = layout.findViewById(R.id.conversation_failed); - viewHolder.receivedStatus = layout.findViewById(R.id.indicator_received); - viewHolder.readStatus = layout.findViewById(R.id.indicator_read); - layout.setTag(viewHolder); + public static ConversationViewHolder get(View layout) { + ConversationViewHolder conversationViewHolder = (ConversationViewHolder) layout.getTag(); + if (conversationViewHolder == null) { + conversationViewHolder = new ConversationViewHolder(layout); + conversationViewHolder.name = layout.findViewById(R.id.conversation_name); + conversationViewHolder.lastMessage = layout.findViewById(R.id.conversation_lastmsg); + conversationViewHolder.lastMessageIcon = layout.findViewById(R.id.conversation_lastmsg_img); + conversationViewHolder.timestamp = layout.findViewById(R.id.conversation_lastupdate); + conversationViewHolder.avatar = layout.findViewById(R.id.conversation_image); + conversationViewHolder.sender = layout.findViewById(R.id.sender_name); + conversationViewHolder.notificationIcon = layout.findViewById(R.id.notification_status); + conversationViewHolder.unreadCount = layout.findViewById(R.id.conversation_unread); + conversationViewHolder.failedCount = layout.findViewById(R.id.conversation_failed); + conversationViewHolder.receivedStatus = layout.findViewById(R.id.indicator_received); + conversationViewHolder.readStatus = layout.findViewById(R.id.indicator_read); + layout.setTag(conversationViewHolder); } - return viewHolder; + return conversationViewHolder; } } @@ -414,4 +429,8 @@ public class ConversationAdapter extends ArrayAdapter<Conversation> { protected SharedPreferences getPreferences() { return PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext()); } + + public interface OnConversationClickListener { + void onConversationClick(View view, Conversation conversation); + } }
\ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/ui/util/PendingActionHelper.java b/src/main/java/de/pixart/messenger/ui/util/PendingActionHelper.java new file mode 100644 index 000000000..06c291711 --- /dev/null +++ b/src/main/java/de/pixart/messenger/ui/util/PendingActionHelper.java @@ -0,0 +1,29 @@ +package de.pixart.messenger.ui.util; + +/** + * Created by mxf on 2018/4/3. + */ + +public class PendingActionHelper { + + private PendingAction pendingAction; + + public void push(PendingAction pendingAction) { + this.pendingAction = pendingAction; + } + + public void execute() { + if (pendingAction != null) { + pendingAction.execute(); + pendingAction = null; + } + } + + public void undo() { + pendingAction = null; + } + + public interface PendingAction { + void execute(); + } +}
\ No newline at end of file |