|
|
/*
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
*
|
|
|
* 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.
|
|
|
*/
|
|
|
|
|
|
// 声明包名,该类位于 net.micode.notes.ui 包下
|
|
|
package net.micode.notes.ui;
|
|
|
|
|
|
import android.content.Context;
|
|
|
// 用于格式化日期,将日期转换为相对时间字符串,如“几分钟前”“几小时前”等
|
|
|
import android.text.format.DateUtils;
|
|
|
import android.view.View;
|
|
|
import android.widget.CheckBox;
|
|
|
import android.widget.ImageView;
|
|
|
import android.widget.LinearLayout;
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
import net.micode.notes.data.Notes;
|
|
|
import net.micode.notes.tool.DataUtils;
|
|
|
import net.micode.notes.tool.ResourceParser.NoteItemBgResources;
|
|
|
|
|
|
/**
|
|
|
* NotesListItem 类继承自 LinearLayout,用于表示笔记列表中的每一项。
|
|
|
* 该类负责初始化视图组件,并根据传入的笔记数据绑定显示内容,同时设置背景。
|
|
|
*/
|
|
|
public class NotesListItem extends LinearLayout {
|
|
|
// 用于显示提醒图标
|
|
|
private ImageView mAlert;
|
|
|
// 用于显示笔记标题或片段内容
|
|
|
private TextView mTitle;
|
|
|
// 用于显示笔记的修改日期
|
|
|
private TextView mTime;
|
|
|
// 用于显示通话记录的联系人姓名
|
|
|
private TextView mCallName;
|
|
|
// 存储当前笔记项的数据
|
|
|
private NoteItemData mItemData;
|
|
|
// 用于选择笔记项的复选框
|
|
|
private CheckBox mCheckBox;
|
|
|
|
|
|
/**
|
|
|
* 构造函数,初始化 NotesListItem 视图。
|
|
|
*
|
|
|
* @param context 上下文对象,用于加载布局和资源
|
|
|
*/
|
|
|
public NotesListItem(Context context) {
|
|
|
super(context);
|
|
|
// 加载 note_item 布局文件到当前 LinearLayout 中
|
|
|
inflate(context, R.layout.note_item, this);
|
|
|
// 通过 findViewById 方法获取布局中的各个视图组件
|
|
|
mAlert = (ImageView) findViewById(R.id.iv_alert_icon);
|
|
|
mTitle = (TextView) findViewById(R.id.tv_title);
|
|
|
mTime = (TextView) findViewById(R.id.tv_time);
|
|
|
mCallName = (TextView) findViewById(R.id.tv_name);
|
|
|
mCheckBox = (CheckBox) findViewById(android.R.id.checkbox);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 绑定笔记数据到视图上,并根据选择模式和选中状态显示复选框。
|
|
|
*
|
|
|
* @param context 上下文对象,用于获取资源和字符串
|
|
|
* @param data 笔记项的数据对象
|
|
|
* @param choiceMode 是否处于选择模式
|
|
|
* @param checked 该项是否被选中
|
|
|
*/
|
|
|
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;
|
|
|
// 如果笔记 ID 为通话记录文件夹的 ID
|
|
|
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);
|
|
|
}
|
|
|
// 如果笔记的父 ID 为通话记录文件夹的 ID
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
// 设置笔记的修改日期为相对时间字符串
|
|
|
mTime.setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate()));
|
|
|
|
|
|
// 根据笔记数据设置背景
|
|
|
setBackground(data);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据笔记数据设置背景资源。
|
|
|
*
|
|
|
* @param data 笔记项的数据对象
|
|
|
*/
|
|
|
private void setBackground(NoteItemData data) {
|
|
|
// 获取笔记的背景颜色 ID
|
|
|
int id = data.getBgColorId();
|
|
|
// 如果笔记类型为普通笔记
|
|
|
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;
|
|
|
}
|
|
|
} |