|
|
/*
|
|
|
* 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实例,传入上下文信息(用于获取资源等操作)
|
|
|
public NotesListItem(Context context) {
|
|
|
super(context);
|
|
|
// 通过inflate方法将布局资源(R.layout.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);
|
|
|
}
|
|
|
|
|
|
// 用于将数据绑定到视图组件上,根据传入的不同数据和状态参数来设置各个视图组件的显示内容和可见性等属性
|
|
|
public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) {
|
|
|
// 如果处于选择模式(choiceMode为true)并且数据类型是笔记(Notes.TYPE_NOTE)
|
|
|
if (choiceMode && data.getType() == Notes.TYPE_NOTE) {
|
|
|
// 将复选框设置为可见,以便用户可以看到并操作选中状态
|
|
|
mCheckBox.setVisibility(View.VISIBLE);
|
|
|
// 根据传入的checked参数设置复选框的选中状态
|
|
|
mCheckBox.setChecked(checked);
|
|
|
} else {
|
|
|
// 如果不满足上述条件,则将复选框隐藏起来
|
|
|
mCheckBox.setVisibility(View.GONE);
|
|
|
}
|
|
|
|
|
|
// 保存传入的笔记数据对象,方便后续获取数据进行展示等操作
|
|
|
mItemData = data;
|
|
|
|
|
|
// 如果数据项的ID是通话记录文件夹的ID(Notes.ID_CALL_RECORD_FOLDER)
|
|
|
if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) {
|
|
|
// 隐藏通话名称相关的TextView,因为在通话记录文件夹情况下可能不需要显示这个信息
|
|
|
mCallName.setVisibility(View.GONE);
|
|
|
// 将提醒图标设置为可见,可能用于表示该文件夹有特殊的提醒相关属性或者状态
|
|
|
mAlert.setVisibility(View.VISIBLE);
|
|
|
// 设置标题的文本外观样式,通过资源ID指定(R.style.TextAppearancePrimaryItem)
|
|
|
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()));
|
|
|
// 设置提醒图标的资源图片,这里使用通话记录相关的图标(R.drawable.call_record)
|
|
|
mAlert.setImageResource(R.drawable.call_record);
|
|
|
} else if (data.getParentId() == Notes.ID_CALL_RECORD_FOLDER) {
|
|
|
// 如果数据项的父ID是通话记录文件夹的ID,说明该数据项可能是通话记录文件夹下的具体内容
|
|
|
mCallName.setVisibility(View.VISIBLE);
|
|
|
// 设置通话名称TextView的文本内容为数据中获取的通话名称
|
|
|
mCallName.setText(data.getCallName());
|
|
|
// 设置标题的文本外观样式,通过资源ID指定(R.style.TextAppearanceSecondaryItem)
|
|
|
mTitle.setTextAppearance(context, R.style.TextAppearanceSecondaryItem);
|
|
|
// 设置标题的文本内容为格式化后的摘要信息(通过DataUtils工具类的方法进行格式化)
|
|
|
mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet()));
|
|
|
// 如果数据项有提醒设置(通过hasAlert方法判断)
|
|
|
if (data.hasAlert()) {
|
|
|
// 设置提醒图标的资源图片为闹钟图标(R.drawable.clock),并将其设置为可见
|
|
|
mAlert.setImageResource(R.drawable.clock);
|
|
|
mAlert.setVisibility(View.VISIBLE);
|
|
|
} else {
|
|
|
// 如果没有提醒设置,则隐藏提醒图标
|
|
|
mAlert.setVisibility(View.GONE);
|
|
|
}
|
|
|
} else {
|
|
|
// 对于其他情况(既不是通话记录文件夹本身,也不是其下的具体内容)
|
|
|
mCallName.setVisibility(View.GONE);
|
|
|
// 设置标题的文本外观样式,通过资源ID指定(R.style.TextAppearancePrimaryItem)
|
|
|
mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem);
|
|
|
|
|
|
// 如果数据类型是文件夹(Notes.TYPE_FOLDER)
|
|
|
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()));
|
|
|
// 如果数据项有提醒设置(通过hasAlert方法判断)
|
|
|
if (data.hasAlert()) {
|
|
|
// 设置提醒图标的资源图片为闹钟图标(R.drawable.clock),并将其设置为可见
|
|
|
mAlert.setImageResource(R.drawable.clock);
|
|
|
mAlert.setVisibility(View.VISIBLE);
|
|
|
} else {
|
|
|
// 如果没有提醒设置,则隐藏提醒图标
|
|
|
mAlert.setVisibility(View.GONE);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 设置时间TextView的文本内容,通过DateUtils工具类的方法将数据中的修改时间转换为相对时间格式(例如“几分钟前”“昨天”等相对时间表述)进行展示
|
|
|
mTime.setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate()));
|
|
|
|
|
|
// 调用setBackground方法根据数据设置当前列表项的背景样式,具体的背景设置逻辑在该方法中实现
|
|
|
setBackground(data);
|
|
|
}
|
|
|
|
|
|
// 私有方法,用于根据笔记数据来设置当前列表项的背景资源,根据数据类型(是否为笔记以及笔记的不同状态等)来选择不同的背景资源进行设置
|
|
|
private void setBackground(NoteItemData data) {
|
|
|
int id = data.getBgColorId();
|
|
|
// 如果数据类型是笔记(Notes.TYPE_NOTE)
|
|
|
if (data.getType() == Notes.TYPE_NOTE) {
|
|
|
// 如果笔记是单独的(isSingle方法判断)或者是某个文件夹下单独跟随的(isOneFollowingFolder方法判断)
|
|
|
if (data.isSingle() || data.isOneFollowingFolder()) {
|
|
|
// 设置背景资源为单个笔记对应的背景资源(通过NoteItemBgResources工具类的方法根据颜色ID获取)
|
|
|
setBackgroundResource(NoteItemBgResources.getNoteBgSingleRes(id));
|
|
|
} else if (data.isLast()) {
|
|
|
// 如果笔记是所在分组的最后一个(isLast方法判断)
|
|
|
setBackgroundResource(NoteItemBgResources.getNoteBgLastRes(id));
|
|
|
} else if (data.isFirst() || data.isMultiFollowingFolder()) {
|
|
|
// 如果笔记是所在分组的第一个(isFirst方法判断)或者是某个文件夹下多个跟随的(isMultiFollowingFolder方法判断)
|
|
|
setBackgroundResource(NoteItemBgResources.getNoteBgFirstRes(id));
|
|
|
} else {
|
|
|
// 其他情况(普通的笔记在中间位置等),设置背景资源为普通笔记对应的背景资源(通过NoteItemBgResources工具类的方法根据颜色ID获取)
|
|
|
setBackgroundResource(NoteItemBgResources.getNoteBgNormalRes(id));
|
|
|
}
|
|
|
} else {
|
|
|
// 如果数据类型不是笔记(比如是文件夹),则设置背景资源为文件夹对应的背景资源(通过NoteItemBgResources工具类的方法获取)
|
|
|
setBackgroundResource(NoteItemBgResources.getFolderBgRes());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 用于获取当前列表项对应的笔记数据对象,方便外部调用者获取数据进行其他相关操作
|
|
|
public NoteItemData getItemData() {
|
|
|
return mItemData;
|
|
|
}
|
|
|
} |