Skip to content

Commit e06f77f

Browse files
committed
Optimize on recyclerview, make more consistent
1 parent 9eb610b commit e06f77f

File tree

10 files changed

+98
-36
lines changed

10 files changed

+98
-36
lines changed

.idea/deploymentTargetDropDown.xml

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,8 @@ protected void onCreate(Bundle savedInstanceState) {
2828
}
2929

3030
TextViewReadMore txtEntered = findViewById(R.id.act_detail_txtEntered);
31-
txtEntered.onClickExpand(v -> {
32-
txtEntered.toggle();
33-
});
34-
txtEntered.onClickCollapse(v -> {
35-
txtEntered.toggle();
36-
});
31+
txtEntered.onClickExpand(v -> txtEntered.toggle());
32+
txtEntered.onClickCollapse(v -> txtEntered.toggle());
3733

3834
}
3935

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,18 @@ protected void onCreate(Bundle savedInstanceState) {
6666
};
6767

6868
int [] img = new int[] {
69-
R.mipmap.img1, R.mipmap.img3, R.mipmap.img2, R.mipmap.img5, R.mipmap.img4,
70-
R.mipmap.img1, R.mipmap.img2, R.mipmap.img3, R.mipmap.img4, R.mipmap.img5,
71-
R.mipmap.img2, R.mipmap.img1
69+
R.mipmap.img1,
70+
R.mipmap.img3,
71+
R.mipmap.img2,
72+
R.mipmap.img5,
73+
R.mipmap.img4,
74+
R.mipmap.img1,
75+
R.mipmap.img2,
76+
R.mipmap.img3,
77+
R.mipmap.img4,
78+
R.mipmap.img5,
79+
R.mipmap.img2,
80+
R.mipmap.img1
7281
};
7382

7483
for (int i=0; i<arr.length; i++) {

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

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int positi
4141
item.setCollapsed(collapsed);
4242
update(position);
4343
});
44+
45+
mHolder.img.setOnClickListener(v -> update(position));
46+
4447
}
4548
}
4649

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
android:layout_width="match_parent"
1212
android:layout_height="wrap_content"
1313
android:orientation="vertical"
14-
android:layout_marginStart="15dp"
1514
android:layout_marginTop="15dp">
1615

1716
<TextView
1817
android:layout_width="wrap_content"
1918
android:layout_height="wrap_content"
2019
android:textSize="18sp"
20+
android:layout_marginStart="15dp"
21+
android:layout_marginEnd="15dp"
2122
android:text="Click to expand"
2223
android:textStyle="bold"/>
2324

@@ -27,10 +28,22 @@
2728
android:layout_height="wrap_content"
2829
android:lineSpacingExtra="3dp"
2930
android:textSize="16sp"
31+
android:layout_marginStart="15dp"
32+
android:layout_marginEnd="15dp"
3033
android:text="@string/lorem_entered"
3134
android:layout_marginTop="15dp"
3235
app:readMoreMaxLines="2"/>
3336

37+
<TextView
38+
android:layout_width="wrap_content"
39+
android:layout_height="wrap_content"
40+
android:text="Another Title"
41+
android:layout_marginStart="15dp"
42+
android:layout_marginEnd="15dp"
43+
android:textStyle="bold"
44+
android:layout_marginTop="10dp"
45+
android:textSize="18sp"/>
46+
3447
</LinearLayout>
3548

3649
</androidx.coordinatorlayout.widget.CoordinatorLayout>

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@
3737
android:layout_width="wrap_content"
3838
android:layout_height="wrap_content"
3939
android:lineSpacingExtra="3dp"
40-
android:textSize="14sp"
40+
android:textSize="15sp"
4141
android:text="@string/lorem_ipsum1"
4242
android:textColor="@color/black"
4343
android:padding="10dp"
44+
android:animationDuration="300"
4445
app:readMoreMaxLines="3"
4546
app:ellipsisType="dots"/>
4647

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<resources>
22
<string name="app_name">ReadMoreTextView</string>
33

4-
<string name="lorem_ipsum2">Long text 1 Lorem ipsum at placerat massa Maecenas sodales rutrum est sed efficitur Nunc at Nunc at placerat massa Maecenas sodales
4+
<string name="lorem_ipsum2">Long text 2 Lorem ipsum at placerat massa Maecenas sodales rutrum est sed efficitur Nunc at Nunc at placerat massa Maecenas sodales
55
placerat massa. asdfasf Maecenas sodales rutrum est sed efficitur.</string>
66

7-
<string name="lorem_ipsum1">Long text 2 Lorem ipsum dolor sit amet, consectetur adipiscing elit.
7+
<string name="lorem_ipsum1">Long text 1 Lorem ipsum dolor sit amet, consectetur adipiscing elit.
88
Maecenas sodales rutrum est sed efficitur. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
99
Fusce eget ligula sed dolor accumsan tristique id vel turpis. Nam congue nunc hendrerit eros fermentum eleifend.
1010
Sed tristique facilisis imperdiet. Donec eget interdum dui.</string>

readMoreTextView/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ afterEvaluate {
99
release(MavenPublication) {
1010
groupId = "com.github.eriffanani"
1111
artifactId = "TextViewReadMore"
12-
version = "3.0.0"
12+
version = "3.1.0"
1313
afterEvaluate {
1414
artifact(tasks.getByName("bundleReleaseAar"))
1515
}

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

+54-16
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import android.text.style.StyleSpan;
2626
import android.text.style.UnderlineSpan;
2727
import android.util.AttributeSet;
28-
import android.util.Log;
2928
import android.view.View;
3029
import android.view.ViewGroup;
3130

@@ -73,6 +72,11 @@ public class TextViewReadMore extends AppCompatTextView {
7372
private int actionClickColor = 0;
7473
private int ellipsisType = ELLIPSIS_TYPE_DOTS;
7574

75+
private int totalMarginSide = 0;
76+
77+
private SpannableStringBuilder spanCollapsed;
78+
private SpannableStringBuilder spanExpanded;
79+
7680
private static long mLastClickTime = 0;
7781

7882
public TextViewReadMore(@NonNull Context context) {
@@ -122,6 +126,10 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr) {
122126

123127
ellipsisType = typedArray.getInt(R.styleable.TextViewReadMore_ellipsisType, ELLIPSIS_TYPE_DOTS);
124128

129+
int marginStart = typedArray.getDimensionPixelSize(R.styleable.TextViewReadMore_android_layout_marginStart, 0);
130+
int marginEnd = typedArray.getDimensionPixelSize(R.styleable.TextViewReadMore_android_layout_marginEnd, 0);
131+
totalMarginSide = marginStart + marginEnd;
132+
125133
} finally {
126134
typedArray.recycle();
127135
}
@@ -154,13 +162,24 @@ public void setText(CharSequence text, BufferType type) {
154162
rebuild = true;
155163
}
156164
}
157-
super.setText(text, type);
165+
if (isEllipsized(text)) {
166+
if (spanExpanded != null && !collapsed) {
167+
super.setText(spanExpanded, type);
168+
} else if (spanCollapsed != null && collapsed) {
169+
super.setText(spanCollapsed, type);
170+
} else {
171+
super.setText(text, type);
172+
}
173+
} else {
174+
super.setText(text, type);
175+
}
158176
}
159177

160178
@Override
161179
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
162180
int givenWidth = MeasureSpec.getSize(widthMeasureSpec);
163-
lineWidth = givenWidth - getCompoundPaddingStart() - getCompoundPaddingEnd();
181+
int sidePadding = getCompoundPaddingStart() + getCompoundPaddingEnd();
182+
lineWidth = givenWidth - sidePadding - totalMarginSide;
164183
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
165184
}
166185

@@ -172,7 +191,6 @@ protected void onDraw(Canvas canvas) {
172191
}
173192

174193
private void buildSpan() {
175-
debug("mHeight("+getHeight()+") "+text);
176194
if (!TextUtils.isEmpty(text)) {
177195
isEllipsized = isEllipsized(text);
178196
if (isEllipsized) {
@@ -212,30 +230,39 @@ private void buildSpan() {
212230
finalText = truncatedText.toString().replace(DOTS_CODE, "");
213231
String collapsedText = finalText + SPACE_CODE + exp;
214232
StaticLayout layout = getStaticLayoutCollapsed(collapsedText);
215-
SpannableStringBuilder spanCollapsed = spanCollapsed(collapsedText);
233+
spanCollapsed = spanCollapsed(collapsedText);
216234
if (spanCollapsed != null) {
217235
updateParam(layout.getHeight());
218236
setText(spanCollapsed);
219237
}
238+
spanExpanded = spanExpanded();
220239
} else {
221-
String collapsedTextSpace = collapseText.replaceAll(" ", SPACE_CODE);
222-
String fullText = text + SPACE_CODE + collapsedTextSpace;
223-
SpannableStringBuilder spanExpanded = spanExpanded(fullText);
224-
StaticLayout layout = getStaticLayout(fullText);
225-
setText(spanExpanded);
240+
spanExpanded = spanExpanded();
241+
StaticLayout layout = getStaticLayout(spanExpanded.toString());
226242
updateParam(layout.getHeight());
243+
setText(spanExpanded);
227244
}
228245
setMovementMethod(LinkMovementMethod.getInstance());
229246
rebuild = false;
230247
}
231248
} else {
249+
rebuild = false;
232250
StaticLayout layout = getStaticLayout(text);
233-
updateParam(layout.getHeight());
251+
post(() -> updateParam(layout.getHeight()));
234252
}
235253
}
236254
}
237255

238-
private boolean isEllipsized(String text) {
256+
private boolean isEllipsized(@Nullable CharSequence charSequence) {
257+
if (charSequence == null)
258+
return false;
259+
StaticLayout layout = getStaticLayout(charSequence.toString());
260+
return layout.getLineCount() > maxLines;
261+
}
262+
263+
private boolean isEllipsized(@Nullable String text) {
264+
if (text == null)
265+
return false;
239266
StaticLayout layout = getStaticLayout(text);
240267
return layout.getLineCount() > maxLines;
241268
}
@@ -287,6 +314,13 @@ public void updateDrawState(@NonNull TextPaint ds) {
287314
return span;
288315
}
289316

317+
private SpannableStringBuilder spanExpanded() {
318+
String collapsedTextSpace = collapseText.replaceAll(" ", SPACE_CODE);
319+
String fullText = text + SPACE_CODE + collapsedTextSpace;
320+
spanExpanded = spanExpanded(fullText);
321+
return spanExpanded;
322+
}
323+
290324
private SpannableStringBuilder spanExpanded(String text) {
291325
SpannableStringBuilder span = new SpannableStringBuilder(text);
292326
int start = text.length() - collapseText.length();
@@ -311,7 +345,7 @@ private SpannableStringBuilder spanExpanded(String text) {
311345
public void onClick(@NonNull View widget) {
312346
if (onClickCollapse != null) {
313347
new Handler(Looper.getMainLooper()).postDelayed(
314-
() -> onClickCollapse.onClick(widget), 150
348+
() -> onClickCollapse.onClick(widget), 100
315349
);
316350
}
317351
}
@@ -364,7 +398,9 @@ public void onAnimationStart(Animator animation) {
364398
super.onAnimationStart(animation);
365399
isAnimate = true;
366400
if (collapsed) {
367-
setText(text, BufferType.SPANNABLE);
401+
collapsed = false;
402+
setText(spanExpanded);
403+
collapsed = true;
368404
}
369405
}
370406
@Override
@@ -374,7 +410,8 @@ public void onAnimationEnd(Animator animation) {
374410
collapsed = !collapsed;
375411
rebuild = true;
376412
new Handler(Looper.getMainLooper()).postDelayed(() -> {
377-
if (toggleListener != null) toggleListener.onToggle(collapsed);
413+
if (toggleListener != null)
414+
toggleListener.onToggle(collapsed);
378415
}, 0);
379416
}
380417
});
@@ -421,10 +458,11 @@ private String ellipsize(String text, int size) {
421458
}
422459

423460

461+
/* Unused function
424462
private void debug(String message) {
425463
Log.d("TextViewReadMore", message);
426464
}
427-
/* Unused function
465+
428466
private String textReplaceSpace() {
429467
return text.replaceAll(" ", SPACE_CODE);
430468
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
<flag name="dots" value="0"/>
2828
<flag name="none" value="1"/>
2929
</attr>
30+
<attr name="android:layout_marginStart"/>
31+
<attr name="android:layout_marginEnd"/>
3032
</declare-styleable>
3133

3234
</resources>

0 commit comments

Comments
 (0)