|
|
/*
|
|
|
* 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);
|
|
|
// 从布局文件note_item.xml填充视图
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
// 设置相对时间显示(如"2分钟前")
|
|
|
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 NoteItemData对象
|
|
|
*/
|
|
|
public NoteItemData getItemData() {
|
|
|
return mItemData;
|
|
|
}
|
|
|
}
|