/* * 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. */ /*这段代码展示了 NotesListItem 类的实现,它是 Android 应用程序中的一个自定义视图,用于显示笔记列表中的单个条目*/ 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); } /*构造函数初始化了一个 LinearLayout 并且使用 inflate 方法加载了布局文件 note_item。此外,还初始化了与笔记条目相关的各个控件,例如图标、标题、时间和复选框等*/ 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); } /*绑定数据 此方法用于将数据绑定到视图组件上。它接收四个参数: context:当前上下文。 data:包含条目信息的 NoteItemData 对象。 choiceMode:指示当前是否处于选择模式。 checked:指示当前条目是否被选中。 方法根据不同的条件设置视图的可见性、文本、背景图像等属性。例如,如果当前是选择模式且条目类型为笔记,则显示复选框并设置其状态。对于不同类型的笔记(文件夹、通话记录等),设置不同的标题文本和图标。最后,更新条目的修改时间显示。*/ 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; } } /*此方法返回当前绑定到该视图的 NoteItemData 对象,使得其他组件可以通过这个方法获取条目的数据*/