|
|
/*
|
|
|
* 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 是一个自定义视图组件,用于在列表中显示每个笔记项的内容。
|
|
|
* 该视图展示了笔记的标题、时间、是否有提醒、是否有附件等信息。
|
|
|
*/
|
|
|
public class NotesListItem extends LinearLayout {
|
|
|
private ImageView mAlert; // 提醒图标
|
|
|
private TextView mTitle; // 标题
|
|
|
private TextView mTime; // 修改时间
|
|
|
private TextView mCallName; // 通话记录联系人名(如果是通话记录项)
|
|
|
private NoteItemData mItemData; // 当前项的数据
|
|
|
private CheckBox mCheckBox; // 复选框,用于选择项
|
|
|
|
|
|
/**
|
|
|
* 构造函数,初始化视图。
|
|
|
* @param context 上下文
|
|
|
*/
|
|
|
public NotesListItem(Context context) {
|
|
|
super(context);
|
|
|
inflate(context, R.layout.note_item, this); // 加载 note_item 布局
|
|
|
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 当前项的数据(NoteItemData)
|
|
|
* @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;
|
|
|
|
|
|
// 判断当前项是否为通话记录文件夹
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据不同的笔记类型和状态设置背景。
|
|
|
* @param data 当前项的数据(NoteItemData)
|
|
|
*/
|
|
|
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 当前项的数据(NoteItemData)
|
|
|
*/
|
|
|
public NoteItemData getItemData() {
|
|
|
return mItemData;
|
|
|
}
|
|
|
}
|