Skip to content

Commit aa1d26a

Browse files
committed
support delete some one by longclick
1 parent 3469033 commit aa1d26a

File tree

8 files changed

+109
-39
lines changed

8 files changed

+109
-39
lines changed

app/src/main/assets/litepal.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<dbname value="huahui_db"></dbname>
55

6-
<version value="1"></version>
6+
<version value="2"></version>
77

88
<list>
99

app/src/main/java/com/liyu/huahui/adapter/WordAdapter.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.liyu.huahui.adapter;
22

33
import android.graphics.Paint;
4+
import android.support.design.widget.Snackbar;
5+
import android.support.v4.content.ContextCompat;
46
import android.text.TextUtils;
57
import android.view.View;
68
import android.widget.TextView;
@@ -10,6 +12,7 @@
1012
import com.chad.library.adapter.base.BaseViewHolder;
1113
import com.liyu.huahui.R;
1214
import com.liyu.huahui.model.Word;
15+
import com.liyu.huahui.ui.MainActivity;
1316
import com.liyu.huahui.utils.DownloadUtil;
1417
import com.liyu.huahui.utils.Player;
1518

@@ -26,18 +29,18 @@ public WordAdapter(int layoutResId, List<Word> data) {
2629
}
2730

2831
@Override
29-
protected void convert(BaseViewHolder helper, final Word item) {
32+
protected void convert(final BaseViewHolder helper, final Word item) {
3033
helper.setText(R.id.tv_name, item.getName());
3134

32-
helper.setText(R.id.tv_correct, "正确:" + item.getCorrect());
35+
helper.setText(R.id.tv_correct, "正确:" + item.getCorrectPhonetic());
3336

34-
if (TextUtils.isEmpty(item.getWrong())) {
35-
helper.setText(R.id.tv_wrong, item.getWrong());
37+
if (TextUtils.isEmpty(item.getWrongPhonetic())) {
38+
helper.setText(R.id.tv_wrong, item.getWrongPhonetic());
3639
} else {
37-
helper.setText(R.id.tv_wrong, "错误:" + item.getWrong());
40+
helper.setText(R.id.tv_wrong, "错误:" + item.getWrongPhonetic());
3841
}
3942

40-
helper.setVisible(R.id.image_play, !TextUtils.isEmpty(item.getVoice()));
43+
helper.setVisible(R.id.image_play, !TextUtils.isEmpty(item.getVoiceUrl()));
4144

4245
((TextView) helper.getView(R.id.tv_wrong)).getPaint()
4346
.setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
@@ -55,6 +58,24 @@ public void onClick(View v) {
5558
playVoice(item);
5659
}
5760
});
61+
62+
helper.itemView.setOnLongClickListener(new View.OnLongClickListener() {
63+
@Override
64+
public boolean onLongClick(View v) {
65+
final int deletedPosition = helper.getAdapterPosition();
66+
remove(deletedPosition);
67+
item.delete();
68+
Snackbar.make(((MainActivity) mContext).getWindow().getDecorView().getRootView().findViewById(R.id.coordinatorLayout), "删除成功!",
69+
Snackbar.LENGTH_LONG).setAction("撤销", new View.OnClickListener() {
70+
@Override
71+
public void onClick(View view) {
72+
addData(deletedPosition, item);
73+
item.save();
74+
}
75+
}).setActionTextColor(ContextCompat.getColor(mContext, R.color.snackbar_action_color)).show();
76+
return false;
77+
}
78+
});
5879
}
5980

6081
private void playVoice(Word item) {

app/src/main/java/com/liyu/huahui/model/Word.java

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,33 @@
1111
public class Word extends DataSupport implements Serializable {
1212

1313
private String name;
14-
private String correct;
15-
private String wrong;
16-
private String voice;
14+
private String correctPhonetic;
15+
private String wrongPhonetic;
16+
private String voiceUrl;
17+
private int sourceFrom;
1718

18-
public String getVoice() {
19-
return voice;
19+
public int getSourceFrom() {
20+
return sourceFrom;
2021
}
2122

22-
public void setVoice(String voice) {
23-
this.voice = voice;
23+
public void setSourceFrom(From sourceFrom) {
24+
this.sourceFrom = sourceFrom.getFrom();
2425
}
2526

26-
public String getCorrect() {
27-
return correct;
27+
public String getVoiceUrl() {
28+
return voiceUrl;
2829
}
2930

30-
public void setCorrect(String correct) {
31-
this.correct = correct;
31+
public void setVoiceUrl(String voiceUrl) {
32+
this.voiceUrl = voiceUrl;
33+
}
34+
35+
public String getCorrectPhonetic() {
36+
return correctPhonetic;
37+
}
38+
39+
public void setCorrectPhonetic(String correctPhonetic) {
40+
this.correctPhonetic = correctPhonetic;
3241
}
3342

3443
public String getName() {
@@ -39,12 +48,12 @@ public void setName(String name) {
3948
this.name = name;
4049
}
4150

42-
public String getWrong() {
43-
return wrong;
51+
public String getWrongPhonetic() {
52+
return wrongPhonetic;
4453
}
4554

46-
public void setWrong(String wrong) {
47-
this.wrong = wrong;
55+
public void setWrongPhonetic(String wrongPhonetic) {
56+
this.wrongPhonetic = wrongPhonetic;
4857
}
4958

5059
@Override
@@ -58,4 +67,24 @@ public boolean equals(Object o) {
5867

5968
}
6069

70+
public enum From {
71+
LOCAL(1),
72+
NETWORK(2);
73+
74+
private int from;
75+
76+
From(int from) {
77+
this.from = from;
78+
}
79+
80+
public int getFrom() {
81+
return from;
82+
}
83+
84+
public void setFrom(int from) {
85+
this.from = from;
86+
}
87+
}
88+
89+
6190
}

app/src/main/java/com/liyu/huahui/ui/MainActivity.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030
import org.litepal.crud.DataSupport;
3131

3232
import java.io.IOException;
33+
import java.text.Collator;
3334
import java.util.ArrayList;
35+
import java.util.Collections;
36+
import java.util.Comparator;
3437
import java.util.List;
3538

3639
import rx.Observable;
@@ -83,7 +86,9 @@ private void getWords() {
8386
Observable<List<Word>> database = Observable.create(new Observable.OnSubscribe<List<Word>>() {
8487
@Override
8588
public void call(Subscriber<? super List<Word>> subscriber) {
86-
List<Word> words = DataSupport.findAll(Word.class);
89+
List<Word> words = DataSupport
90+
.where("sourceFrom =?", String.valueOf(Word.From.NETWORK.getFrom()))
91+
.find(Word.class);
8792
if (words == null || words.isEmpty()) {
8893
subscriber.onCompleted();
8994
} else {
@@ -111,13 +116,13 @@ public List<Word> call(String url) {
111116
Word word = new Word();
112117
word.setName(tds.get(0).ownText());
113118
Element a = tds.get(0).select("a").first();
114-
word.setVoice(a == null ? "" : a.attr("href"));
115-
word.setCorrect(tds.get(1).ownText());
116-
word.setWrong(tds.get(2).ownText());
119+
word.setVoiceUrl(a == null ? "" : a.attr("href"));
120+
word.setCorrectPhonetic(tds.get(1).ownText());
121+
word.setWrongPhonetic(tds.get(2).ownText());
122+
word.setSourceFrom(Word.From.NETWORK);
117123
words.add(word);
118124
}
119125
}
120-
DataSupport.deleteAll(Word.class);
121126
DataSupport.saveAll(words);
122127
return words;
123128
}
@@ -143,13 +148,25 @@ public void onError(Throwable e) {
143148

144149
@Override
145150
public void onNext(List<Word> words) {
146-
tvTotal.setText(String.format("总计:%s个", words.size()));
147-
adapter.setNewData(words);
151+
List<Word> allWords = DataSupport.findAll(Word.class);
152+
tvTotal.setText(String.format("总计:%s个", allWords.size()));
153+
adapter.setNewData(allWords);
154+
sortData();
148155
}
149156
});
150157

151158
}
152159

160+
private void sortData() {
161+
final Collator collator = Collator.getInstance();
162+
Collections.sort(adapter.getData(), new Comparator<Word>() {
163+
@Override
164+
public int compare(Word o1, Word o2) {
165+
return collator.compare(o1.getName(), o2.getName());
166+
}
167+
});
168+
}
169+
153170
@Override
154171
public boolean onCreateOptionsMenu(Menu menu) {
155172
getMenuInflater().inflate(R.menu.menu_main, menu);
@@ -168,7 +185,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
168185
startActivity(intent);
169186
return true;
170187
} else if (id == R.id.action_sync) {
171-
DataSupport.deleteAll(Word.class);
188+
DataSupport.deleteAll(Word.class, "sourceFrom =?", String.valueOf(Word.From.NETWORK.getFrom()));
172189
getWords();
173190
return true;
174191
} else if (id == R.id.action_cache) {
@@ -234,12 +251,14 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
234251
Word word = (Word) data.getSerializableExtra(QueryActivity.EXTRA_WORD);
235252
if (adapter.getData().contains(word)) {
236253
Toast.makeText(this, "列表中已包含 " + word.getName(), Toast.LENGTH_SHORT).show();
237-
} else if (word.getCorrect().contains("null")) {
254+
} else if (word.getCorrectPhonetic().contains("null")) {
238255
Toast.makeText(this, "该单词没有找到合适的读法", Toast.LENGTH_SHORT).show();
239256
} else {
240257
word.save();
241258
adapter.addData(word);
242-
recyclerView.scrollToPosition(adapter.getData().size() - 1);
259+
sortData();
260+
int wordIndex = adapter.getData().indexOf(word);
261+
recyclerView.scrollToPosition(wordIndex);
243262
}
244263
}
245264
}

app/src/main/java/com/liyu/huahui/ui/QueryActivity.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import android.view.animation.Interpolator;
1616
import android.widget.EditText;
1717
import android.widget.LinearLayout;
18-
import android.widget.Toast;
1918

2019
import com.liyu.huahui.App;
2120
import com.liyu.huahui.R;
@@ -30,7 +29,6 @@
3029

3130
import rx.Observer;
3231
import rx.android.schedulers.AndroidSchedulers;
33-
import rx.functions.Func1;
3432
import rx.schedulers.Schedulers;
3533

3634
/**
@@ -107,8 +105,9 @@ public void onError(Throwable e) {
107105
public void onNext(YoudaoResponse youdaoResponse) {
108106
Word word = new Word();
109107
word.setName(youdaoResponse.getQuery());
110-
word.setCorrect(String.format("[%s]", youdaoResponse.getBasic().getPhonetic()));
111-
word.setVoice(String.format("http://dict.youdao.com/dictvoice?audio=%s&type=1", youdaoResponse.getQuery()));
108+
word.setCorrectPhonetic(String.format("[%s]", youdaoResponse.getBasic().getPhonetic()));
109+
word.setVoiceUrl(String.format("http://dict.youdao.com/dictvoice?audio=%s&type=1", youdaoResponse.getQuery()));
110+
word.setSourceFrom(Word.From.LOCAL);
112111
Intent i = new Intent();
113112
i.putExtra(EXTRA_WORD, word);
114113
setResult(RESULT_OK, i);

app/src/main/java/com/liyu/huahui/utils/DownloadUtil.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class DownloadUtil {
2323
private static Handler handler = new Handler(Looper.getMainLooper());
2424

2525
public static void start(Word word, final SingleFileDownloadListener listener) {
26-
FileDownloader.getImpl().create(word.getVoice()).setTag(word.getName()).setPath(App.getContext().getFilesDir().getPath() + "/" + word + ".mp3")
26+
FileDownloader.getImpl().create(word.getVoiceUrl()).setTag(word.getName()).setPath(App.getContext().getFilesDir().getPath() + "/" + word + ".mp3")
2727
.setListener(new FileDownloadListener() {
2828
@Override
2929
protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
@@ -116,8 +116,8 @@ protected void warn(BaseDownloadTask task) {
116116
final List<BaseDownloadTask> tasks = new ArrayList<>();
117117

118118
for (Word word : list) {
119-
if (!TextUtils.isEmpty(word.getVoice()))
120-
tasks.add(FileDownloader.getImpl().create(word.getVoice()).setPath(App.getContext().getFilesDir().getPath() + "/" + word.getName() + ".mp3").setTag(word.getName()));
119+
if (!TextUtils.isEmpty(word.getVoiceUrl()))
120+
tasks.add(FileDownloader.getImpl().create(word.getVoiceUrl()).setPath(App.getContext().getFilesDir().getPath() + "/" + word.getName() + ".mp3").setTag(word.getName()));
121121
}
122122

123123
queueSet.disableCallbackProgressTimes();

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<android.support.design.widget.CoordinatorLayout
3+
android:id="@+id/coordinatorLayout"
34
xmlns:android="http://schemas.android.com/apk/res/android"
45
xmlns:app="http://schemas.android.com/apk/res-auto"
56
xmlns:tools="http://schemas.android.com/tools"

app/src/main/res/values/colors.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
<color name="white">#ffffff</color>
77
<color name="dialog_background_color">#fafafa</color>
88
<color name="dialog_background_scrim">#99323232</color>
9+
<color name="snackbar_action_color">#8BB2DE</color>
910
</resources>

0 commit comments

Comments
 (0)