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.
123456/java/net/micode/notes/ui/NotesListItem.java

155 lines
9.7 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.
* 总体分析
这段 Java 代码定义了NotesListItem类它继承自LinearLayout是用于展示笔记列表中每一项具体内容的自定义视图类。通过绑定NoteItemData类型的数据根据笔记不同的类型如通话记录文件夹、普通文件夹、普通笔记等以及相关属性是否有提醒、是否是首项、末项等来设置各个子视图如TextView、ImageView、CheckBox等的显示内容和可见性并设置该项的背景样式以此实现个性化的笔记列表项展示效果同时提供了获取所绑定数据的方法方便外部获取对应笔记信息。
函数分析
构造函数
NotesListItem(Context context)
所属类NotesListItem
功能:
首先调用父类LinearLayout的构造函数传入Context完成基础的初始化。
通过inflate方法加载名为R.layout.note_item的布局文件到自身该布局文件应该定义了笔记列表项的具体外观结构包含了如提醒图标、标题、时间、联系人姓名、复选框等子视图的布局设置。
接着使用findViewById方法从加载的布局中找到对应的子视图控件并赋值给相应的成员变量如mAlert、mTitle等方便后续在bind方法中对这些子视图进行内容设置等操作。
数据绑定函数
bind(Context context, NoteItemData data, boolean choiceMode, boolean checked)
所属类NotesListItem
功能:
根据传入的选择模式choiceMode以及笔记类型通过data.getType()判断是否为Notes.TYPE_NOTE来设置复选框mCheckBox的可见性和选中状态。如果处于选择模式且是普通笔记类型则将复选框设为可见并按照传入的checked参数设置其选中状态否则将其隐藏。
将传入的NoteItemData实例赋值给mItemData成员变量用于后续可能的获取数据操作。
接着依据笔记的不同情况进行具体的视图内容设置:
如果笔记的ID是通话记录文件夹的IDNotes.ID_CALL_RECORD_FOLDER则隐藏联系人姓名视图mCallName显示提醒图标mAlert设置标题的文本外观样式通过setTextAppearance方法并设置标题文本为通话记录文件夹名称加上包含笔记数量的格式化字符串同时设置提醒图标的资源为通话记录对应的图标R.drawable.call_record
如果笔记的父ID是通话记录文件夹的ID则显示联系人姓名视图并设置其文本为笔记关联的联系人姓名通过data.getCallName()获取设置标题文本外观样式并将标题文本设置为格式化后的笔记内容片段通过DataUtils.getFormattedSnippet方法获取再根据笔记是否有提醒通过data.hasAlert()判断)来设置提醒图标的可见性和对应图标资源(有提醒则显示时钟图标)。
对于其他情况如果是普通文件夹类型data.getType() == Notes.TYPE_FOLDER则隐藏联系人姓名视图设置标题文本外观样式并将标题文本设置为笔记内容片段加上包含笔记数量的格式化字符串同时隐藏提醒图标如果是普通笔记类型则设置标题文本外观样式并将标题文本设置为格式化后的笔记内容片段再根据是否有提醒来设置提醒图标的可见性和对应图标资源。
最后通过mTime.setText方法设置时间文本为根据笔记修改日期通过data.getModifiedDate()获取格式化后的相对时间字符串通过DateUtils.getRelativeTimeSpanString方法获取并调用setBackground方法根据笔记数据来设置该项的背景样式完成整个笔记列表项的内容和样式设置。
背景设置函数
setBackground(NoteItemData data)
所属类NotesListItem
功能:
首先获取笔记数据中的背景颜色ID通过data.getBgColorId())。
如果笔记类型是普通笔记类型data.getType() == Notes.TYPE_NOTE则进一步根据笔记在列表中的位置关系是否是单个、首项、末项、跟随文件夹等情况通过data.isSingle()、data.isFirst()等方法判断来设置该项的背景资源分别调用NoteItemBgResources类中对应的获取背景资源方法如getNoteBgSingleRes、getNoteBgLastRes等来获取相应的背景资源并设置给自身通过setBackgroundResource方法实现不同位置普通笔记的差异化背景显示效果。
如果笔记类型不是普通笔记类型即其他类型比如文件夹类型则统一调用NoteItemBgResources.getFolderBgRes方法获取文件夹对应的背景资源并设置给自身确保文件夹有对应的合适背景样式。
获取数据函数
getItemData()
所属类NotesListItem
功能返回成员变量mItemData外部代码可以通过调用这个方法获取当前NotesListItem所绑定的NoteItemData实例进而获取笔记的各项详细信息方便在其他业务逻辑中使用这些数据比如获取笔记ID、内容片段等信息。
*/
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;
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 = (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);
}
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);
}
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());
}
}
public NoteItemData getItemData() {
return mItemData;
}
}