Skip to content

Commit be25156

Browse files
committed
update listener
1 parent 5289a10 commit be25156

File tree

8 files changed

+76
-62
lines changed

8 files changed

+76
-62
lines changed

app/src/main/java/com/erif/readmoretextview/MainActivity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class MainActivity extends AppCompatActivity {
1414
protected void onCreate(Bundle savedInstanceState) {
1515
super.onCreate(savedInstanceState);
1616
setContentView(R.layout.activity_main);
17-
TextViewReadMore2 txtReadMore = findViewById(R.id.txtReadMore);
17+
TextViewReadMore txtReadMore = findViewById(R.id.txtReadMore);
1818
txtReadMore.onClickExpand(v -> txtReadMore.toggle());
1919
txtReadMore.onClickCollapse(v -> txtReadMore.toggle());
2020

app/src/main/java/com/erif/readmoretextview/MainActivityRecyclerView.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ protected void onCreate(Bundle savedInstanceState) {
5656
List<ModelItemRecyclerView> list = new ArrayList<>();
5757
for (int i=0; i<arr.length; i++) {
5858
ModelItemRecyclerView item = new ModelItemRecyclerView(i, arr[i]);
59-
/*if (i == 0) {
59+
if (i == 3) {
6060
item.setCollapsed(false);
61-
}*/
61+
}
6262
list.add(item);
6363
}
6464
adapter.setList(list);

app/src/main/java/com/erif/readmoretextview/helper/AdapterRecyclerView.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import androidx.recyclerview.widget.RecyclerView;
1010

1111
import com.erif.readmoretextview.R;
12-
import com.erif.readmoretextview.TextViewReadMore2;
12+
import com.erif.readmoretextview.TextViewReadMore;
1313

1414
import java.util.ArrayList;
1515
import java.util.List;
@@ -32,12 +32,25 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int positi
3232
ModelItemRecyclerView item = list.get(position);
3333
mHolder.text.setText(item.getText());
3434
mHolder.text.collapsed(item.isCollapsed());
35-
mHolder.text.onClickExpand(v -> mHolder.text.toggle());
35+
/*mHolder.text.onClickExpand(v -> mHolder.text.toggle());
3636
mHolder.text.onClickCollapse(v -> mHolder.text.toggle());
3737
38-
mHolder.text.actionListener(collapsed -> {
38+
mHolder.text.toggleListener(collapsed -> {
3939
item.setCollapsed(collapsed);
4040
update(position);
41+
});*/
42+
mHolder.text.onClickExpand(v -> {
43+
boolean status = !item.isCollapsed();
44+
mHolder.text.collapsed(status);
45+
item.setCollapsed(status);
46+
update(position);
47+
});
48+
49+
mHolder.text.onClickCollapse(v -> {
50+
boolean status = !item.isCollapsed();
51+
mHolder.text.collapsed(status);
52+
item.setCollapsed(status);
53+
update(position);
4154
});
4255

4356
}
@@ -59,7 +72,7 @@ private void update(int position) {
5972
}
6073

6174
private static class Holder extends RecyclerView.ViewHolder {
62-
private final TextViewReadMore2 text;
75+
private final TextViewReadMore text;
6376
public Holder(@NonNull View itemView) {
6477
super(itemView);
6578
text = itemView.findViewById(R.id.item_recyclerView_txt);

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

+15-6
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,32 @@
77
xmlns:app="http://schemas.android.com/apk/res-auto"
88
tools:context=".MainActivity">
99

10-
<com.erif.readmoretextview.TextViewReadMore2
10+
<com.erif.readmoretextview.TextViewReadMore
1111
android:id="@+id/txtReadMore"
1212
android:layout_width="wrap_content"
1313
android:layout_height="wrap_content"
1414
android:layout_margin="12dp"
15-
android:background="#e3e3e3"
16-
app:collapsed="false"
17-
android:text="@string/lorem_ngawur_wur"
18-
app:readMoreMaxLines="2"/>
15+
app:readMoreMaxLines="2"
16+
app:expandText="@string/read_more"
17+
app:expandTextStyle="bold|italic"
18+
app:expandTextColor="@color/teal_700"
19+
app:expandTextUnderline="true"
20+
21+
app:collapseText="@string/read_less"
22+
app:collapseTextStyle="italic"
23+
app:collapseTextColor="@color/teal_700"
24+
app:collapseTextUnderline="true"
25+
app:actionClickColor="@color/teal_200"
26+
27+
android:text="@string/lorem_ngawur" />
1928

2029
<Button
2130
android:id="@+id/btnSet"
2231
android:layout_width="200dp"
2332
android:layout_height="wrap_content"
2433
android:layout_gravity="bottom|center_horizontal"
2534
android:layout_margin="12dp"
26-
android:text="Set Text" />
35+
android:text="Change Text" />
2736

2837
<com.google.android.material.floatingactionbutton.FloatingActionButton
2938
android:id="@+id/act_main_fab"

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
android:layout_height="wrap_content"
1414
android:padding="10dp">
1515

16-
<com.erif.readmoretextview.TextViewReadMore2
16+
<com.erif.readmoretextview.TextViewReadMore
1717
android:id="@+id/item_recyclerView_txt"
1818
android:layout_width="wrap_content"
1919
android:layout_height="wrap_content"

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

+3
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,7 @@
7777
Fusce eget ligula sed dolor accumsan tristique id vel turpis. Nam congue nunc hendrerit eros fermentum eleifend.
7878
Sed tristique facilisis imperdiet. Donec eget interdum dui. Sed tristique facilisis imperdiet. Donec</string>
7979

80+
<string name="read_more">Buka Dong</string>
81+
<string name="read_less">Tutup Ah</string>
82+
8083
</resources>

readMoreTextView/src/main/java/com/erif/readmoretextview/TextViewReadMore.java

+33-23
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.graphics.Typeface;
1111
import android.os.Handler;
1212
import android.os.Looper;
13+
import android.os.SystemClock;
1314
import android.text.SpannableStringBuilder;
1415
import android.text.Spanned;
1516
import android.text.StaticLayout;
@@ -30,7 +31,7 @@
3031
import androidx.appcompat.widget.AppCompatTextView;
3132
import androidx.core.content.ContextCompat;
3233

33-
public class TextViewReadMore2 extends AppCompatTextView {
34+
public class TextViewReadMore extends AppCompatTextView {
3435

3536
private static final String DEFAULT_EXPAND_TEXT = "Read More";
3637
private static final String DEFAULT_COLLAPSE_TEXT = "Close";
@@ -57,7 +58,7 @@ public class TextViewReadMore2 extends AppCompatTextView {
5758
private boolean isAnimate = false;
5859
private boolean isEllipsized = false;
5960
private int animationDuration = 200;
60-
private TextViewReadMoreCallback callback;
61+
private ToggleListener toggleListener;
6162

6263
private SpannableStringBuilder spanCollapsed;
6364
private SpannableStringBuilder spanExpanded;
@@ -66,17 +67,19 @@ public class TextViewReadMore2 extends AppCompatTextView {
6667
private View.OnClickListener onClickCollapse;
6768
private int actionClickColor = 0;
6869

69-
public TextViewReadMore2(@NonNull Context context) {
70+
private static long mLastClickTime = 0;
71+
72+
public TextViewReadMore(@NonNull Context context) {
7073
super(context);
7174
init(context, null, 0);
7275
}
7376

74-
public TextViewReadMore2(@NonNull Context context, @Nullable AttributeSet attrs) {
77+
public TextViewReadMore(@NonNull Context context, @Nullable AttributeSet attrs) {
7578
super(context, attrs);
7679
init(context, attrs, 0);
7780
}
7881

79-
public TextViewReadMore2(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
82+
public TextViewReadMore(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
8083
super(context, attrs, defStyleAttr);
8184
init(context, attrs, defStyleAttr);
8285
}
@@ -85,30 +88,30 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr) {
8588
Resources.Theme theme = context.getTheme();
8689
if (theme != null) {
8790
TypedArray typedArray = theme.obtainStyledAttributes(
88-
attrs, R.styleable.TextViewReadMore2, defStyleAttr, 0
91+
attrs, R.styleable.TextViewReadMore, defStyleAttr, 0
8992
);
9093
try {
91-
text = typedArray.getString(R.styleable.TextViewReadMore2_android_text);
92-
int getMaxLines = typedArray.getInt(R.styleable.TextViewReadMore2_readMoreMaxLines, 2);
94+
text = typedArray.getString(R.styleable.TextViewReadMore_android_text);
95+
int getMaxLines = typedArray.getInt(R.styleable.TextViewReadMore_readMoreMaxLines, 2);
9396
maxLines = Math.max(getMaxLines, 1);
94-
collapsed = typedArray.getBoolean(R.styleable.TextViewReadMore2_collapsed, true);
97+
collapsed = typedArray.getBoolean(R.styleable.TextViewReadMore_collapsed, true);
9598

96-
String getExpandText = typedArray.getString(R.styleable.TextViewReadMore2_expandText);
99+
String getExpandText = typedArray.getString(R.styleable.TextViewReadMore_expandText);
97100
expandText = TextUtils.isEmpty(getExpandText) ? DEFAULT_EXPAND_TEXT : getExpandText;
98-
expandTextColor = typedArray.getColor(R.styleable.TextViewReadMore2_expandTextColor, Color.BLUE);
99-
expandTextStyle = typedArray.getInt(R.styleable.TextViewReadMore2_expandTextStyle, 0);
100-
expandTextUnderline = typedArray.getBoolean(R.styleable.TextViewReadMore2_expandTextUnderline, expandTextUnderline);
101+
expandTextColor = typedArray.getColor(R.styleable.TextViewReadMore_expandTextColor, Color.BLUE);
102+
expandTextStyle = typedArray.getInt(R.styleable.TextViewReadMore_expandTextStyle, 0);
103+
expandTextUnderline = typedArray.getBoolean(R.styleable.TextViewReadMore_expandTextUnderline, expandTextUnderline);
101104

102-
String getCollapseText = typedArray.getString(R.styleable.TextViewReadMore2_collapseText);
105+
String getCollapseText = typedArray.getString(R.styleable.TextViewReadMore_collapseText);
103106
collapseText = getCollapseText == null ? DEFAULT_COLLAPSE_TEXT : getCollapseText;
104-
collapseTextColor = typedArray.getColor(R.styleable.TextViewReadMore2_collapseTextColor, Color.BLUE);
105-
collapseTextStyle = typedArray.getInt(R.styleable.TextViewReadMore2_collapseTextStyle, 0);
106-
collapseTextUnderline = typedArray.getBoolean(R.styleable.TextViewReadMore2_collapseTextUnderline, collapseTextUnderline);
107+
collapseTextColor = typedArray.getColor(R.styleable.TextViewReadMore_collapseTextColor, Color.BLUE);
108+
collapseTextStyle = typedArray.getInt(R.styleable.TextViewReadMore_collapseTextStyle, 0);
109+
collapseTextUnderline = typedArray.getBoolean(R.styleable.TextViewReadMore_collapseTextUnderline, collapseTextUnderline);
107110

108111
int defaultActionClickColor = ContextCompat.getColor(context, R.color.text_view_read_more_button_hover_color);
109-
actionClickColor = typedArray.getColor(R.styleable.TextViewReadMore2_actionClickColor, defaultActionClickColor);
112+
actionClickColor = typedArray.getColor(R.styleable.TextViewReadMore_actionClickColor, defaultActionClickColor);
110113

111-
int getAnimationDuration = typedArray.getInt(R.styleable.TextViewReadMore2_android_animationDuration, animationDuration);
114+
int getAnimationDuration = typedArray.getInt(R.styleable.TextViewReadMore_android_animationDuration, animationDuration);
112115
if (getAnimationDuration > 1000) {
113116
animationDuration = 1000;
114117
} else animationDuration = Math.max(getAnimationDuration, 100);
@@ -311,11 +314,14 @@ public void updateDrawState(@NonNull TextPaint ds) {
311314
invalidate();
312315
}
313316
}, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
314-
//setMovementMethod(LinkMovementMethod.getInstance());
315317
return span;
316318
}
317319

318320
public void toggle() {
321+
if (SystemClock.elapsedRealtime() - mLastClickTime <= animationDuration) { // 1000 = 1second
322+
return;
323+
}
324+
mLastClickTime = SystemClock.elapsedRealtime();
319325
setMovementMethod(null);
320326
int start = collapsed ? halfHeight : fullHeight;
321327
int end = collapsed ? fullHeight : halfHeight;
@@ -341,7 +347,7 @@ public void onAnimationEnd(Animator animation) {
341347
collapsed = !collapsed;
342348
rebuild = true;
343349
new Handler(Looper.getMainLooper()).postDelayed(() -> {
344-
if (callback != null) callback.actionListener(collapsed);
350+
if (toggleListener != null) toggleListener.onToggle(collapsed);
345351
}, 100);
346352
}
347353
});
@@ -367,8 +373,8 @@ private void underlineText(SpannableStringBuilder builder, int start, int end) {
367373
);
368374
}
369375

370-
public void actionListener(TextViewReadMoreCallback callback) {
371-
this.callback = callback;
376+
public void toggleListener(ToggleListener toggleListener) {
377+
this.toggleListener = toggleListener;
372378
}
373379
public void onClickExpand(View.OnClickListener onClickExpand) {
374380
this.onClickExpand = onClickExpand;
@@ -381,4 +387,8 @@ private void debug(String message) {
381387
Log.d("TextViewReadMore", message);
382388
}
383389

390+
public interface ToggleListener {
391+
public void onToggle(boolean collapsed);
392+
}
393+
384394
}

readMoreTextView/src/main/res/values/attrs.xml

+4-25
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3+
34
<declare-styleable name="TextViewReadMore">
4-
<attr name="android:text"/> <!---->
5-
<attr name="android:maxLines"/> <!---->
6-
<attr name="collapsed" format="boolean"/> <!---->
5+
<attr name="android:text"/>
6+
<attr name="readMoreMaxLines" format="integer"/>
7+
<attr name="collapsed" format="boolean"/>
78
<attr name="expandText" format="string"/>
89
<attr name="expandTextColor" format="reference|color"/>
910
<attr name="expandTextUnderline" format="boolean"/>
10-
<attr name="collapsable" format="boolean"/>
1111
<attr name="expandTextStyle">
1212
<flag name="normal" value="0" />
1313
<flag name="bold" value="1" />
@@ -22,27 +22,6 @@
2222
<flag name="italic" value="2" />
2323
</attr>
2424
<attr name="android:animationDuration"/>
25-
<!--<attr name="buttonPosition">
26-
<enum name="inline" value="0"/>
27-
<enum name="start" value="1"/>
28-
<enum name="center" value="2"/>
29-
<enum name="end" value="3"/>
30-
</attr>-->
31-
</declare-styleable>
32-
33-
<declare-styleable name="TextViewReadMore2">
34-
<attr name="android:text"/>
35-
<attr name="readMoreMaxLines" format="integer"/>
36-
<attr name="collapsed"/>
37-
<attr name="expandText"/>
38-
<attr name="expandTextColor"/>
39-
<attr name="expandTextUnderline"/>
40-
<attr name="expandTextStyle"/>
41-
<attr name="collapseText"/>
42-
<attr name="collapseTextColor"/>
43-
<attr name="collapseTextUnderline"/>
44-
<attr name="collapseTextStyle"/>
45-
<attr name="android:animationDuration"/>
4625
<attr name="actionClickColor" format="reference|color"/>
4726
</declare-styleable>
4827

0 commit comments

Comments
 (0)