费时两天,主要难点在RecyclerView和CheckBox的长按事件上。
上代码:
Note的JavaBean:
package com.example.jarvist.remember;
import org.litepal.crud.DataSupport;
import java.io.Serializable;
import java.util.Date;
/**
* Created by Jarvist on 2017/8/24.
*/
//传递对象要接入Serializable接口
public class Note extends DataSupport implements Serializable {
private int id;
private String content;
private Date date;
private boolean checked;
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
RecyclerView所需的Adapter
package com.example.jarvist.remember;
import android.content.Context;
import android.content.Intent;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by Jarvist on 2017/8/24.
*/
public class NoteAdapter extends RecyclerView.Adapter
implements View.OnClickListener,View.OnLongClickListener{
private List mNoteList;
private Context mContext;
private RecyclerViewOnItemClickListener onItemClickListener;
//multiple choice
public boolean MUL_tag = false;
//checkbox situation map 保存CheckBox选中状态
private HashMap ischecked = new HashMap();
public NoteAdapter (List noteList){
mNoteList = noteList;
initMaps();
}
public void initMaps(){
for(int i = 0; i getMap(){
return ischecked;
}
//接口,以便MainActivity中进行调用重写
public interface RecyclerViewOnItemClickListener
{
void onItemClickListener(View view,int position);
boolean onLongClickListener(View view, int position);
}
}
package com.example.jarvist.remember;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import org.litepal.crud.DataSupport;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private int status;//判断读写
public static final int WRITE = 1;// 1是新增便签,2是读取已有便签
public static final int READ = 2;
private RecyclerView recyclerView;
private List noteList;
private NoteAdapter adapter;
private ActionBar actionBar;
private boolean showboxTag = false;//判断是否显示CheckBox
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
final ImageButton deletebtn = (ImageButton)findViewById(R.id.menu1);
TextView title = (TextView)findViewById(R.id.title);
FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
fab.setBackgroundColor(Color.YELLOW);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
if(actionBar != null){
//左上角菜单键
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.menu3);
actionBar.setDisplayShowTitleEnabled(false);
}
noteList = DataSupport.order("date desc").find(Note.class);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,WriteActivity.class);
status = 1;
intent.putExtra("Status", status);//传递信息
startActivity(intent);
}
});
deletebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);//创建dialog
dialog.setTitle("删除便签");
dialog.setMessage("您确定要删除所选便签?");
dialog.setCancelable(false);
dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 获取保存通过CheckBox选中的便签,选中状态由adapter中的map保存,通过getMap()获取
Map map = adapter.getMap();
//从后往前删除
for(int i=map.size()-1; i>=0; i--){
if(map.get(i)){
//i为CheckBox选中的view的position
int id = noteList.get(i).getId();
deleteData(id);
//noteList中移除
noteList.remove(i);
//adapter重新设置item
adapter.notifyItemRemoved(i);
}
}
//adapter长度重新设置
adapter.notifyItemRangeChanged(0,noteList.size());
//删除后回到正常状态,CheckBox不显示,map重新归false
adapter.MUL_tag = false;
adapter.initMaps();
showboxTag = false;
adapter.notifyDataSetChanged();
}
});
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();//取消dialog
}
});
dialog.show();
}
});
deletebtn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//delete键按下后背景发生变化
if(event.getAction() == MotionEvent.ACTION_DOWN)
deletebtn.setBackgroundColor(getResources().getColor(R.color.colorbg1));
else if(event.getAction() == MotionEvent.ACTION_UP)
deletebtn.setBackgroundColor(getResources().getColor(R.color.colorbg2));
return false;
}
});
//创建adapter
adapter = new NoteAdapter(noteList);
//设置监听
adapter.setRecycleViewOnItemClickListener(new NoteAdapter.RecyclerViewOnItemClickListener() {
@Override
public void onItemClickListener(View view, int position) {
Intent intent = new Intent(MainActivity.this,WriteActivity.class);
Note note = noteList.get(position);
status = 2;
intent.putExtra("Status", status);
intent.putExtra("Content",note);//传递对象
startActivity(intent);
}
//长按时间
@Override
public boolean onLongClickListener(View view, int position) {
//长按显示CheckBox,并且长按位置选中该便签吖
adapter.setSelection(position);
adapter.setCheckBox();
adapter.notifyDataSetChanged();
showboxTag = true;
return true;
}
});
//瀑布流布局
StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(adapter);
}
//resume
@Override
protected void onResume(){
super.onResume();
//status归零
status = 0;
//noteList清空
noteList.clear();
//创建新list读取并按时间倒序排列
List newList = DataSupport.order("date desc").find(Note.class);
//将新list加入到noteList中
noteList.addAll(newList);
//adapter中CheckBox初始化
adapter.MUL_tag = false;
adapter.initMaps();
showboxTag = false;
adapter.notifyDataSetChanged();
}
public void deleteData(int id){
DataSupport.deleteAll(Note.class,"id = ?",String.valueOf(id));
}
@Override
public void onBackPressed(){
//处于多选状态,按下返回键回到正常状态
if(showboxTag){
adapter.MUL_tag = false;
adapter.initMaps();
adapter.notifyDataSetChanged();
showboxTag = false;
}
//否则调用父类方法
else
super.onBackPressed();
}
}
package com.example.jarvist.remember;
import android.content.Intent;
import android.media.Image;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import java.util.Date;
public class WriteActivity extends AppCompatActivity {
private ActionBar actionBar;
private int noteID;
private int status;
private EditText contentText;
private ImageButton menuView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_write);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
contentText = (EditText)findViewById(R.id.editText);
menuView = (ImageButton) findViewById(R.id.menu2);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
if(actionBar != null){
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
}
Intent intent = getIntent();
status = intent.getIntExtra("Status",0);
if(status == MainActivity.READ){
//读操作
Note note = (Note)intent.getSerializableExtra("Content");
//显示内容和游标放到内容末端
contentText.setText(note.getContent());
contentText.setSelection(note.getContent().length());
//获取id
noteID = note.getId();
}
//按钮按下背景变化
menuView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN)
menuView.setBackgroundColor(getResources().getColor(R.color.colorbg1));
else if(event.getAction() == MotionEvent.ACTION_UP)
menuView.setBackgroundColor(getResources().getColor(R.color.colorbg2));
return false;
}
});
menuView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//新增便签
if(status == MainActivity.WRITE){
Intent intent1 = new Intent(WriteActivity.this,MainActivity.class);
startActivity(intent1);
saveData();
}
else if(status == MainActivity.READ){
//读取便签后更新数据
Intent intent1 = new Intent(WriteActivity.this,MainActivity.class);
startActivity(intent1);
updateData();
}
finish();
}
});
}
//左上角home键
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case android.R.id.home:
Intent intent = new Intent(WriteActivity.this,MainActivity.class);
startActivity(intent);
break;
default:
break;
}
return true;
}
public void updateData(){
if(!(contentText.getText().toString().equals(""))){
Note note = new Note();
note.setDate(new Date());
note.setContent(contentText.getText().toString());
note.update(noteID);
}
}
public void saveData(){
if(!(contentText.getText().toString().equals("")) ){
Note note = new Note();
note.setDate(new Date());
note.setContent(contentText.getText().toString());
note.save();
}
}
}
MainActivity.xml:
Github项目地址:
https://github.com/JarvistFth/Rememberhttps://github.com/JarvistFth/Remember
点击打开链接
第一个练手的app,感谢郭神的书。