|
|
/*
|
|
|
* 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 static net.micode.notes.ui.NotesListAdapter.slideHelper;
|
|
|
|
|
|
import android.content.ContentResolver;
|
|
|
import android.content.ContentValues;
|
|
|
import android.content.Context;
|
|
|
import android.text.format.DateUtils;
|
|
|
import android.view.View;
|
|
|
import android.widget.CheckBox;
|
|
|
import android.widget.ImageButton;
|
|
|
import android.widget.ImageView;
|
|
|
import android.widget.LinearLayout;
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
import com.d.lib.slidelayout.SlideLayout;
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
import net.micode.notes.data.Notes;
|
|
|
import net.micode.notes.tool.DataUtils;
|
|
|
import net.micode.notes.tool.ResourceParser.NoteItemBgResources;
|
|
|
import net.micode.notes.tool.SlideHelper;
|
|
|
import net.micode.notes.tool.onContentClickListener;
|
|
|
|
|
|
public class NotesListItem extends LinearLayout {
|
|
|
private ImageView mAlert;
|
|
|
private TextView mTitle;
|
|
|
private TextView mTime;
|
|
|
private TextView mCallName;
|
|
|
private NoteItemData mItemData;
|
|
|
private CheckBox mCheckBox;
|
|
|
private boolean isOpen = false;//slidelayout特有功能,判断某个位置的item是否打开侧滑
|
|
|
private SlideLayout swipeMenuLayout;//SlideLayout是一个开源Android库,允许开发人员轻松添加SlideMenu功能.
|
|
|
private ImageButton top;//置顶图形按钮
|
|
|
private ImageView complete;
|
|
|
//public static final SlideHelper slideHelper = new SlideHelper();
|
|
|
|
|
|
public NotesListItem(Context context) {
|
|
|
super(context);
|
|
|
inflate(context, R.layout.note_item, this); //Inflate()作用就是将xml定义的一个布局找出来
|
|
|
mAlert = (ImageView) findViewById(R.id.iv_alert_icon);//再用findViewById()找到它上面的其它组件
|
|
|
//侧滑菜单
|
|
|
swipeMenuLayout = findViewById(R.id.swipeMenuLayout);
|
|
|
swipeMenuLayout.setEnable(false); //设置成不可点击
|
|
|
/*置顶按钮*/
|
|
|
top = findViewById(R.id.top);
|
|
|
complete = findViewById(R.id.image_complete);
|
|
|
/*给侧滑菜单设置监听事件*/
|
|
|
swipeMenuLayout.setOnStateChangeListener(new SlideLayout.OnStateChangeListener() {
|
|
|
@Override
|
|
|
public void onStateChanged(SlideLayout layout, boolean open) {
|
|
|
isOpen = open;
|
|
|
/*slideHelper保存当前侧滑菜单对象*/
|
|
|
slideHelper.onStateChanged(layout, open);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean onInterceptTouchEvent(SlideLayout layout) {
|
|
|
/*如果触摸到了当前item的侧滑就关闭其他侧滑*/
|
|
|
slideHelper.closeAll(layout);
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
/*传入onContentClickListener*/
|
|
|
public void setOnContentClick(onContentClickListener contentClick, int position) {
|
|
|
/*给侧滑内容区域添加点击事件*/
|
|
|
LinearLayout content = findViewById(R.id.note_item);
|
|
|
content.setOnClickListener(new View.OnClickListener() {
|
|
|
@Override
|
|
|
public void onClick(View v) { //onclick 事件会在元素被点击时发生
|
|
|
/*如果侧滑是打开状态,就不执行点击事件,同时关闭侧滑*/
|
|
|
if (isOpen) {
|
|
|
swipeMenuLayout.setOpen(false, true);
|
|
|
return;
|
|
|
}
|
|
|
/*接口回调NotesListActivity中的点击事件*/
|
|
|
contentClick.onClick(position, mItemData, NotesListItem.this, position);
|
|
|
}
|
|
|
});
|
|
|
content.setOnLongClickListener(new OnLongClickListener() {
|
|
|
@Override
|
|
|
public boolean onLongClick(View v) {
|
|
|
/*如果侧滑是打开状态,就不执行长按事件,同时关闭侧滑*/
|
|
|
if (isOpen) {
|
|
|
swipeMenuLayout.setOpen(false, true);
|
|
|
return true;
|
|
|
}
|
|
|
/*接口回调NotesListActivity中的长按事件*/
|
|
|
contentClick.onLongClick(position, mItemData, NotesListItem.this, position);
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
|
|
|
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.getType() == Notes.TYPE_NOTE) {
|
|
|
/*如果是note类型就设置可侧滑,相反如果是文件夹就不可侧滑*/
|
|
|
swipeMenuLayout.setEnable(true);
|
|
|
/*设置置顶的显示图片 以及点击事件*/
|
|
|
top.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
|
@Override
|
|
|
public void onClick(View v) {
|
|
|
/*根据note id更新数据库,标记置顶*/
|
|
|
ContentResolver contentResolver = v.getContext().getContentResolver();//是以类似数据库中表的方式将数据暴露,获取或者更新数据
|
|
|
ContentValues values = new ContentValues();//contenvalues只能存储基本类型的数据
|
|
|
values.put(Notes.NoteColumns.TOP, !data.isTop()); //put函数赋值
|
|
|
values.put(Notes.NoteColumns.TYPE, Notes.TYPE_NOTE);
|
|
|
values.put(Notes.NoteColumns.LOCAL_MODIFIED, 1);
|
|
|
contentResolver.update(Notes.CONTENT_NOTE_URI, values, Notes.NoteColumns.ID //CONTENT_NOTE_URI:Uri to query all notes and folders
|
|
|
+ "=?", new String[]{
|
|
|
String.valueOf(mItemData.getId())
|
|
|
});
|
|
|
/*关闭所有侧滑菜单*/
|
|
|
slideHelper.closeAll();
|
|
|
}
|
|
|
});
|
|
|
/*根据数据库中的Top字段判断显示置顶图片还是取消置顶*/
|
|
|
if (mItemData.isTop()) {
|
|
|
top.setImageResource(R.drawable.cancel_zhiding);
|
|
|
} else {
|
|
|
top.setImageResource(R.drawable.top);
|
|
|
}
|
|
|
|
|
|
/*根据数据库中的Complete字段判断是否显示五角星图片*/
|
|
|
if (mItemData.isComplete()) {
|
|
|
complete.setVisibility(VISIBLE);
|
|
|
} else {
|
|
|
complete.setVisibility(GONE);
|
|
|
}
|
|
|
} else {
|
|
|
/*相反如果是文件夹就不可侧滑,同时不能显示完成的五角星**/
|
|
|
complete.setVisibility(GONE);
|
|
|
swipeMenuLayout.setEnable(false);
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|