|
|
/*
|
|
|
* 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.
|
|
|
*/
|
|
|
|
|
|
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 {
|
|
|
// 用于显示提醒图标的 ImageView
|
|
|
private ImageView mAlert;
|
|
|
// 用于显示标题的 TextView
|
|
|
private TextView mTitle;
|
|
|
// 用于显示修改时间的 TextView
|
|
|
private TextView mTime;
|
|
|
// 用于显示通话记录中联系人姓名的 TextView
|
|
|
private TextView mCallName;
|
|
|
// 存储当前笔记项数据的对象
|
|
|
private NoteItemData mItemData;
|
|
|
// 用于选择笔记项的 CheckBox
|
|
|
private CheckBox mCheckBox;
|
|
|
|
|
|
/**
|
|
|
* 构造函数,初始化 NotesListItem 视图。
|
|
|
*
|
|
|
* @param context 上下文对象
|
|
|
*/
|
|
|
public NotesListItem(Context context) {
|
|
|
super(context);
|
|
|
// 将 note_item 布局文件填充到当前视图中
|
|
|
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) {
|
|
|
// 显示 CheckBox 并设置其选中状态
|
|
|
mCheckBox.setVisibility(View.VISIBLE);
|
|
|
mCheckBox.setChecked(checked);
|
|
|
} else {
|
|
|
// 否则隐藏 CheckBox
|
|
|
mCheckBox.setVisibility(View.GONE);
|
|
|
}
|
|
|
|
|
|
// 保存当前笔记项数据
|
|
|
mItemData = data;
|
|
|
// 如果笔记项的 ID 为通话记录文件夹的 ID
|
|
|
if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) {
|
|
|
// 隐藏通话记录联系人姓名的 TextView
|
|
|
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) {
|
|
|
// 显示通话记录联系人姓名的 TextView 并设置其文本
|
|
|
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 {
|
|
|
// 隐藏通话记录联系人姓名的 TextView
|
|
|
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;
|
|
|
}
|
|
|
} |