|
|
/*
|
|
|
* 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; // Android视图系统的基类
|
|
|
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; // 引用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
|
|
|
public NotesListItem(Context context) {
|
|
|
super(context); // 调用父类的构造函数
|
|
|
inflate(context, R.layout.note_item, this); // 加载布局文件
|
|
|
// 初始化各个控件
|
|
|
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); // 注意这里使用的是Android系统提供的ID
|
|
|
}
|
|
|
|
|
|
// bind方法用于绑定数据和设置UI
|
|
|
public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) {
|
|
|
// 根据choiceMode和数据类型设置复选框的可见性
|
|
|
if (choiceMode && data.getType() == Notes.TYPE_NOTE) {
|
|
|
mCheckBox.setVisibility(View.VISIBLE);
|
|
|
mCheckBox.setChecked(checked);
|
|
|
} else {
|
|
|
mCheckBox.setVisibility(View.GONE);
|
|
|
}
|
|
|
|
|
|
mItemData = data; // 保存数据项
|
|
|
// 根据数据项的不同类型设置UI
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
// 设置时间文本
|
|
|
mTime.setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate()));
|
|
|
|
|
|
// 设置背景
|
|
|
setBackground(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());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 获取当前绑定的数据项
|
|
|
public NoteItemData getItemData() {
|
|
|
return mItemData;
|
|
|
}
|
|
|
} |