Merge "Revive checkboxes to select messages on MessageList"
This commit is contained in:
commit
4772322cc9
|
@ -4,9 +4,9 @@
|
|||
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.
|
||||
|
@ -25,6 +25,14 @@
|
|||
android:layout_width="4dip"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_centerVertical="true" />
|
||||
<ImageView
|
||||
android:id="@+id/selected"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_toRightOf="@id/chip"
|
||||
android:paddingTop="10dip"
|
||||
android:paddingLeft="4dip"
|
||||
android:src="@drawable/btn_check_buttonless_dark_off" />
|
||||
<ImageView
|
||||
android:id="@+id/favorite"
|
||||
android:layout_width="wrap_content"
|
||||
|
@ -36,7 +44,7 @@
|
|||
android:id="@+id/from"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_toRightOf="@id/chip"
|
||||
android:layout_toRightOf="@id/selected"
|
||||
android:layout_toLeftOf="@id/favorite"
|
||||
android:ellipsize="end"
|
||||
android:singleLine="true"
|
||||
|
|
|
@ -192,7 +192,7 @@ public class MessageListFragment extends ListFragment
|
|||
listView.setOnItemLongClickListener(this);
|
||||
listView.setItemsCanFocus(false);
|
||||
|
||||
mListAdapter = new MessagesAdapter(mActivity, new Handler(), this);
|
||||
mListAdapter = new MessagesAdapter(mActivity, this);
|
||||
|
||||
mListFooterView = getActivity().getLayoutInflater().inflate(
|
||||
R.layout.message_list_item_footer, listView, false);
|
||||
|
@ -373,7 +373,7 @@ public class MessageListFragment extends ListFragment
|
|||
}
|
||||
|
||||
private void toggleSelection(MessageListItem itemView) {
|
||||
mListAdapter.updateSelected(itemView, !mListAdapter.isSelected(itemView));
|
||||
mListAdapter.toggleSelected(itemView);
|
||||
}
|
||||
|
||||
private void onMessageOpen(final long mailboxId, final long messageId) {
|
||||
|
|
|
@ -29,20 +29,22 @@ import android.widget.RelativeLayout;
|
|||
* 2. It handles internal clicks such as the checkbox or the favorite star
|
||||
*/
|
||||
public class MessageListItem extends RelativeLayout {
|
||||
|
||||
public long mMessageId;
|
||||
public long mMailboxId;
|
||||
public long mAccountId;
|
||||
public boolean mRead;
|
||||
public boolean mFavorite;
|
||||
// Note: messagesAdapter directly fiddles with these fields.
|
||||
/* package */ long mMessageId;
|
||||
/* package */ long mMailboxId;
|
||||
/* package */ long mAccountId;
|
||||
/* package */ boolean mRead;
|
||||
/* package */ boolean mFavorite;
|
||||
|
||||
private MessagesAdapter mAdapter;
|
||||
|
||||
private boolean mDownEvent;
|
||||
private boolean mCachedViewPositions;
|
||||
private int mCheckRight;
|
||||
private int mStarLeft;
|
||||
|
||||
// Padding to increase clickable areas on right of each list item
|
||||
// Padding to increase clickable areas on left & right of each list item
|
||||
private final static float CHECKMARK_PAD = 10.0F;
|
||||
private final static float STAR_PAD = 10.0F;
|
||||
|
||||
public MessageListItem(Context context) {
|
||||
|
@ -77,8 +79,10 @@ public class MessageListItem extends RelativeLayout {
|
|||
int touchX = (int) event.getX();
|
||||
|
||||
if (!mCachedViewPositions) {
|
||||
float paddingScale = getContext().getResources().getDisplayMetrics().density;
|
||||
int starPadding = (int) ((STAR_PAD * paddingScale) + 0.5);
|
||||
final float paddingScale = getContext().getResources().getDisplayMetrics().density;
|
||||
final int checkPadding = (int) ((CHECKMARK_PAD * paddingScale) + 0.5);
|
||||
final int starPadding = (int) ((STAR_PAD * paddingScale) + 0.5);
|
||||
mCheckRight = findViewById(R.id.selected).getRight() + checkPadding;
|
||||
mStarLeft = findViewById(R.id.favorite).getLeft() - starPadding;
|
||||
mCachedViewPositions = true;
|
||||
}
|
||||
|
@ -86,7 +90,7 @@ public class MessageListItem extends RelativeLayout {
|
|||
switch (event.getAction()) {
|
||||
case MotionEvent.ACTION_DOWN:
|
||||
mDownEvent = true;
|
||||
if (touchX > mStarLeft) {
|
||||
if ((touchX < mCheckRight) || (touchX > mStarLeft)) {
|
||||
handled = true;
|
||||
}
|
||||
break;
|
||||
|
@ -97,7 +101,10 @@ public class MessageListItem extends RelativeLayout {
|
|||
|
||||
case MotionEvent.ACTION_UP:
|
||||
if (mDownEvent) {
|
||||
if (touchX > mStarLeft) {
|
||||
if (touchX < mCheckRight) {
|
||||
mAdapter.toggleSelected(this);
|
||||
handled = true;
|
||||
} else if (touchX > mStarLeft) {
|
||||
mFavorite = !mFavorite;
|
||||
mAdapter.updateFavorite(this, mFavorite);
|
||||
handled = true;
|
||||
|
|
|
@ -80,6 +80,8 @@ import java.util.Set;
|
|||
private final Drawable mInvitationIcon;
|
||||
private final Drawable mFavoriteIconOn;
|
||||
private final Drawable mFavoriteIconOff;
|
||||
private final Drawable mSelectedIconOn;
|
||||
private final Drawable mSelectedIconOff;
|
||||
|
||||
private final ColorStateList mTextColorPrimary;
|
||||
private final ColorStateList mTextColorSecondary;
|
||||
|
@ -91,7 +93,10 @@ import java.util.Set;
|
|||
private final java.text.DateFormat mDateFormat;
|
||||
private final java.text.DateFormat mTimeFormat;
|
||||
|
||||
private final HashSet<Long> mSelected = new HashSet<Long>();
|
||||
/**
|
||||
* Set of seleced message IDs. Note for performac{@link MessageListItem
|
||||
*/
|
||||
private final HashSet<Long> mSelectedSet = new HashSet<Long>();
|
||||
|
||||
/**
|
||||
* Callback from MessageListAdapter. All methods are called on the UI thread.
|
||||
|
@ -106,14 +111,8 @@ import java.util.Set;
|
|||
|
||||
private final Callback mCallback;
|
||||
|
||||
/**
|
||||
* Used to call callbacks in the UI thread.
|
||||
*/
|
||||
private final Handler mHandler;
|
||||
|
||||
public MessagesAdapter(Context context, Handler handler, Callback callback) {
|
||||
public MessagesAdapter(Context context, Callback callback) {
|
||||
super(context.getApplicationContext(), null, 0 /* no auto requery */);
|
||||
mHandler = handler;
|
||||
mCallback = callback;
|
||||
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
|
||||
|
@ -122,6 +121,8 @@ import java.util.Set;
|
|||
mInvitationIcon = resources.getDrawable(R.drawable.ic_calendar_event_small);
|
||||
mFavoriteIconOn = resources.getDrawable(R.drawable.btn_star_big_buttonless_dark_on);
|
||||
mFavoriteIconOff = resources.getDrawable(R.drawable.btn_star_big_buttonless_dark_off);
|
||||
mSelectedIconOn = resources.getDrawable(R.drawable.btn_check_buttonless_dark_on);
|
||||
mSelectedIconOff = resources.getDrawable(R.drawable.btn_check_buttonless_dark_off);
|
||||
|
||||
Theme theme = context.getTheme();
|
||||
TypedArray array;
|
||||
|
@ -153,11 +154,11 @@ import java.util.Set;
|
|||
}
|
||||
|
||||
public Set<Long> getSelectedSet() {
|
||||
return mSelected;
|
||||
return mSelectedSet;
|
||||
}
|
||||
|
||||
public boolean isSelected(MessageListItem itemView) {
|
||||
return mSelected.contains(itemView.mMessageId);
|
||||
return mSelectedSet.contains(itemView.mMessageId);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -218,6 +219,7 @@ import java.util.Set;
|
|||
R.drawable.message_list_item_background_unread));
|
||||
}
|
||||
|
||||
updateCheckBox(itemView);
|
||||
ImageView favoriteView = (ImageView) view.findViewById(R.id.favorite);
|
||||
favoriteView.setImageDrawable(itemView.mFavorite ? mFavoriteIconOn : mFavoriteIconOff);
|
||||
updateBackgroundColor(itemView);
|
||||
|
@ -228,6 +230,15 @@ import java.util.Set;
|
|||
return mInflater.inflate(R.layout.message_list_item, parent, false);
|
||||
}
|
||||
|
||||
private void updateCheckBox(MessageListItem itemView) {
|
||||
ImageView selectedView = (ImageView) itemView.findViewById(R.id.selected);
|
||||
selectedView.setImageDrawable(isSelected(itemView) ? mSelectedIconOn : mSelectedIconOff);
|
||||
}
|
||||
|
||||
public void toggleSelected(MessageListItem itemView) {
|
||||
updateSelected(itemView, !isSelected(itemView));
|
||||
}
|
||||
|
||||
/**
|
||||
* This is used as a callback from the list items, to set the selected state
|
||||
*
|
||||
|
@ -236,17 +247,16 @@ import java.util.Set;
|
|||
* @param itemView the item being changed
|
||||
* @param newSelected the new value of the selected flag (checkbox state)
|
||||
*/
|
||||
public void updateSelected(MessageListItem itemView, boolean newSelected) {
|
||||
// Set checkbox state in list, and show/hide panel if necessary
|
||||
Long id = Long.valueOf(itemView.mMessageId);
|
||||
private void updateSelected(MessageListItem itemView, boolean newSelected) {
|
||||
if (newSelected) {
|
||||
mSelected.add(id);
|
||||
mSelectedSet.add(itemView.mMessageId);
|
||||
} else {
|
||||
mSelected.remove(id);
|
||||
mSelectedSet.remove(itemView.mMessageId);
|
||||
}
|
||||
updateCheckBox(itemView);
|
||||
updateBackgroundColor(itemView);
|
||||
if (mCallback != null) {
|
||||
mCallback.onAdapterSelectedChanged(itemView, newSelected, mSelected.size());
|
||||
mCallback.onAdapterSelectedChanged(itemView, newSelected, mSelectedSet.size());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue