You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
7.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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.
*/
/**
* 文件: NotesListItem.java
* 描述: 便签列表项视图组件
* 作用: 提供便签列表中单个项目的视图表现负责数据与UI的绑定
* 功能:
* 1. 显示便签或文件夹的基本信息(标题、时间、提醒图标等)
* 2. 根据便签类型和位置设置不同的背景样式
* 3. 支持多选模式下的勾选功能
* 4. 处理通话记录便签的特殊显示逻辑
*/
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;
/**
* 便签列表项视图类
*
* 继承自LinearLayout用于在列表中显示单个便签或文件夹
* 根据数据类型和状态设置不同的显示效果
* 处理多选模式下的勾选状态
*/
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);
// 初始化视图组件
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;
// 根据不同类型的便签设置不同的显示效果
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 列表项数据对象
*/
private void setBackground(NoteItemData data) {
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;
}
}