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.
xiaomi/ui/NotesListItem.java

138 lines
6.2 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.
*/
// 包声明,指明这个类属于哪个包
package net.micode.notes.ui;
// 导入所需的Android SDK类和包
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;
// 导入Notes应用特有的资源和数据类
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 {
// 类的成员变量
private ImageView mAlert; // 用于显示提醒图标的ImageView
private TextView mTitle; // 显示标题的TextView
private TextView mTime; // 显示时间的TextView
private TextView mCallName; // 显示来电姓名的TextView
private NoteItemData mItemData; // 包含笔记数据的NoteItemData对象
private CheckBox mCheckBox; // 用于选择模式的CheckBox
// 构造函数,初始化视图
public NotesListItem(Context context) {
super(context); // 调用父类的构造函数
inflate(context, R.layout.note_item, this); // 将布局文件inflate到当前视图中
mAlert = (ImageView) findViewById(R.id.iv_alert_icon); // 获取布局中的ImageView
mTitle = (TextView) findViewById(R.id.tv_title); // 获取布局中的TextView
mTime = (TextView) findViewById(R.id.tv_time); // 获取布局中的TextView
mCallName = (TextView) findViewById(R.id.tv_name); // 获取布局中的TextView
mCheckBox = (CheckBox) findViewById(android.R.id.checkbox); // 获取布局中的CheckBox
}
// bind方法用于将数据绑定到视图上
public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) {
// 根据是否处于选择模式和笔记类型设置CheckBox的可见性和选中状态
if (choiceMode && data.getType() == Notes.TYPE_NOTE) {
mCheckBox.setVisibility(View.VISIBLE);
mCheckBox.setChecked(checked);
} else {
mCheckBox.setVisibility(View.GONE);
}
mItemData = data; // 保存NoteItemData对象
// 根据笔记数据的不同类型,设置不同的视图显示
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);
}
// setBackground方法用于设置视图的背景
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());
}
}
// getItemData方法用于获取当前项的NoteItemData对象
public NoteItemData getItemData() {
return mItemData;
}
}