|
|
/*
|
|
|
* 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是通话记录文件夹的ID(Notes.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;
|
|
|
}
|
|
|
}
|