Skip to content

Commit 8fcdff9

Browse files
committed
add restore() method
1 parent 136f4b5 commit 8fcdff9

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

LineMatchingView/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
defaultConfig {
99
minSdk 16
1010
targetSdk 30
11-
versionCode 1
12-
versionName "1.0"
11+
versionCode 2
12+
versionName "1.1"
1313

1414
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1515
consumerProguardFiles "consumer-rules.pro"

LineMatchingView/src/main/java/com/zaaach/linematchingview/LineMatchingView.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,34 @@ public LineMatchingView<T> init(@NonNull LinkableAdapter<T> adapter){
8383
return this;
8484
}
8585

86+
public void restore(){
87+
finished = false;
88+
oldLines.addAll(newLines);
89+
newLines.clear();
90+
invalidate();
91+
if (leftItems != null){
92+
for (int i = 0; i < leftItems.size(); i++) {
93+
leftItems.get(i).lined = false;
94+
leftItems.get(i).line = null;
95+
notifyItemStateChanged(i, NORMAL, true);
96+
}
97+
}
98+
if (rightItems != null){
99+
for (int i = 0; i < rightItems.size(); i++) {
100+
rightItems.get(i).lined = false;
101+
rightItems.get(i).line = null;
102+
notifyItemStateChanged(i, NORMAL, false);
103+
}
104+
}
105+
}
106+
107+
public boolean isFinished(){
108+
return finished;
109+
}
110+
86111
public void setItems(@NonNull List<T> left, @NonNull List<T> right){
87112
if (linkableAdapter == null) {
88-
throw new IllegalStateException("LinkableAdapter must not be null, please see method setLinkableAdapter()");
113+
throw new IllegalStateException("LinkableAdapter must not be null, please see method init()");
89114
}
90115
leftItems = new ArrayList<>();
91116
rightItems = new ArrayList<>();

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ android {
99
applicationId "com.zaaach.demo"
1010
minSdk 16
1111
targetSdk 30
12-
versionCode 1
13-
versionName "1.0"
12+
versionCode 2
13+
versionName "1.1"
1414

1515
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1616
}

app/src/main/java/com/zaaach/demo/MainActivity.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import android.view.LayoutInflater;
88
import android.view.View;
99
import android.view.ViewGroup;
10+
import android.widget.Button;
1011
import android.widget.ImageView;
1112
import android.widget.TextView;
13+
import android.widget.Toast;
1214

1315
import com.zaaach.linematchingview.LineMatchingView;
1416

@@ -24,6 +26,17 @@ protected void onCreate(Bundle savedInstanceState) {
2426
setContentView(R.layout.activity_main);
2527

2628
lineMatchingView = findViewById(R.id.line_matching_view);
29+
Button btnRetry = findViewById(R.id.btn_retry);
30+
btnRetry.setOnClickListener(new View.OnClickListener() {
31+
@Override
32+
public void onClick(View v) {
33+
if (lineMatchingView.isFinished()){
34+
lineMatchingView.restore();
35+
}else {
36+
Toast.makeText(MainActivity.this, "连线未完成", Toast.LENGTH_SHORT).show();
37+
}
38+
}
39+
});
2740

2841
List<ItemInfo> left = new ArrayList<>();
2942
left.add(new ItemInfo(ItemInfo.TEXT, "草莓"));

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
android:layout_height="match_parent"
66
xmlns:app="http://schemas.android.com/apk/res-auto"
77
android:gravity="center"
8+
android:orientation="vertical"
89
tools:context=".MainActivity">
910

1011
<com.zaaach.linematchingview.LineMatchingView
@@ -22,4 +23,11 @@
2223
app:lmv_line_color_correct="@color/correct_color"
2324
app:lmv_line_color_error="@color/error_color"/>
2425

26+
<Button
27+
android:id="@+id/btn_retry"
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content"
30+
android:layout_marginTop="16dp"
31+
android:text="重做"/>
32+
2533
</LinearLayout>

0 commit comments

Comments
 (0)