From 34000e7b790eb48ed6100b6c7554531043158679 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Sat, 1 Oct 2016 17:40:38 +0200 Subject: [PATCH] Some layout improvements and fixes --- .../dissem/apps/abit/MessageListFragment.java | 11 ++-- .../abit/adapter/AddressSelectorAdapter.java | 10 ---- .../dissem/apps/abit/adapter/Selectable.java | 29 ++++++++++ .../abit/adapter/SwipeableMessageAdapter.java | 31 +++++++++-- .../bg_swipe_item_left.xml | 0 .../bg_swipe_item_right.xml | 0 .../res/drawable/bg_item_selected_state.xml | 21 ++++++++ .../layout-w720dp/activity_message_list.xml | 53 ++++++++++--------- .../main/res/layout/fragment_message_list.xml | 4 +- app/src/main/res/values/colors.xml | 2 + 10 files changed, 118 insertions(+), 43 deletions(-) create mode 100644 app/src/main/java/ch/dissem/apps/abit/adapter/Selectable.java rename app/src/main/res/{drawable-anydpi-v21 => drawable-v21}/bg_swipe_item_left.xml (100%) rename app/src/main/res/{drawable-anydpi-v21 => drawable-v21}/bg_swipe_item_right.xml (100%) create mode 100644 app/src/main/res/drawable/bg_item_selected_state.xml diff --git a/app/src/main/java/ch/dissem/apps/abit/MessageListFragment.java b/app/src/main/java/ch/dissem/apps/abit/MessageListFragment.java index bf79b0d..9328ce2 100644 --- a/app/src/main/java/ch/dissem/apps/abit/MessageListFragment.java +++ b/app/src/main/java/ch/dissem/apps/abit/MessageListFragment.java @@ -70,7 +70,7 @@ public class MessageListFragment extends Fragment implements ListHolder { private Label currentLabel; private MenuItem emptyTrashMenuItem; private MessageRepository messageRepo; - private List messages; + private boolean activateOnItemClick; /** * Mandatory empty constructor for the fragment manager to instantiate the @@ -105,7 +105,7 @@ public class MessageListFragment extends Fragment implements ListHolder { } private void doUpdateList(Label label) { - messages = Singleton.getMessageRepository(getContext()).findMessages(label); + List<Plaintext> messages = Singleton.getMessageRepository(getContext()).findMessages(label); if (getActivity() instanceof ActionBarListener) { if (label != null) { ((ActionBarListener) getActivity()).updateTitle(label.toString()); @@ -153,6 +153,7 @@ public class MessageListFragment extends Fragment implements ListHolder { //adapter adapter = new SwipeableMessageAdapter(); + adapter.setActivateOnItemClick(activateOnItemClick); adapter.setEventListener(new SwipeableMessageAdapter.EventListener() { @Override public void onItemDeleted(Plaintext item) { @@ -174,6 +175,7 @@ public class MessageListFragment extends Fragment implements ListHolder { @Override public void onItemViewClicked(View v, boolean pinned) { int position = recyclerView.getChildAdapterPosition(v); + adapter.setSelectedPosition(position); if (position != RecyclerView.NO_POSITION) { Plaintext item = adapter.getItem(position); ((MainActivity) getActivity()).onItemSelected(item); @@ -263,6 +265,9 @@ public class MessageListFragment extends Fragment implements ListHolder { @Override public void setActivateOnItemClick(boolean activateOnItemClick) { - // TODO + if (adapter != null) { + adapter.setActivateOnItemClick(activateOnItemClick); + } + this.activateOnItemClick = activateOnItemClick; } } diff --git a/app/src/main/java/ch/dissem/apps/abit/adapter/AddressSelectorAdapter.java b/app/src/main/java/ch/dissem/apps/abit/adapter/AddressSelectorAdapter.java index cf23ed7..4103097 100644 --- a/app/src/main/java/ch/dissem/apps/abit/adapter/AddressSelectorAdapter.java +++ b/app/src/main/java/ch/dissem/apps/abit/adapter/AddressSelectorAdapter.java @@ -34,7 +34,6 @@ import ch.dissem.bitmessage.entity.BitmessageAddress; /** * @author Christian Basler */ - public class AddressSelectorAdapter extends RecyclerView.Adapter<AddressSelectorAdapter.ViewHolder> { @@ -88,15 +87,6 @@ public class AddressSelectorAdapter } } - private static class Selectable<T> { - private final T data; - private boolean selected = false; - - private Selectable(T data) { - this.data = data; - } - } - public List<BitmessageAddress> getSelected() { List<BitmessageAddress> result = new LinkedList<>(); for (Selectable<BitmessageAddress> selectable : data) { diff --git a/app/src/main/java/ch/dissem/apps/abit/adapter/Selectable.java b/app/src/main/java/ch/dissem/apps/abit/adapter/Selectable.java new file mode 100644 index 0000000..ddaaf4c --- /dev/null +++ b/app/src/main/java/ch/dissem/apps/abit/adapter/Selectable.java @@ -0,0 +1,29 @@ +/* + * Copyright 2016 Christian Basler + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.dissem.apps.abit.adapter; + +/** + * @author Christian Basler + */ +class Selectable<T> { + final T data; + boolean selected = false; + + Selectable(T data) { + this.data = data; + } +} diff --git a/app/src/main/java/ch/dissem/apps/abit/adapter/SwipeableMessageAdapter.java b/app/src/main/java/ch/dissem/apps/abit/adapter/SwipeableMessageAdapter.java index 843266a..a0f9423 100644 --- a/app/src/main/java/ch/dissem/apps/abit/adapter/SwipeableMessageAdapter.java +++ b/app/src/main/java/ch/dissem/apps/abit/adapter/SwipeableMessageAdapter.java @@ -17,6 +17,7 @@ package ch.dissem.apps.abit.adapter; +import android.annotation.SuppressLint; import android.graphics.Typeface; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; @@ -50,7 +51,7 @@ import static ch.dissem.apps.abit.util.Strings.normalizeWhitespaces; * * @author Christian Basler * @see <a href="https://github.com/h6ah4i/android-advancedrecyclerview"> - * https://github.com/h6ah4i/android-advancedrecyclerview</a> + * https://github.com/h6ah4i/android-advancedrecyclerview</a> */ public class SwipeableMessageAdapter extends RecyclerView.Adapter<SwipeableMessageAdapter.ViewHolder> @@ -62,6 +63,12 @@ public class SwipeableMessageAdapter private View.OnClickListener swipeableViewContainerOnClickListener; private Label label; + private int selectedPosition; + private boolean activateOnItemClick; + + public void setActivateOnItemClick(boolean activateOnItemClick) { + this.activateOnItemClick = activateOnItemClick; + } public interface EventListener { void onItemDeleted(Plaintext item); @@ -71,14 +78,15 @@ public class SwipeableMessageAdapter void onItemViewClicked(View v, boolean pinned); } - public static class ViewHolder extends AbstractSwipeableItemViewHolder { + @SuppressWarnings("WeakerAccess") + static class ViewHolder extends AbstractSwipeableItemViewHolder { public FrameLayout container; public final ImageView avatar; public final TextView sender; public final TextView subject; public final TextView extract; - public ViewHolder(View v) { + ViewHolder(View v) { super(v); container = (FrameLayout) v.findViewById(R.id.container); avatar = (ImageView) v.findViewById(R.id.avatar); @@ -150,6 +158,14 @@ public class SwipeableMessageAdapter public void onBindViewHolder(ViewHolder holder, int position) { final Plaintext item = data.get(position); + if (activateOnItemClick) { + holder.container.setBackgroundResource( + position == selectedPosition + ? R.drawable.bg_item_selected_state + : R.drawable.bg_item_normal_state + ); + } + // set listeners // (if the item is *pinned*, click event comes to the itemView) holder.itemView.setOnClickListener(itemViewOnClickListener); @@ -184,6 +200,7 @@ public class SwipeableMessageAdapter } @Override + @SuppressLint("SwitchIntDef") public void onSetSwipeBackground(ViewHolder holder, int position, int type) { int bgRes = 0; switch (type) { @@ -205,6 +222,7 @@ public class SwipeableMessageAdapter } @Override + @SuppressLint("SwitchIntDef") public SwipeResultAction onSwipeItem(ViewHolder holder, final int position, int result) { switch (result) { // swipe right @@ -223,6 +241,13 @@ public class SwipeableMessageAdapter this.eventListener = eventListener; } + public void setSelectedPosition(int selectedPosition) { + int oldPosition = this.selectedPosition; + this.selectedPosition = selectedPosition; + notifyItemChanged(oldPosition); + notifyItemChanged(selectedPosition); + } + private static class SwipeLeftResultAction extends SwipeResultActionMoveToSwipedDirection { private SwipeableMessageAdapter adapter; private final int position; diff --git a/app/src/main/res/drawable-anydpi-v21/bg_swipe_item_left.xml b/app/src/main/res/drawable-v21/bg_swipe_item_left.xml similarity index 100% rename from app/src/main/res/drawable-anydpi-v21/bg_swipe_item_left.xml rename to app/src/main/res/drawable-v21/bg_swipe_item_left.xml diff --git a/app/src/main/res/drawable-anydpi-v21/bg_swipe_item_right.xml b/app/src/main/res/drawable-v21/bg_swipe_item_right.xml similarity index 100% rename from app/src/main/res/drawable-anydpi-v21/bg_swipe_item_right.xml rename to app/src/main/res/drawable-v21/bg_swipe_item_right.xml diff --git a/app/src/main/res/drawable/bg_item_selected_state.xml b/app/src/main/res/drawable/bg_item_selected_state.xml new file mode 100644 index 0000000..32a0c99 --- /dev/null +++ b/app/src/main/res/drawable/bg_item_selected_state.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2015 Haruki Hasegawa + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> + <item> + <color android:color="@color/bg_item_selected_state"/> + </item> +</layer-list> diff --git a/app/src/main/res/layout-w720dp/activity_message_list.xml b/app/src/main/res/layout-w720dp/activity_message_list.xml index 8ad5fef..a4b6855 100644 --- a/app/src/main/res/layout-w720dp/activity_message_list.xml +++ b/app/src/main/res/layout-w720dp/activity_message_list.xml @@ -1,52 +1,53 @@ <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" - android:gravity="center" android:layout_width="match_parent" - android:layout_height="match_parent"> + android:layout_height="match_parent" + android:gravity="center"> <android.support.v7.widget.Toolbar - android:id="@+id/toolbar" - android:layout_width="match_parent" - android:layout_height="?attr/actionBarSize" - android:background="?attr/colorPrimary" - android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" - app:popupTheme="@style/ThemeOverlay.AppCompat.Light" - android:elevation="4dp" - tools:ignore="UnusedAttribute"/> + android:id="@+id/toolbar" + android:layout_width="match_parent" + android:layout_height="?attr/actionBarSize" + android:background="?attr/colorPrimary" + android:elevation="4dp" + android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" + app:popupTheme="@style/ThemeOverlay.AppCompat.Light" + tools:ignore="UnusedAttribute"/> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" - android:layout_below="@id/toolbar" android:layout_width="match_parent" android:layout_height="match_parent" - android:layout_marginLeft="16dp" - android:layout_marginRight="16dp" + android:layout_below="@id/toolbar" + android:background="@color/bg_item_selected_state" android:baselineAligned="false" - android:divider="?android:attr/dividerHorizontal" android:orientation="horizontal" android:showDividers="middle" - tools:context=".MessageListActivity"> + tools:context=".MainActivity"> <!-- This layout is a two-pane layout for the Messages master/detail flow. - --> <FrameLayout - android:id="@+id/item_list" - android:layout_width="0dp" - android:layout_height="match_parent" - android:layout_weight="1" - tools:context=".MessageListActivity" - tools:layout="@android:layout/list_content"/> + android:id="@+id/item_list" + android:layout_width="0dp" + android:layout_height="match_parent" + android:layout_weight="1" + tools:context=".MessageListActivity" + tools:layout="@layout/fragment_message_list"/> <FrameLayout - android:id="@+id/message_detail_container" - android:layout_width="0dp" - android:layout_height="match_parent" - android:layout_weight="3"/> + android:id="@+id/message_detail_container" + android:layout_width="0dp" + android:layout_height="match_parent" + android:layout_margin="16dp" + android:layout_weight="2" + android:background="@color/contentBackground" + android:elevation="2dp" + tools:layout="@layout/fragment_message_detail"/> </LinearLayout> </RelativeLayout> diff --git a/app/src/main/res/layout/fragment_message_list.xml b/app/src/main/res/layout/fragment_message_list.xml index 286612c..0fe153d 100644 --- a/app/src/main/res/layout/fragment_message_list.xml +++ b/app/src/main/res/layout/fragment_message_list.xml @@ -1,6 +1,7 @@ <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" + xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> @@ -15,7 +16,8 @@ android:paddingBottom="88dp" android:scrollbarStyle="outsideOverlay" - android:scrollbars="vertical"/> + android:scrollbars="vertical" + tools:listitem="@layout/message_row"/> <android.support.design.widget.FloatingActionButton android:id="@+id/fab_compose_message" diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index e36d1eb..f2f59f8 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -8,6 +8,7 @@ <color name="colorAccent">#607D8B</color> <color name="colorPrimaryText">#212121</color> <color name="colorSecondaryText">#727272</color> + <color name="contentBackground">#FFFFFF</color> <color name="icons">#212121</color> <color name="divider">#B6B6B6</color> @@ -49,6 +50,7 @@ <color name="bg_item_normal_state">#ffffffff</color> <color name="bg_item_swiping_state">@color/colorPrimaryLight</color> <color name="bg_item_swiping_active_state">@color/colorPrimary</color> + <color name="bg_item_selected_state">@color/colorPrimaryLight</color> <color name="bg_swipe_item_neutral">@android:color/transparent</color> <color name="bg_swipe_item_trash">#fff45f30</color> <color name="bg_swipe_item_archive">#fff9930d</color>