Skip to content

Commit

Permalink
2018.11.16
Browse files Browse the repository at this point in the history
修复Demo中的各种异常
  • Loading branch information
sendtion committed Nov 16, 2018
1 parent 932897b commit e556234
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 134 deletions.
17 changes: 2 additions & 15 deletions app/src/main/java/com/sendtion/xrichtextdemo/ui/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,10 @@ private void initView() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_main);
setSupportActionBar(toolbar);

final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_main);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "新建笔记", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
Intent intent = new Intent(MainActivity.this, NewActivity.class);
intent.putExtra("groupName", groupName);
intent.putExtra("flag", 0);
startActivity(intent);
}
});

noteDao = new NoteDao(this);

rv_list_main = (RecyclerView) findViewById(R.id.rv_list_main);

rv_list_main.addItemDecoration(new SpacesItemDecoration(0));//设置item间距

LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);//竖向列表
rv_list_main.setLayoutManager(layoutManager);
Expand Down Expand Up @@ -116,6 +101,8 @@ public void onClick(DialogInterface dialog, int which) {

//刷新笔记列表
private void refreshNoteList(){
if (noteDao == null)
noteDao = new NoteDao(this);
noteList = noteDao.queryNotesAll(groupId);
mNoteListAdapter.setmNotes(noteList);
mNoteListAdapter.notifyDataSetChanged();
Expand Down
163 changes: 88 additions & 75 deletions app/src/main/java/com/sendtion/xrichtextdemo/ui/NewActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ protected void onCreate(Bundle savedInstanceState) {
private void initView() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_new);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
//toolbar.setNavigationIcon(R.drawable.ic_dialog_info);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -96,15 +98,6 @@ public void onClick(View v) {
}
});

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_new);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

groupDao = new GroupDao(this);
noteDao = new NoteDao(this);
note = new Note();
Expand All @@ -121,34 +114,6 @@ public void onClick(View view) {
tv_new_time = (TextView) findViewById(R.id.tv_new_time);
tv_new_group = (TextView) findViewById(R.id.tv_new_group);

// 图片删除事件
et_new_content.setOnRtImageDeleteListener(new RichTextEditor.OnRtImageDeleteListener() {

@Override
public void onRtImageDelete(String imagePath) {
if (!TextUtils.isEmpty(imagePath)) {
boolean isOK = SDCardUtil.deleteFile(imagePath);
if (isOK) {
showToast("删除成功:" + imagePath);
}
}
}
});
// 图片点击事件
et_new_content.setOnRtImageClickListener(new RichTextEditor.OnRtImageClickListener() {
@Override
public void onRtImageClick(String imagePath) {
myContent = getEditData();
if (!TextUtils.isEmpty(myContent)){
List<String> imageList = StringUtils.getTextFromHtml(myContent, true);
if (!TextUtils.isEmpty(imagePath)) {
int currentPosition = imageList.indexOf(imagePath);
showToast("点击图片:" + currentPosition + ":" + imagePath);
}
}
}
});

openSoftKeyInput();//打开软键盘显示

Intent intent = getIntent();
Expand Down Expand Up @@ -178,9 +143,7 @@ public void onRtImageClick(String imagePath) {
et_new_content.post(new Runnable() {
@Override
public void run() {
//showEditData(note.getContent());
et_new_content.clearAllLayout();
showDataSync(note.getContent());
dealWithContent();
}
});
}
Expand All @@ -196,6 +159,40 @@ public void run() {

}

private void dealWithContent(){
//showEditData(note.getContent());
et_new_content.clearAllLayout();
showDataSync(note.getContent());

// 图片删除事件
et_new_content.setOnRtImageDeleteListener(new RichTextEditor.OnRtImageDeleteListener() {

@Override
public void onRtImageDelete(String imagePath) {
if (!TextUtils.isEmpty(imagePath)) {
boolean isOK = SDCardUtil.deleteFile(imagePath);
if (isOK) {
showToast("删除成功:" + imagePath);
}
}
}
});
// 图片点击事件
et_new_content.setOnRtImageClickListener(new RichTextEditor.OnRtImageClickListener() {
@Override
public void onRtImageClick(String imagePath) {
myContent = getEditData();
if (!TextUtils.isEmpty(myContent)){
List<String> imageList = StringUtils.getTextFromHtml(myContent, true);
if (!TextUtils.isEmpty(imagePath)) {
int currentPosition = imageList.indexOf(imagePath);
showToast("点击图片:" + currentPosition + ":" + imagePath);
}
}
}
});
}

/**
* 关闭软键盘
*/
Expand Down Expand Up @@ -247,8 +244,10 @@ public void onComplete() {
if (loadingDialog != null){
loadingDialog.dismiss();
}
//在图片全部插入完毕后,再插入一个EditText,防止最后一张图片后无法插入文字
et_new_content.addEditTextAtIndex(et_new_content.getLastIndex(), "");
if (et_new_content != null) {
//在图片全部插入完毕后,再插入一个EditText,防止最后一张图片后无法插入文字
et_new_content.addEditTextAtIndex(et_new_content.getLastIndex(), "");
}
}

@Override
Expand All @@ -266,14 +265,16 @@ public void onSubscribe(Disposable d) {

@Override
public void onNext(String text) {
if (text.contains("<img") && text.contains("src=")) {
//imagePath可能是本地路径,也可能是网络地址
String imagePath = StringUtils.getImgSrc(text);
//插入空的EditText,以便在图片前后插入文字
et_new_content.addEditTextAtIndex(et_new_content.getLastIndex(), "");
et_new_content.addImageViewAtIndex(et_new_content.getLastIndex(), imagePath);
} else {
et_new_content.addEditTextAtIndex(et_new_content.getLastIndex(), text);
if (et_new_content != null) {
if (text.contains("<img") && text.contains("src=")) {
//imagePath可能是本地路径,也可能是网络地址
String imagePath = StringUtils.getImgSrc(text);
//插入空的EditText,以便在图片前后插入文字
et_new_content.addEditTextAtIndex(et_new_content.getLastIndex(), "");
et_new_content.addImageViewAtIndex(et_new_content.getLastIndex(), imagePath);
} else {
et_new_content.addEditTextAtIndex(et_new_content.getLastIndex(), text);
}
}
}
});
Expand Down Expand Up @@ -315,7 +316,7 @@ private String getEditData() {
/**
* 保存数据,=0销毁当前界面,=1不销毁界面,为了防止在后台时保存笔记并销毁,应该只保存笔记
*/
private void saveNoteData(boolean isBackground) {
private void saveNoteData(boolean isBackground) throws Exception {
String noteTitle = et_new_title.getText().toString();
String noteContent = getEditData();
String groupName = tv_new_group.getText().toString();
Expand Down Expand Up @@ -382,7 +383,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
callGallery();
break;
case R.id.action_new_save:
saveNoteData(false);
try {
saveNoteData(false);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
return super.onOptionsItemSelected(item);
Expand Down Expand Up @@ -502,37 +507,45 @@ protected void onResume() {
@Override
protected void onStop() {
super.onStop();
//如果APP处于后台,或者手机锁屏,则保存数据
if (CommonUtil.isAppOnBackground(getApplicationContext()) ||
CommonUtil.isLockScreeen(getApplicationContext())){
saveNoteData(true);//处于后台时保存数据
}
try {
//如果APP处于后台,或者手机锁屏,则保存数据
if (CommonUtil.isAppOnBackground(getApplicationContext()) ||
CommonUtil.isLockScreeen(getApplicationContext())){
saveNoteData(true);//处于后台时保存数据
}

if (subsLoading != null && subsLoading.isDisposed()){
subsLoading.dispose();
}
if (subsInsert != null && subsInsert.isDisposed()){
subsInsert.dispose();
if (subsLoading != null && subsLoading.isDisposed()){
subsLoading.dispose();
}
if (subsInsert != null && subsInsert.isDisposed()){
subsInsert.dispose();
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 退出处理
*/
private void dealwithExit(){
String noteTitle = et_new_title.getText().toString();
String noteContent = getEditData();
String groupName = tv_new_group.getText().toString();
String noteTime = tv_new_time.getText().toString();
if (flag == 0) {//新建笔记
if (noteTitle.length() > 0 || noteContent.length() > 0) {
saveNoteData(false);
}
}else if (flag == 1) {//编辑笔记
if (!noteTitle.equals(myTitle) || !noteContent.equals(myContent)
|| !groupName.equals(myGroupName) || !noteTime.equals(myNoteTime)) {
saveNoteData(false);
try {
String noteTitle = et_new_title.getText().toString();
String noteContent = getEditData();
String groupName = tv_new_group.getText().toString();
String noteTime = tv_new_time.getText().toString();
if (flag == 0) {//新建笔记
if (noteTitle.length() > 0 || noteContent.length() > 0) {
saveNoteData(false);
}
}else if (flag == 1) {//编辑笔记
if (!noteTitle.equals(myTitle) || !noteContent.equals(myContent)
|| !groupName.equals(myGroupName) || !noteTime.equals(myNoteTime)) {
saveNoteData(false);
}
}
} catch (Exception e) {
e.printStackTrace();
}
finish();
}
Expand Down
58 changes: 32 additions & 26 deletions app/src/main/java/com/sendtion/xrichtextdemo/ui/NoteActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,6 @@ public void onClick(View view) {
tv_note_time = (TextView) findViewById(R.id.tv_note_time);
tv_note_group = (TextView) findViewById(R.id.tv_note_group);

// 图片点击事件
tv_note_content.setOnRtImageClickListener(new RichTextView.OnRtImageClickListener() {
@Override
public void onRtImageClick(String imagePath) {
ArrayList<String> imageList = StringUtils.getTextFromHtml(myContent, true);
int currentPosition = imageList.indexOf(imagePath);
showToast("点击图片:"+currentPosition+":"+imagePath);

//点击图片预览
// PhotoPreview.builder()
// .setPhotos(imageList)
// .setCurrentItem(currentPosition)
// .setShowDeleteButton(false)
// .start(NoteActivity.this);
}
});

Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("data");
note = (Note) bundle.getSerializable("note");
Expand All @@ -136,16 +119,37 @@ public void onRtImageClick(String imagePath) {
tv_note_content.post(new Runnable() {
@Override
public void run() {
//showEditData(myContent);
tv_note_content.clearAllLayout();
showDataSync(myContent);
dealWithContent();
}
});
tv_note_time.setText(note.getCreateTime());
}

}

private void dealWithContent(){
//showEditData(myContent);
tv_note_content.clearAllLayout();
showDataSync(myContent);

// 图片点击事件
tv_note_content.setOnRtImageClickListener(new RichTextView.OnRtImageClickListener() {
@Override
public void onRtImageClick(String imagePath) {
ArrayList<String> imageList = StringUtils.getTextFromHtml(myContent, true);
int currentPosition = imageList.indexOf(imagePath);
showToast("点击图片:"+currentPosition+":"+imagePath);

//点击图片预览
// PhotoPreview.builder()
// .setPhotos(imageList)
// .setCurrentItem(currentPosition)
// .setShowDeleteButton(false)
// .start(NoteActivity.this);
}
});
}

/**
* 异步方式显示数据
* @param html
Expand Down Expand Up @@ -185,12 +189,14 @@ public void onSubscribe(Disposable d) {

@Override
public void onNext(String text) {
if (text.contains("<img") && text.contains("src=")) {
//imagePath可能是本地路径,也可能是网络地址
String imagePath = StringUtils.getImgSrc(text);
tv_note_content.addImageViewAtIndex(tv_note_content.getLastIndex(), imagePath);
} else {
tv_note_content.addTextViewAtIndex(tv_note_content.getLastIndex(), text);
if (tv_note_content !=null) {
if (text.contains("<img") && text.contains("src=")) {
//imagePath可能是本地路径,也可能是网络地址
String imagePath = StringUtils.getImgSrc(text);
tv_note_content.addImageViewAtIndex(tv_note_content.getLastIndex(), imagePath);
} else {
tv_note_content.addTextViewAtIndex(tv_note_content.getLastIndex(), text);
}
}
}
});
Expand Down
9 changes: 0 additions & 9 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,4 @@

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:visibility="gone"
app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>
9 changes: 0 additions & 9 deletions app/src/main/res/layout/activity_new.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,4 @@

<include layout="@layout/content_new" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:visibility="gone"
app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

0 comments on commit e556234

Please sign in to comment.