├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── values
│ │ │ │ ├── ids.xml
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── drawable
│ │ │ │ ├── ic_add_white_24dp.xml
│ │ │ │ ├── ic_chevron_right_black_24dp.xml
│ │ │ │ ├── ic_delete_forever_black_24dp.xml
│ │ │ │ ├── ic_search_black_24dp.xml
│ │ │ │ └── ic_launcher_background.xml
│ │ │ ├── menu
│ │ │ │ └── main_menu.xml
│ │ │ ├── navigation
│ │ │ │ └── navigation.xml
│ │ │ ├── layout
│ │ │ │ ├── activity_main.xml
│ │ │ │ ├── fragment_words.xml
│ │ │ │ ├── fragment_add.xml
│ │ │ │ ├── cell_normal.xml
│ │ │ │ ├── cell_card.xml
│ │ │ │ ├── cell_normal_2.xml
│ │ │ │ └── cell_card_2.xml
│ │ │ └── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── example
│ │ │ │ └── words
│ │ │ │ ├── WordDao.java
│ │ │ │ ├── WordViewModel.java
│ │ │ │ ├── MainActivity.java
│ │ │ │ ├── Word.java
│ │ │ │ ├── WordDatabase.java
│ │ │ │ ├── WordRepository.java
│ │ │ │ ├── AddFragment.java
│ │ │ │ ├── MyAdapter.java
│ │ │ │ └── WordsFragment.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── words
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── example
│ │ └── words
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── README.md
├── .idea
├── encodings.xml
├── misc.xml
├── runConfigurations.xml
├── gradle.xml
├── codeStyles
│ └── Project.xml
└── dbnavigator.xml
├── .gitignore
├── gradle.properties
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/longway777/Words-Step-4/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/longway777/Words-Step-4/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/longway777/Words-Step-4/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/longway777/Words-Step-4/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/longway777/Words-Step-4/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/longway777/Words-Step-4/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Words-Step-4-Done
2 | Source code for video tutorials of "Android 基础教程" on bilibili.com [longway777](https://space.bilibili.com/137860026)
3 |
--------------------------------------------------------------------------------
/app/src/main/res/values/ids.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/longway777/Words-Step-4/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/longway777/Words-Step-4/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/longway777/Words-Step-4/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/longway777/Words-Step-4/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/longway777/Words-Step-4/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Words
3 |
4 |
5 | Hello blank fragment
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #D81B60
6 |
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Sep 02 03:14:38 CST 2019
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_add_white_24dp.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_chevron_right_black_24dp.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/example/words/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.example.words;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_delete_forever_black_24dp.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_search_black_24dp.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/main_menu.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/example/words/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.example.words;
2 |
3 | import android.content.Context;
4 |
5 | import androidx.test.InstrumentationRegistry;
6 | import androidx.test.runner.AndroidJUnit4;
7 |
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 |
11 | import static org.junit.Assert.*;
12 |
13 | /**
14 | * Instrumented test, which will execute on an Android device.
15 | *
16 | * @see Testing documentation
17 | */
18 | @RunWith(AndroidJUnit4.class)
19 | public class ExampleInstrumentedTest {
20 | @Test
21 | public void useAppContext() {
22 | // Context of the app under test.
23 | Context appContext = InstrumentationRegistry.getTargetContext();
24 |
25 | assertEquals("com.example.roombasic", appContext.getPackageName());
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/words/WordDao.java:
--------------------------------------------------------------------------------
1 | package com.example.words;
2 |
3 | import androidx.lifecycle.LiveData;
4 | import androidx.room.Dao;
5 | import androidx.room.Delete;
6 | import androidx.room.Insert;
7 | import androidx.room.Query;
8 | import androidx.room.Update;
9 |
10 | import java.util.List;
11 |
12 | @Dao // Database access object
13 | public interface WordDao {
14 | @Insert
15 | void insertWords(Word... words);
16 |
17 | @Update
18 | void updateWords(Word... words);
19 |
20 | @Delete
21 | void deleteWords(Word... words);
22 |
23 | @Query("DELETE FROM WORD")
24 | void deleteAllWords();
25 |
26 | @Query("SELECT * FROM WORD ORDER BY ID DESC")
27 | //List getAllWords();
28 | LiveData>getAllWordsLive();
29 |
30 | @Query("SELECT * FROM WORD WHERE english_word LIKE :pattern ORDER BY ID DESC")
31 | LiveData>findWordsWithPattern(String pattern);
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/app/src/main/res/navigation/navigation.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
17 |
18 |
23 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
15 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
20 |
21 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_words.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
19 |
20 |
24 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/words/WordViewModel.java:
--------------------------------------------------------------------------------
1 | package com.example.words;
2 |
3 | import android.app.Application;
4 |
5 | import androidx.annotation.NonNull;
6 | import androidx.lifecycle.AndroidViewModel;
7 | import androidx.lifecycle.LiveData;
8 |
9 | import java.util.List;
10 |
11 | public class WordViewModel extends AndroidViewModel {
12 | private WordRepository wordRepository;
13 | public WordViewModel(@NonNull Application application) {
14 | super(application);
15 | wordRepository = new WordRepository(application);
16 | }
17 |
18 | LiveData> getAllWordsLive() {
19 | return wordRepository.getAllWordsLive();
20 | }
21 | LiveData> findWordsWithPattern(String patten) {
22 | return wordRepository.findWordsWithPattern(patten);
23 | }
24 |
25 | void insertWords(Word... words) {
26 | wordRepository.insertWords(words);
27 | }
28 | void updateWords(Word... words) {
29 | wordRepository.updateWords(words);
30 | }
31 | void deleteWords(Word... words) {
32 | wordRepository.deleteWords(words);
33 | }
34 | void deleteAllWords() {
35 | wordRepository.deleteAllWords();
36 | }
37 |
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/words/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.words;
2 |
3 | import android.content.Context;
4 | import android.os.Bundle;
5 | import android.view.inputmethod.InputMethodManager;
6 |
7 | import androidx.appcompat.app.AppCompatActivity;
8 | import androidx.navigation.NavController;
9 | import androidx.navigation.Navigation;
10 | import androidx.navigation.ui.NavigationUI;
11 |
12 | public class MainActivity extends AppCompatActivity {
13 | private NavController navController;
14 |
15 |
16 | @Override
17 | protected void onCreate(Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | setContentView(R.layout.activity_main);
20 | navController = Navigation.findNavController(findViewById(R.id.fragment));
21 | NavigationUI.setupActionBarWithNavController(this,navController);
22 |
23 | }
24 |
25 |
26 |
27 | @Override
28 | public void onBackPressed() {
29 | super.onBackPressed();
30 | navController.navigateUp();
31 | }
32 |
33 | @Override
34 | public boolean onSupportNavigateUp() {
35 | InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
36 | imm.hideSoftInputFromWindow(findViewById(R.id.fragment).getWindowToken(),0);
37 | navController.navigateUp();
38 | return super.onSupportNavigateUp();
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 28
5 | defaultConfig {
6 | applicationId "com.example.words"
7 | minSdkVersion 19
8 | targetSdkVersion 28
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
12 | android.defaultConfig.vectorDrawables.useSupportLibrary = true
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | implementation fileTree(dir: 'libs', include: ['*.jar'])
24 | implementation 'androidx.appcompat:appcompat:1.0.2'
25 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
26 | implementation 'androidx.legacy:legacy-support-v4:1.0.0'
27 | testImplementation 'junit:junit:4.12'
28 | androidTestImplementation 'androidx.test:runner:1.2.0'
29 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
30 |
31 | def room_version = "2.2.0-alpha01"
32 |
33 | implementation "androidx.room:room-runtime:$room_version"
34 | annotationProcessor "androidx.room:room-compiler:$room_version"
35 | // For Kotlin use kapt instead of annotationProcessor
36 |
37 |
38 | // Test helpers
39 | testImplementation "androidx.room:room-testing:$room_version"
40 | implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
41 | implementation 'androidx.recyclerview:recyclerview:1.0.0'
42 | implementation 'androidx.cardview:cardview:1.0.0'
43 | implementation 'androidx.navigation:navigation-fragment:2.0.0'
44 | implementation 'androidx.navigation:navigation-ui:2.1.0'
45 | }
46 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/words/Word.java:
--------------------------------------------------------------------------------
1 | package com.example.words;
2 |
3 | import androidx.room.ColumnInfo;
4 | import androidx.room.Entity;
5 | import androidx.room.PrimaryKey;
6 |
7 | @Entity
8 | public class Word {
9 | @PrimaryKey(autoGenerate = true)
10 | private int id;
11 |
12 | @ColumnInfo(name = "english_word")
13 | private String word;
14 | @ColumnInfo(name = "chinese_meaning")
15 | private String chineseMeaning;
16 |
17 | @ColumnInfo(name = "chinese_invisible")
18 | private boolean chineseInvisible;
19 |
20 | boolean isChineseInvisible() {
21 | return chineseInvisible;
22 | }
23 |
24 | void setChineseInvisible(boolean chineseInvisible) {
25 | this.chineseInvisible = chineseInvisible;
26 | }
27 | // @ColumnInfo(name = "foo_data")
28 | // private boolean foo;
29 | // @ColumnInfo(name = "bar_data")
30 | // private boolean bar;
31 | //
32 | // public boolean isBar() {
33 | // return bar;
34 | // }
35 | //
36 | // public void setBar(boolean bar) {
37 | // this.bar = bar;
38 | // }
39 | //
40 | // public boolean isFoo() {
41 | // return foo;
42 | // }
43 | //
44 | // public void setFoo(boolean foo) {
45 | // this.foo = foo;
46 | // }
47 |
48 | public Word(String word, String chineseMeaning) {
49 | this.word = word;
50 | this.chineseMeaning = chineseMeaning;
51 | }
52 |
53 | public int getId() {
54 | return id;
55 | }
56 |
57 | public void setId(int id) {
58 | this.id = id;
59 | }
60 |
61 | public String getWord() {
62 | return word;
63 | }
64 |
65 | public void setWord(String word) {
66 | this.word = word;
67 | }
68 |
69 | String getChineseMeaning() {
70 | return chineseMeaning;
71 | }
72 |
73 | }
74 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/words/WordDatabase.java:
--------------------------------------------------------------------------------
1 | package com.example.words;
2 |
3 | import android.content.Context;
4 |
5 | import androidx.annotation.NonNull;
6 | import androidx.room.Database;
7 | import androidx.room.Room;
8 | import androidx.room.RoomDatabase;
9 | import androidx.room.migration.Migration;
10 | import androidx.sqlite.db.SupportSQLiteDatabase;
11 |
12 | //singleton
13 | @Database(entities = {Word.class},version = 5,exportSchema = false)
14 | public abstract class WordDatabase extends RoomDatabase {
15 | private static WordDatabase INSTANCE;
16 | static synchronized WordDatabase getDatabase(Context context) {
17 | if (INSTANCE == null) {
18 | INSTANCE = Room.databaseBuilder(context.getApplicationContext(),WordDatabase.class,"word_database")
19 | //.fallbackToDestructiveMigration()
20 | .addMigrations(MIGRATION_4_5)
21 | .build();
22 | }
23 | return INSTANCE;
24 | }
25 | public abstract WordDao getWordDao();
26 |
27 | static final Migration MIGRATION_2_3 = new Migration(2,3) {
28 | @Override
29 | public void migrate(@NonNull SupportSQLiteDatabase database) {
30 | database.execSQL("ALTER TABLE word ADD COLUMN bar_data INTEGER NOT NULL DEFAULT 1");
31 | }
32 | };
33 |
34 | static final Migration MIGRATION_3_4 = new Migration(3,4) {
35 | @Override
36 | public void migrate(@NonNull SupportSQLiteDatabase database) {
37 | database.execSQL("CREATE TABLE word_temp (id INTEGER PRIMARY KEY NOT NULL ,english_word TEXT," +
38 | "chinese_meaning TEXT)");
39 | database.execSQL("INSERT INTO word_temp (id,english_word,chinese_meaning) " +
40 | "SELECT id,english_word,chinese_meaning FROM word");
41 | database.execSQL("DROP TABLE word");
42 | database.execSQL("ALTER TABLE word_temp RENAME to word");
43 | }
44 | };
45 |
46 | private static final Migration MIGRATION_4_5 = new Migration(4,5) {
47 | @Override
48 | public void migrate(@NonNull SupportSQLiteDatabase database) {
49 | database.execSQL("ALTER TABLE word ADD COLUMN chinese_invisible INTEGER NOT NULL DEFAULT 0");
50 | }
51 | };
52 | }
53 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_add.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
21 |
22 |
36 |
37 |
49 |
50 |
60 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/words/WordRepository.java:
--------------------------------------------------------------------------------
1 | package com.example.words;
2 |
3 | import android.content.Context;
4 | import android.os.AsyncTask;
5 |
6 | import androidx.lifecycle.LiveData;
7 |
8 | import java.util.List;
9 |
10 | class WordRepository {
11 | private LiveData> allWordsLive;
12 | private WordDao wordDao;
13 |
14 | WordRepository(Context context) {
15 | WordDatabase wordDatabase = WordDatabase.getDatabase(context.getApplicationContext());
16 | wordDao = wordDatabase.getWordDao();
17 | allWordsLive = wordDao.getAllWordsLive();
18 | }
19 |
20 | void insertWords(Word... words) {
21 | new InsertAsyncTask(wordDao).execute(words);
22 | }
23 |
24 | void updateWords(Word... words) {
25 | new UpdateAsyncTask(wordDao).execute(words);
26 | }
27 |
28 | void deleteWords(Word... words) {
29 | new DeleteAsyncTask(wordDao).execute(words);
30 | }
31 |
32 | void deleteAllWords() {
33 | new DeleteAllAsyncTask(wordDao).execute();
34 | }
35 |
36 |
37 | LiveData> getAllWordsLive() {
38 | return allWordsLive;
39 | }
40 | LiveData> findWordsWithPattern(String pattern) {
41 | return wordDao.findWordsWithPattern("%" + pattern + "%");
42 | }
43 |
44 | static class InsertAsyncTask extends AsyncTask {
45 | private WordDao wordDao;
46 |
47 | InsertAsyncTask(WordDao wordDao) {
48 | this.wordDao = wordDao;
49 | }
50 |
51 | @Override
52 | protected Void doInBackground(Word... words) {
53 | wordDao.insertWords(words);
54 | return null;
55 | }
56 |
57 | }
58 |
59 | static class UpdateAsyncTask extends AsyncTask {
60 | private WordDao wordDao;
61 |
62 | UpdateAsyncTask(WordDao wordDao) {
63 | this.wordDao = wordDao;
64 | }
65 |
66 | @Override
67 | protected Void doInBackground(Word... words) {
68 | wordDao.updateWords(words);
69 | return null;
70 | }
71 |
72 | }
73 |
74 | static class DeleteAsyncTask extends AsyncTask {
75 | private WordDao wordDao;
76 |
77 | DeleteAsyncTask(WordDao wordDao) {
78 | this.wordDao = wordDao;
79 | }
80 |
81 | @Override
82 | protected Void doInBackground(Word... words) {
83 | wordDao.deleteWords(words);
84 | return null;
85 | }
86 |
87 | }
88 |
89 | static class DeleteAllAsyncTask extends AsyncTask {
90 | private WordDao wordDao;
91 |
92 | DeleteAllAsyncTask(WordDao wordDao) {
93 | this.wordDao = wordDao;
94 | }
95 |
96 | @Override
97 | protected Void doInBackground(Void... voids) {
98 | wordDao.deleteAllWords();
99 | return null;
100 | }
101 |
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/cell_normal.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
15 |
16 |
22 |
23 |
35 |
36 |
50 |
51 |
60 |
61 |
74 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/words/AddFragment.java:
--------------------------------------------------------------------------------
1 | package com.example.words;
2 |
3 |
4 | import android.content.Context;
5 | import android.os.Bundle;
6 |
7 | import androidx.annotation.Nullable;
8 | import androidx.fragment.app.Fragment;
9 | import androidx.fragment.app.FragmentActivity;
10 | import androidx.lifecycle.ViewModelProviders;
11 | import androidx.navigation.NavController;
12 | import androidx.navigation.Navigation;
13 |
14 | import android.text.Editable;
15 | import android.text.TextWatcher;
16 | import android.view.LayoutInflater;
17 | import android.view.View;
18 | import android.view.ViewGroup;
19 | import android.view.inputmethod.InputMethodManager;
20 | import android.widget.Button;
21 | import android.widget.EditText;
22 |
23 |
24 | /**
25 | * A simple {@link Fragment} subclass.
26 | */
27 | public class AddFragment extends Fragment {
28 | private Button buttonSubmit;
29 | private EditText editTextEnglish,editTextChinese;
30 | private WordViewModel wordViewModel;
31 |
32 |
33 |
34 | public AddFragment() {
35 | // Required empty public constructor
36 | }
37 |
38 |
39 | @Override
40 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
41 | Bundle savedInstanceState) {
42 | // Inflate the layout for this fragment
43 | return inflater.inflate(R.layout.fragment_add, container, false);
44 | }
45 |
46 | @Override
47 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
48 | super.onActivityCreated(savedInstanceState);
49 | FragmentActivity activity = requireActivity();
50 | wordViewModel = ViewModelProviders.of(activity).get(WordViewModel.class);
51 | buttonSubmit = activity.findViewById(R.id.buttonSubmit);
52 | editTextEnglish = activity.findViewById(R.id.editTextEnglish);
53 | editTextChinese = activity.findViewById(R.id.editTextChinese);
54 | buttonSubmit.setEnabled(false);
55 | editTextEnglish.requestFocus();
56 | InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
57 | imm.showSoftInput(editTextEnglish,0);
58 |
59 | TextWatcher textWatcher = new TextWatcher() {
60 | @Override
61 | public void beforeTextChanged(CharSequence s, int start, int count, int after) {
62 |
63 | }
64 |
65 | @Override
66 | public void onTextChanged(CharSequence s, int start, int before, int count) {
67 | String english = editTextEnglish.getText().toString().trim();
68 | String chinese = editTextChinese.getText().toString().trim();
69 | buttonSubmit.setEnabled(!english.isEmpty() && !chinese.isEmpty());
70 | }
71 |
72 | @Override
73 | public void afterTextChanged(Editable s) {
74 |
75 | }
76 | };
77 | editTextEnglish.addTextChangedListener(textWatcher);
78 | editTextChinese.addTextChangedListener(textWatcher);
79 | buttonSubmit.setOnClickListener(new View.OnClickListener() {
80 | @Override
81 | public void onClick(View v) {
82 | String english = editTextEnglish.getText().toString().trim();
83 | String chinese = editTextChinese.getText().toString().trim();
84 | Word word = new Word(english,chinese);
85 | wordViewModel.insertWords(word);
86 | NavController navController = Navigation.findNavController(v);
87 | navController.navigateUp();
88 | InputMethodManager imm = (InputMethodManager) requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
89 | imm.hideSoftInputFromWindow(v.getWindowToken(),0);
90 | }
91 | });
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/cell_card.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
18 |
19 |
22 |
23 |
29 |
30 |
36 |
37 |
50 |
51 |
65 |
66 |
75 |
76 |
89 |
90 |
91 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/cell_normal_2.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
20 |
21 |
29 |
30 |
40 |
41 |
53 |
54 |
62 |
63 |
70 |
71 |
72 |
80 |
81 |
91 |
92 |
93 |
99 |
100 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/words/MyAdapter.java:
--------------------------------------------------------------------------------
1 | package com.example.words;
2 |
3 | import android.content.Intent;
4 | import android.net.Uri;
5 | import android.view.LayoutInflater;
6 | import android.view.View;
7 | import android.view.ViewGroup;
8 | import android.widget.CompoundButton;
9 | import android.widget.Switch;
10 | import android.widget.TextView;
11 |
12 | import androidx.annotation.NonNull;
13 | import androidx.recyclerview.widget.DiffUtil;
14 | import androidx.recyclerview.widget.ListAdapter;
15 | import androidx.recyclerview.widget.RecyclerView;
16 |
17 | public class MyAdapter extends ListAdapter {
18 | private boolean useCardView;
19 | private WordViewModel wordViewModel;
20 |
21 | MyAdapter(boolean useCardView, WordViewModel wordViewModel) {
22 | super(new DiffUtil.ItemCallback() {
23 | @Override
24 | public boolean areItemsTheSame(@NonNull Word oldItem, @NonNull Word newItem) {
25 | return oldItem.getId() == newItem.getId();
26 | }
27 |
28 | @Override
29 | public boolean areContentsTheSame(@NonNull Word oldItem, @NonNull Word newItem) {
30 | return (oldItem.getWord().equals(newItem.getWord())
31 | && oldItem.getChineseMeaning().equals(newItem.getChineseMeaning())
32 | && oldItem.isChineseInvisible() == newItem.isChineseInvisible());
33 | }
34 | });
35 | this.useCardView = useCardView;
36 | this.wordViewModel = wordViewModel;
37 | }
38 |
39 |
40 |
41 | @NonNull
42 | @Override
43 | public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
44 | LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
45 | View itemView;
46 | if (useCardView) {
47 | itemView = layoutInflater.inflate(R.layout.cell_card_2,parent,false);
48 | } else {
49 | itemView = layoutInflater.inflate(R.layout.cell_normal_2,parent,false);
50 | }
51 | final MyViewHolder holder = new MyViewHolder(itemView);
52 | holder.itemView.setOnClickListener(new View.OnClickListener() {
53 | @Override
54 | public void onClick(View v) {
55 | Uri uri = Uri.parse("https://m.youdao.com/dict?le=eng&q=" + holder.textViewEnglish.getText());
56 | Intent intent = new Intent(Intent.ACTION_VIEW);
57 | intent.setData(uri);
58 | holder.itemView.getContext().startActivity(intent);
59 | }
60 | });
61 |
62 | holder.aSwitchChineseInvisible.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
63 | @Override
64 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
65 | Word word = (Word) holder.itemView.getTag(R.id.word_for_view_holder);
66 | if (isChecked) {
67 | holder.textViewChinese.setVisibility(View.GONE);
68 | word.setChineseInvisible(true);
69 | wordViewModel.updateWords(word);
70 | } else {
71 | holder.textViewChinese.setVisibility(View.VISIBLE);
72 | word.setChineseInvisible(false);
73 | wordViewModel.updateWords(word);
74 | }
75 | }
76 | });
77 | return holder;
78 | }
79 |
80 | @Override
81 | public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
82 | final Word word = getItem(position);
83 | holder.itemView.setTag(R.id.word_for_view_holder,word);
84 | holder.textViewNumber.setText(String.valueOf(position + 1));
85 | holder.textViewEnglish.setText(word.getWord());
86 | holder.textViewChinese.setText(word.getChineseMeaning());
87 |
88 | if (word.isChineseInvisible()) {
89 | holder.textViewChinese.setVisibility(View.GONE);
90 | holder.aSwitchChineseInvisible.setChecked(true);
91 | } else {
92 | holder.textViewChinese.setVisibility(View.VISIBLE);
93 | holder.aSwitchChineseInvisible.setChecked(false);
94 | }
95 |
96 | }
97 |
98 | @Override
99 | public void onViewAttachedToWindow(@NonNull MyViewHolder holder) {
100 | super.onViewAttachedToWindow(holder);
101 | holder.textViewNumber.setText(String.valueOf(holder.getAdapterPosition() + 1));
102 | }
103 |
104 | static class MyViewHolder extends RecyclerView.ViewHolder {
105 | TextView textViewNumber,textViewEnglish,textViewChinese;
106 | Switch aSwitchChineseInvisible;
107 | MyViewHolder(@NonNull View itemView) {
108 | super(itemView);
109 | textViewNumber = itemView.findViewById(R.id.textViewNumber);
110 | textViewEnglish = itemView.findViewById(R.id.textViewEnglish);
111 | textViewChinese = itemView.findViewById(R.id.textViewChinese);
112 | aSwitchChineseInvisible = itemView.findViewById(R.id.switchChineseInvisible);
113 | }
114 | }
115 | }
116 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/cell_card_2.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
16 |
17 |
20 |
21 |
32 |
33 |
41 |
42 |
52 |
53 |
65 |
66 |
74 |
75 |
82 |
83 |
84 |
91 |
92 |
103 |
104 |
105 |
111 |
112 |
113 |
114 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | xmlns:android
59 |
60 | ^$
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 | xmlns:.*
70 |
71 | ^$
72 |
73 |
74 | BY_NAME
75 |
76 |
77 |
78 |
79 |
80 |
81 | .*:id
82 |
83 | http://schemas.android.com/apk/res/android
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 | .*:name
93 |
94 | http://schemas.android.com/apk/res/android
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 | name
104 |
105 | ^$
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 | style
115 |
116 | ^$
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 | .*
126 |
127 | ^$
128 |
129 |
130 | BY_NAME
131 |
132 |
133 |
134 |
135 |
136 |
137 | .*
138 |
139 | http://schemas.android.com/apk/res/android
140 |
141 |
142 | ANDROID_ATTRIBUTE_ORDER
143 |
144 |
145 |
146 |
147 |
148 |
149 | .*
150 |
151 | .*
152 |
153 |
154 | BY_NAME
155 |
156 |
157 |
158 |
159 |
160 |
161 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/words/WordsFragment.java:
--------------------------------------------------------------------------------
1 | package com.example.words;
2 |
3 |
4 | import android.app.AlertDialog;
5 | import android.content.Context;
6 | import android.content.DialogInterface;
7 | import android.content.SharedPreferences;
8 | import android.graphics.Canvas;
9 | import android.graphics.Color;
10 | import android.graphics.drawable.ColorDrawable;
11 | import android.graphics.drawable.Drawable;
12 | import android.os.Bundle;
13 | import android.view.LayoutInflater;
14 | import android.view.Menu;
15 | import android.view.MenuInflater;
16 | import android.view.MenuItem;
17 | import android.view.View;
18 | import android.view.ViewGroup;
19 | import android.widget.SearchView;
20 |
21 | import androidx.annotation.NonNull;
22 | import androidx.annotation.Nullable;
23 | import androidx.core.content.ContextCompat;
24 | import androidx.fragment.app.Fragment;
25 | import androidx.lifecycle.LiveData;
26 | import androidx.lifecycle.Observer;
27 | import androidx.lifecycle.ViewModelProviders;
28 | import androidx.navigation.NavController;
29 | import androidx.navigation.Navigation;
30 | import androidx.recyclerview.widget.DefaultItemAnimator;
31 | import androidx.recyclerview.widget.DividerItemDecoration;
32 | import androidx.recyclerview.widget.ItemTouchHelper;
33 | import androidx.recyclerview.widget.LinearLayoutManager;
34 | import androidx.recyclerview.widget.RecyclerView;
35 |
36 | import com.google.android.material.floatingactionbutton.FloatingActionButton;
37 | import com.google.android.material.snackbar.Snackbar;
38 |
39 | import java.util.List;
40 |
41 |
42 | /**
43 | * A simple {@link Fragment} subclass.
44 | */
45 | public class WordsFragment extends Fragment {
46 | private WordViewModel wordViewModel;
47 | private RecyclerView recyclerView;
48 | private MyAdapter myAdapter1, myAdapter2;
49 | private LiveData> filteredWords;
50 | private static final String VIEW_TYPE_SHP = "view_type_shp";
51 | private static final String IS_USING_CARD_VIEW = "is_using_card_view";
52 | private List allWords;
53 | private boolean undoAction;
54 | private DividerItemDecoration dividerItemDecoration;
55 |
56 |
57 | public WordsFragment() {
58 | // Required empty public constructor
59 | setHasOptionsMenu(true);
60 | }
61 |
62 | @Override
63 | public boolean onOptionsItemSelected(MenuItem item) {
64 | switch (item.getItemId()) {
65 | case R.id.clearData:
66 | AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
67 | builder.setTitle("清空数据");
68 | builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
69 | @Override
70 | public void onClick(DialogInterface dialog, int which) {
71 | wordViewModel.deleteAllWords();
72 | }
73 | });
74 | builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
75 | @Override
76 | public void onClick(DialogInterface dialog, int which) {
77 |
78 | }
79 | });
80 | builder.create();
81 | builder.show();
82 | break;
83 | case R.id.switchViewType:
84 | SharedPreferences shp = requireActivity().getSharedPreferences(VIEW_TYPE_SHP, Context.MODE_PRIVATE);
85 | boolean isUsingCardView = shp.getBoolean(IS_USING_CARD_VIEW, false);
86 | SharedPreferences.Editor editor = shp.edit();
87 | if (isUsingCardView) {
88 | recyclerView.setAdapter(myAdapter1);
89 | recyclerView.addItemDecoration(dividerItemDecoration);
90 | editor.putBoolean(IS_USING_CARD_VIEW, false);
91 | } else {
92 | recyclerView.setAdapter(myAdapter2);
93 | recyclerView.removeItemDecoration(dividerItemDecoration);
94 | editor.putBoolean(IS_USING_CARD_VIEW, true);
95 | }
96 | editor.apply();
97 | }
98 | return super.onOptionsItemSelected(item);
99 | }
100 |
101 | @Override
102 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
103 | super.onCreateOptionsMenu(menu, inflater);
104 | inflater.inflate(R.menu.main_menu, menu);
105 | SearchView searchView = (SearchView) menu.findItem(R.id.app_bar_search).getActionView();
106 | searchView.setMaxWidth(1000);
107 | searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
108 | @Override
109 | public boolean onQueryTextSubmit(String query) {
110 | return false;
111 | }
112 |
113 | @Override
114 | public boolean onQueryTextChange(String newText) {
115 | String pattern = newText.trim();
116 | filteredWords.removeObservers(getViewLifecycleOwner()); //先移除之前在onActivityCreated中添加的observer
117 | filteredWords = wordViewModel.findWordsWithPattern(pattern);
118 | filteredWords.observe(getViewLifecycleOwner(), new Observer>() {
119 | @Override
120 | public void onChanged(List words) {
121 | int temp = myAdapter1.getItemCount();
122 | allWords = words;
123 | if (temp != words.size()) {
124 | myAdapter1.submitList(words);
125 | myAdapter2.submitList(words);
126 | }
127 | }
128 | });
129 | return true;
130 | }
131 | });
132 | }
133 |
134 | @Override
135 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
136 | Bundle savedInstanceState) {
137 | // Inflate the layout for this fragment
138 | return inflater.inflate(R.layout.fragment_words, container, false);
139 | }
140 |
141 | @Override
142 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
143 | super.onActivityCreated(savedInstanceState);
144 | wordViewModel = ViewModelProviders.of(requireActivity()).get(WordViewModel.class);
145 | recyclerView = requireActivity().findViewById(R.id.recyclerView);
146 | recyclerView.setLayoutManager(new LinearLayoutManager(requireActivity()));
147 | myAdapter1 = new MyAdapter(false, wordViewModel);
148 | myAdapter2 = new MyAdapter(true, wordViewModel);
149 | recyclerView.setItemAnimator(new DefaultItemAnimator() {
150 | @Override
151 | public void onAnimationFinished(@NonNull RecyclerView.ViewHolder viewHolder) {
152 | super.onAnimationFinished(viewHolder);
153 | LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
154 | if (linearLayoutManager != null) {
155 | int firstPosition = linearLayoutManager.findFirstVisibleItemPosition();
156 | int lastPosition = linearLayoutManager.findLastVisibleItemPosition();
157 | for (int i = firstPosition; i <= lastPosition; i++) {
158 | MyAdapter.MyViewHolder holder = (MyAdapter.MyViewHolder) recyclerView.findViewHolderForAdapterPosition(i);
159 | if (holder != null) {
160 | holder.textViewNumber.setText(String.valueOf(i + 1));
161 |
162 | }
163 | }
164 | }
165 |
166 | }
167 | });
168 | SharedPreferences shp = requireActivity().getSharedPreferences(VIEW_TYPE_SHP, Context.MODE_PRIVATE);
169 | boolean isUsingCardView = shp.getBoolean(IS_USING_CARD_VIEW, false);
170 | dividerItemDecoration = new DividerItemDecoration(requireActivity(),DividerItemDecoration.VERTICAL);
171 |
172 | if (isUsingCardView) {
173 | recyclerView.setAdapter(myAdapter2);
174 | } else {
175 | recyclerView.setAdapter(myAdapter1);
176 | recyclerView.addItemDecoration(dividerItemDecoration);
177 | }
178 |
179 | //recyclerView.setAdapter(myAdapter1);
180 | filteredWords = wordViewModel.getAllWordsLive();
181 | filteredWords.observe(getViewLifecycleOwner(), new Observer>() {
182 | @Override
183 | public void onChanged(List words) {
184 | int temp = myAdapter1.getItemCount();
185 | allWords = words;
186 | if (temp != words.size()) {
187 | if (temp < words.size() && !undoAction) {
188 | recyclerView.smoothScrollBy(0,-200);
189 | }
190 | undoAction = false;
191 | myAdapter1.submitList(words);
192 | myAdapter2.submitList(words);
193 | }
194 | }
195 | });
196 |
197 | new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.START | ItemTouchHelper.END) {
198 | @Override
199 |
200 | //拖拽排序的操作由于动作的和数据更新不同步,所以不能这样简单处理。拖拽速度快了就出错了。
201 |
202 | public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
203 | // Word wordFrom = allWords.get(viewHolder.getAdapterPosition());
204 | // Word wordTo = allWords.get(target.getAdapterPosition());
205 | // int idTemp = wordFrom.getId();
206 | // wordFrom.setId(wordTo.getId());
207 | // wordTo.setId(idTemp);
208 | // wordViewModel.updateWords(wordFrom,wordTo);
209 | // myAdapter1.notifyItemMoved(viewHolder.getAdapterPosition(),target.getAdapterPosition());
210 | // myAdapter2.notifyItemMoved(viewHolder.getAdapterPosition(),target.getAdapterPosition());
211 | return false;
212 | }
213 |
214 | @Override
215 | public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
216 | final Word wordToDelete = allWords.get(viewHolder.getAdapterPosition());
217 | wordViewModel.deleteWords(wordToDelete);
218 | Snackbar.make(requireActivity().findViewById(R.id.wordsFragmentView),"删除了一个词汇",Snackbar.LENGTH_SHORT)
219 | .setAction("撤销", new View.OnClickListener() {
220 | @Override
221 | public void onClick(View v) {
222 | undoAction = true;
223 | wordViewModel.insertWords(wordToDelete);
224 | }
225 | })
226 | .show();
227 |
228 | }
229 | //在滑动的时候,画出浅灰色背景和垃圾桶图标,增强删除的视觉效果
230 |
231 | Drawable icon = ContextCompat.getDrawable(requireActivity(),R.drawable.ic_delete_forever_black_24dp);
232 | Drawable background = new ColorDrawable(Color.LTGRAY);
233 | @Override
234 | public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
235 | super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
236 | View itemView = viewHolder.itemView;
237 | int iconMargin = (itemView.getHeight() - icon.getIntrinsicHeight()) / 2;
238 |
239 | int iconLeft,iconRight,iconTop,iconBottom;
240 | int backTop,backBottom,backLeft,backRight;
241 | backTop = itemView.getTop();
242 | backBottom = itemView.getBottom();
243 | iconTop = itemView.getTop() + (itemView.getHeight() - icon.getIntrinsicHeight()) /2;
244 | iconBottom = iconTop + icon.getIntrinsicHeight();
245 | if (dX > 0) {
246 | backLeft = itemView.getLeft();
247 | backRight = itemView.getLeft() + (int)dX;
248 | background.setBounds(backLeft,backTop,backRight,backBottom);
249 | iconLeft = itemView.getLeft() + iconMargin ;
250 | iconRight = iconLeft + icon.getIntrinsicWidth();
251 | icon.setBounds(iconLeft,iconTop,iconRight,iconBottom);
252 | } else if (dX < 0){
253 | backRight = itemView.getRight();
254 | backLeft = itemView.getRight() + (int)dX;
255 | background.setBounds(backLeft,backTop,backRight,backBottom);
256 | iconRight = itemView.getRight() - iconMargin;
257 | iconLeft = iconRight - icon.getIntrinsicWidth();
258 | icon.setBounds(iconLeft,iconTop,iconRight,iconBottom);
259 | } else {
260 | background.setBounds(0,0,0,0);
261 | icon.setBounds(0,0,0,0);
262 | }
263 | background.draw(c);
264 | icon.draw(c);
265 | }
266 |
267 | }).attachToRecyclerView(recyclerView);
268 |
269 | FloatingActionButton floatingActionButton = requireActivity().findViewById(R.id.floatingActionButton);
270 | floatingActionButton.setOnClickListener(new View.OnClickListener() {
271 | @Override
272 | public void onClick(View v) {
273 | NavController navController = Navigation.findNavController(v);
274 | navController.navigate(R.id.action_wordsFragment_to_addFragment);
275 | }
276 | });
277 |
278 | }
279 |
280 | }
281 |
--------------------------------------------------------------------------------
/.idea/dbnavigator.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
291 |
292 |
293 |
294 |
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 |
319 |
320 |
321 |
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
338 |
339 |
340 |
341 |
342 |
343 |
344 |
345 |
346 |
347 |
348 |
349 |
350 |
351 |
352 |
353 |
354 |
355 |
356 |
357 |
358 |
359 |
360 |
361 |
362 |
363 |
364 |
365 |
366 |
367 |
368 |
369 |
370 |
371 |
372 |
373 |
374 |
375 |
376 |
377 |
378 |
379 |
380 |
381 |
382 |
383 |
384 |
385 |
386 |
387 |
388 |
389 |
390 |
391 |
392 |
393 |
394 |
395 |
396 |
397 |
398 |
399 |
400 |
401 |
402 |
403 |
404 |
405 |
406 |
407 |
408 |
409 |
410 |
411 |
412 |
413 |
414 |
415 |
416 |
417 |
418 |
419 |
420 |
421 |
422 |
423 |
424 |
425 |
426 |
427 |
428 |
429 |
430 |
431 |
432 |
433 |
434 |
435 |
436 |
437 |
438 |
439 |
440 |
441 |
442 |
443 |
444 |
445 |
446 |
447 |
448 |
449 |
450 |
451 |
452 |
453 |
454 |
455 |
456 |
--------------------------------------------------------------------------------