/* * 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; 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);//inflate()作用就是将xml定义的一个布局找出来 mAlert = (ImageView) findViewById(R.id.iv_alert_icon);//findViewById用于从contentView中查找指定ID的View,转换出来的形式根据需要而定 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) {//根据data的属性对各个控件的属性的控制, // 主要是可见性Visibility,内容setText,格式setTextAppearance 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) {//设置控件属性,通过判断保存到文件夹的ID、当前ID以及父ID之间关系决定 mCallName.setVisibility(View.GONE);//设置setText 的style 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);//如果父类和当前id均与保存在文件夹中的id不同,设置联系人姓名不可见 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) {//根据data的文件属性来设置背景 int id = data.getBgColorId();//获取id,用此id用来获取背景颜色 if (data.getType() == Notes.TYPE_NOTE) {//若是note型文件,则4种情况,对于4种不同情况的背景来源 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; }//返回当前便签的文件信息 }