|
|
/*
|
|
|
* 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net)
|
|
|
* 根据 Apache 许可证 2.0 版本("许可证")授权;
|
|
|
* 除非符合许可证的规定,否则不得使用本文件。
|
|
|
* 您可以从以下网址获取许可证副本:
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
* 除非适用法律要求或书面同意,本软件按"原样"分发,
|
|
|
* 没有任何明示或暗示的保证或条件。
|
|
|
* 详见许可证中规定的权限和限制。
|
|
|
* (注:这是一份标准的Apache许可证2.0版本的开源声明)
|
|
|
*/
|
|
|
|
|
|
// 声明当前Java文件所在的包路径(net.micode.notes.ui 表示笔记应用的UI层)
|
|
|
package net.micode.notes.ui;
|
|
|
// Android注解库,@SuppressLint用于忽略特定Lint警告
|
|
|
import android.annotation.SuppressLint;
|
|
|
// Android上下文类,提供应用环境信息和访问系统服务
|
|
|
import android.content.Context;
|
|
|
// Android日期/时间格式化工具类,用于友好显示时间
|
|
|
import android.text.format.DateUtils;
|
|
|
// Android基础视图类,所有UI组件的基类
|
|
|
import android.view.View;
|
|
|
// 复选框控件,用于多选操作
|
|
|
import android.widget.CheckBox;
|
|
|
// 图片视图控件,用于显示图标
|
|
|
import android.widget.ImageView;
|
|
|
// 线性布局容器,用于水平或垂直排列子视图
|
|
|
import android.widget.LinearLayout;
|
|
|
// 文本视图控件,用于显示文字内容
|
|
|
import android.widget.TextView;
|
|
|
// 导入应用R资源文件(自动生成,包含所有资源ID)
|
|
|
import net.micode.notes.R;
|
|
|
// 导入笔记数据模型和常量定义
|
|
|
import net.micode.notes.data.Notes;
|
|
|
// 导入数据工具类,提供数据处理方法
|
|
|
import net.micode.notes.tool.DataUtils;
|
|
|
// 导入笔记项背景资源定义类
|
|
|
import net.micode.notes.tool.ResourceParser.NoteItemBgResources;
|
|
|
|
|
|
/**
|
|
|
* 自定义笔记列表项视图组件
|
|
|
* 继承自LinearLayout,展示单条笔记/文件夹的完整信息
|
|
|
*/
|
|
|
public class NotesListItem extends LinearLayout {
|
|
|
// 提醒图标(显示时钟或通话记录图标)
|
|
|
private final ImageView mAlert;
|
|
|
// 标题/内容文本视图
|
|
|
private final TextView mTitle;
|
|
|
// 时间文本视图(显示相对时间)
|
|
|
private final TextView mTime;
|
|
|
// 通话记录联系人名称(仅通话记录可见)
|
|
|
private final TextView mCallName;
|
|
|
// 当前绑定的笔记数据对象
|
|
|
private NoteItemData mItemData;
|
|
|
// 多选模式下的复选框
|
|
|
private final CheckBox mCheckBox;
|
|
|
|
|
|
/**
|
|
|
* 构造函数
|
|
|
* @param context 上下文环境
|
|
|
*/
|
|
|
public NotesListItem(Context context) {
|
|
|
super(context);
|
|
|
// 加载布局文件(R.layout.note_item )到当前视图
|
|
|
inflate(context, R.layout.note_item, this);
|
|
|
|
|
|
// 初始化视图组件
|
|
|
mAlert = findViewById(R.id.iv_alert_icon); // 提醒图标
|
|
|
mTitle = findViewById(R.id.tv_title); // 标题文本
|
|
|
mTime = findViewById(R.id.tv_time); // 时间文本
|
|
|
mCallName = findViewById(R.id.tv_name); // 联系人名称
|
|
|
mCheckBox = findViewById(android.R.id.checkbox);// 多选复选框
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 绑定数据到视图
|
|
|
* @param context 上下文
|
|
|
* @param data 笔记数据对象
|
|
|
* @param choiceMode 是否处于多选模式
|
|
|
* @param checked 是否被选中
|
|
|
*/
|
|
|
@SuppressLint("SetTextI18n") // 忽略国际化的Lint警告
|
|
|
public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) {
|
|
|
// 处理多选模式显示逻辑
|
|
|
if (choiceMode && data.getType() == Notes.TYPE_NOTE) {
|
|
|
mCheckBox.setVisibility(View.VISIBLE); // 显示复选框
|
|
|
mCheckBox.setChecked(checked); // 设置选中状态
|
|
|
} else {
|
|
|
mCheckBox.setVisibility(View.GONE); // 隐藏复选框
|
|
|
}
|
|
|
|
|
|
mItemData = data; // 保存数据引用
|
|
|
|
|
|
// 判断笔记类型并设置不同显示样式
|
|
|
if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) {
|
|
|
// 通话记录文件夹的特殊处理
|
|
|
mCallName.setVisibility(View.GONE); // 隐藏联系人名称
|
|
|
mAlert.setVisibility(View.VISIBLE); // 显示提醒图标
|
|
|
// 设置标题样式和内容
|
|
|
mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem);
|
|
|
mTitle.setText(context.getString(R.string.call_record_folder_name)
|
|
|
+ context.getString(R.string.format_folder_files_count, data.getNotesCount()));
|
|
|
mAlert.setImageResource(R.drawable.call_record); // 设置通话记录图标
|
|
|
} else if (data.getParentId() == Notes.ID_CALL_RECORD_FOLDER) {
|
|
|
// 通话记录笔记的特殊处理
|
|
|
mCallName.setVisibility(View.VISIBLE); // 显示联系人名称
|
|
|
mCallName.setText(data.getCallName()); // 设置联系人名称
|
|
|
// 设置次级标题样式
|
|
|
mTitle.setTextAppearance(context, R.style.TextAppearanceSecondaryItem);
|
|
|
mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet())); // 设置内容摘要
|
|
|
// 处理提醒图标显示
|
|
|
if (data.hasAlert()) {
|
|
|
mAlert.setImageResource(R.drawable.clock); // 设置时钟图标
|
|
|
mAlert.setVisibility(View.VISIBLE);
|
|
|
} else {
|
|
|
mAlert.setVisibility(View.GONE);
|
|
|
}
|
|
|
} else {
|
|
|
// 普通笔记/文件夹的处理
|
|
|
mCallName.setVisibility(View.GONE); // 隐藏联系人名称
|
|
|
// 设置主标题样式
|
|
|
mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem);
|
|
|
|
|
|
if (data.getType() == Notes.TYPE_FOLDER) {
|
|
|
// 文件夹的特殊处理
|
|
|
mTitle.setText(data.getSnippet() + context.getString(R.string.format_folder_files_count,
|
|
|
data.getNotesCount())); // 显示文件夹名+笔记数
|
|
|
mAlert.setVisibility(View.GONE); // 隐藏提醒图标
|
|
|
} else {
|
|
|
// 普通笔记的处理
|
|
|
mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet())); // 设置内容摘要
|
|
|
// 处理提醒图标显示
|
|
|
if (data.hasAlert()) {
|
|
|
mAlert.setImageResource(R.drawable.clock); // 设置时钟图标
|
|
|
mAlert.setVisibility(View.VISIBLE);
|
|
|
} else {
|
|
|
mAlert.setVisibility(View.GONE);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 设置相对时间显示(如"2分钟前")
|
|
|
mTime.setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate()));
|
|
|
|
|
|
// 设置背景样式
|
|
|
setBackground(data);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据笔记位置设置不同背景样式
|
|
|
* @param data 笔记数据对象
|
|
|
*/
|
|
|
private void setBackground(NoteItemData data) {
|
|
|
int id = data.getBgColorId(); // 获取背景颜色ID
|
|
|
if (data.getType() == Notes.TYPE_NOTE) {
|
|
|
// 笔记项的背景处理
|
|
|
if (data.isSingle() || data.isOneFollowingFolder()) {
|
|
|
// 单一项或紧接文件夹的项
|
|
|
setBackgroundResource(NoteItemBgResources.getNoteBgSingleRes(id));
|
|
|
} else if (data.isLast()) {
|
|
|
// 最后一项
|
|
|
setBackgroundResource(NoteItemBgResources.getNoteBgLastRes(id));
|
|
|
} else if (data.isFirst() || data.isMultiFollowingFolder()) {
|
|
|
// 第一项或跟随多文件夹的项
|
|
|
setBackgroundResource(NoteItemBgResources.getNoteBgFirstRes(id));
|
|
|
} else {
|
|
|
// 普通中间项
|
|
|
setBackgroundResource(NoteItemBgResources.getNoteBgNormalRes(id));
|
|
|
}
|
|
|
} else {
|
|
|
// 文件夹项的背景
|
|
|
setBackgroundResource(NoteItemBgResources.getFolderBgRes());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取当前绑定的笔记数据
|
|
|
* @return 笔记数据对象
|
|
|
*/
|
|
|
public NoteItemData getItemData() {
|
|
|
return mItemData;
|
|
|
}
|
|
|
}
|