From 742f3d8075d586800721081659cf812069a0fbc1 Mon Sep 17 00:00:00 2001 From: STRIV1 <1476836209@qq.com> Date: Mon, 30 Dec 2024 19:41:06 +0800 Subject: [PATCH] =?UTF-8?q?ui=E4=BB=A3=E7=A0=81=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NotesListAdapter.java | 196 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 NotesListAdapter.java diff --git a/NotesListAdapter.java b/NotesListAdapter.java new file mode 100644 index 0000000..6ceb504 --- /dev/null +++ b/NotesListAdapter.java @@ -0,0 +1,196 @@ +/* + * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * + * 根据Apache License, Version 2.0(以下简称“许可证”)授权; + * 您可能不会使用此文件,除非遵守许可证。 + * 您可以在以下网址获得许可证的副本: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 除非适用法律要求或书面同意,否则按照许可证分发的软件 + * 按“原样”分发,不附带任何明示或暗示的保证或条件。 + * 请参阅控制权限和许可证下的限制的具体语言的许可证。 + */ + +package net.micode.notes.ui; + +import android.content.Context; +import android.database.Cursor; +import android.util.Log; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CursorAdapter; + +import net.micode.notes.data.Notes; + +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; + + +// NotesListAdapter 是一个继承自 CursorAdapter 的类,用于为便签列表提供数据和视图。 +public class NotesListAdapter extends CursorAdapter { + private static final String TAG = "NotesListAdapter"; // 日志标签。 + private Context mContext; // 上下文对象。 + private HashMap mSelectedIndex; // 存储选中项的哈希映射。 + private int mNotesCount; // 便签数量。 + private boolean mChoiceMode; // 是否处于选择模式。 + + // AppWidgetAttribute 内部类,用于存储小部件的属性。 + public static class AppWidgetAttribute { + public int widgetId; // 小部件ID。 + public int widgetType; // 小部件类型。 + }; + + // NotesListAdapter 的构造函数。 + public NotesListAdapter(Context context) { + super(context, null); + mSelectedIndex = new HashMap(); + mContext = context; + mNotesCount = 0; + } + + // 新建视图的方法。 + @Override + public View newView(Context context, Cursor cursor, ViewGroup parent) { + return new NotesListItem(context); + } + + // 绑定视图和数据的方法。 + @Override + public void bindView(View view, Context context, Cursor cursor) { + if (view instanceof NotesListItem) { + NoteItemData itemData = new NoteItemData(context, cursor); + ((NotesListItem) view).bind(context, itemData, mChoiceMode, + isSelectedItem(cursor.getPosition())); + } + } + + // 设置项的选中状态。 + public void setCheckedItem(final int position, final boolean checked) { + mSelectedIndex.put(position, checked); + notifyDataSetChanged(); + } + + // 获取是否处于选择模式。 + public boolean isInChoiceMode() { + return mChoiceMode; + } + + // 设置选择模式。 + public void setChoiceMode(boolean mode) { + mSelectedIndex.clear(); + mChoiceMode = mode; + } + + // 全选或全不选。 + public void selectAll(boolean checked) { + Cursor cursor = getCursor(); + for (int i = 0; i < getCount(); i++) { + if (cursor.moveToPosition(i)) { + if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) { + setCheckedItem(i, checked); + } + } + } + } + + // 获取选中项的ID集合。 + public HashSet getSelectedItemIds() { + HashSet itemSet = new HashSet(); + for (Integer position : mSelectedIndex.keySet()) { + if (mSelectedIndex.get(position) == true) { + Long id = getItemId(position); + if (id == Notes.ID_ROOT_FOLDER) { + Log.d(TAG, "Wrong item id, should not happen"); + } else { + itemSet.add(id); + } + } + } + + return itemSet; + } + + // 获取选中的小部件属性集合。 + public HashSet getSelectedWidget() { + HashSet itemSet = new HashSet(); + for (Integer position : mSelectedIndex.keySet()) { + if (mSelectedIndex.get(position) == true) { + Cursor c = (Cursor) getItem(position); + if (c != null) { + AppWidgetAttribute widget = new AppWidgetAttribute(); + NoteItemData item = new NoteItemData(mContext, c); + widget.widgetId = item.getWidgetId(); + widget.widgetType = item.getWidgetType(); + itemSet.add(widget); + } else { + Log.e(TAG, "Invalid cursor"); + return null; + } + } + } + return itemSet; + } + + // 获取选中项的数量。 + public int getSelectedCount() { + Collection values = mSelectedIndex.values(); + if (null == values) { + return 0; + } + Iterator iter = values.iterator(); + int count = 0; + while (iter.hasNext()) { + if (true == iter.next()) { + count++; + } + } + return count; + } + + // 判断是否全部选中。 + public boolean isAllSelected() { + int checkedCount = getSelectedCount(); + return (checkedCount != 0 && checkedCount == mNotesCount); + } + + // 判断项是否被选中。 + public boolean isSelectedItem(final int position) { + if (null == mSelectedIndex.get(position)) { + return false; + } + return mSelectedIndex.get(position); + } + + // 当内容改变时调用。 + @Override + protected void onContentChanged() { + super.onContentChanged(); + calcNotesCount(); + } + + // 当游标改变时调用。 + @Override + public void changeCursor(Cursor cursor) { + super.changeCursor(cursor); + calcNotesCount(); + } + + // 计算便签数量。 + private void calcNotesCount() { + mNotesCount = 0; + for (int i = 0; i < getCount(); i++) { + Cursor c = (Cursor) getItem(i); + if (c != null) { + if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) { + mNotesCount++; + } + } else { + Log.e(TAG, "Invalid cursor"); + return; + } + } + } +} \ No newline at end of file