From c4c350c81c662f2702aaa5b13b7c300bc97be908 Mon Sep 17 00:00:00 2001 From: MikkoAyaka <3401286177@qq.com> Date: Sun, 9 Apr 2023 14:43:04 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=20NotesListAdapter=20?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/micode/notes/ui/NotesListAdapter.java | 73 ++++++++++++++++++- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/net/micode/notes/ui/NotesListAdapter.java b/app/src/main/java/net/micode/notes/ui/NotesListAdapter.java index 0593aa4..05b1500 100644 --- a/app/src/main/java/net/micode/notes/ui/NotesListAdapter.java +++ b/app/src/main/java/net/micode/notes/ui/NotesListAdapter.java @@ -30,7 +30,9 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; - +/** + * 便签列表适配器 + */ public class NotesListAdapter extends CursorAdapter { private static final String TAG = "NotesListAdapter"; private Context mContext; @@ -38,6 +40,9 @@ public class NotesListAdapter extends CursorAdapter { private int mNotesCount; private boolean mChoiceMode; + /** + * APP页面属性类 + */ public static class AppWidgetAttribute { public int widgetId; public int widgetType; @@ -45,18 +50,35 @@ public class NotesListAdapter extends CursorAdapter { ; + /** + * 构造器 + * @param context 便签列表上下文环境 + */ public NotesListAdapter(Context context) { super(context, null); - mSelectedIndex = new HashMap(); + mSelectedIndex = new HashMap<>(); mContext = context; mNotesCount = 0; } + /** + * 新建便签列表视图的方法 + * @param context 上下文环境 + * @param cursor 数据库指针 + * @param parent 视图父类 + * @return + */ @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return new NotesListItem(context); } + /** + * 视图绑定 + * @param view 视图 + * @param context 上下文 + * @param cursor 数据库指针 + */ @Override public void bindView(View view, Context context, Cursor cursor) { if (view instanceof NotesListItem) { @@ -66,20 +88,37 @@ public class NotesListAdapter extends CursorAdapter { } } + /** + * 设置子项选中状态 + * @param position 子项的索引 + * @param checked 是否选中 + */ public void setCheckedItem(final int position, final boolean checked) { mSelectedIndex.put(position, checked); notifyDataSetChanged(); } + /** + * 是否在决策模式 + * @return 结果 + */ public boolean isInChoiceMode() { return mChoiceMode; } + /** + * 设置决策模式是否启用 + * @param mode 是否启用 + */ public void setChoiceMode(boolean mode) { mSelectedIndex.clear(); mChoiceMode = mode; } + /** + * 选择全部便签 + * @param checked 选中/取消选择 + */ public void selectAll(boolean checked) { Cursor cursor = getCursor(); for (int i = 0; i < getCount(); i++) { @@ -91,6 +130,10 @@ public class NotesListAdapter extends CursorAdapter { } } + /** + * 获取当前选定的所有标签项的Id + * @return + */ public HashSet getSelectedItemIds() { HashSet itemSet = new HashSet(); for (Integer position : mSelectedIndex.keySet()) { @@ -107,6 +150,9 @@ public class NotesListAdapter extends CursorAdapter { return itemSet; } + /** + * 获取选中的界面 + */ public HashSet getSelectedWidget() { HashSet itemSet = new HashSet(); for (Integer position : mSelectedIndex.keySet()) { @@ -130,6 +176,10 @@ public class NotesListAdapter extends CursorAdapter { return itemSet; } + /** + * 获取当前选中的数量 + * @return 数量 + */ public int getSelectedCount() { Collection values = mSelectedIndex.values(); if (null == values) { @@ -145,11 +195,20 @@ public class NotesListAdapter extends CursorAdapter { return count; } + /** + * 标签是否全部选中 + * @return 是否全选 + */ public boolean isAllSelected() { int checkedCount = getSelectedCount(); return (checkedCount != 0 && checkedCount == mNotesCount); } + /** + * 是否选择了指定索引的便签项 + * @param position + * @return + */ public boolean isSelectedItem(final int position) { if (null == mSelectedIndex.get(position)) { return false; @@ -157,18 +216,28 @@ public class NotesListAdapter extends CursorAdapter { return mSelectedIndex.get(position); } + /** + * 在内容改变时调用该方法重新计算便签数量 + */ @Override protected void onContentChanged() { super.onContentChanged(); calcNotesCount(); } + /** + * 修改数据库指针然后重新计算标签项 + * @param cursor 新的指针 + */ @Override public void changeCursor(Cursor cursor) { super.changeCursor(cursor); calcNotesCount(); } + /** + * 计算便签数量 + */ private void calcNotesCount() { mNotesCount = 0; for (int i = 0; i < getCount(); i++) {