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.

142 lines
6.0 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;
// 导入必要的包和类
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; // 选择框
// 构造函数,初始化视图
public NotesListItem(Context context) {
super(context);
inflate(context, R.layout.note_item, this); // 加载布局文件
// 初始化视图组件
mAlert = findViewById(R.id.iv_alert_icon);
mTitle = findViewById(R.id.tv_title);
mTime = findViewById(R.id.tv_time);
mCallName = findViewById(R.id.tv_name);
mCheckBox = findViewById(android.R.id.checkbox); // 注意这里使用了Android内置的ID
}
// 绑定数据到视图
public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) {
// 根据choiceMode和数据类型设置CheckBox的可见性
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);
}
// 根据数据设置背景
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());
}
}
// 获取当前项的数据
public NoteItemData getItemData() {
return mItemData;
}
}