/* * 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; /** * 笔记列表项自定义视图,用于显示单个笔记或文件夹项 * 继承自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); // 使用系统预定义的checkbox ID mCheckBox = (CheckBox) findViewById(android.R.id.checkbox); } /** * 将数据绑定到视图上,根据数据类型和状态显示不同的UI * @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; // 根据数据类型显示不同的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); } } } // 设置相对时间显示(如"2分钟前"、"昨天"等) mTime.setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate())); // 根据位置和类型设置背景 setBackground(data); } /** * 根据数据项的位置和类型设置不同的背景 * 实现列表项的分组视觉效果(第一个、最后一个、中间项等) * @param 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()); } } /** * 获取当前绑定的数据对象 * @return NoteItemData对象 */ public NoteItemData getItemData() { return mItemData; } }