forked from HuanCheng65/TiebaLite
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e50d48
commit 3da25a4
Showing
6 changed files
with
285 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
app/src/main/java/com/huanchengfly/tieba/post/components/LinkMovementClickMethod.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.huanchengfly.tieba.post.components | ||
|
||
import android.text.Selection | ||
import android.text.Spannable | ||
import android.text.method.LinkMovementMethod | ||
import android.text.style.ClickableSpan | ||
import android.view.MotionEvent | ||
import android.widget.TextView | ||
|
||
|
||
object LinkMovementClickMethod : LinkMovementMethod() { | ||
private const val CLICK_DELAY = 500L | ||
|
||
private var lastClickTime: Long = 0 | ||
|
||
override fun canSelectArbitrarily(): Boolean { | ||
return false | ||
} | ||
|
||
override fun onTouchEvent(widget: TextView, buffer: Spannable, event: MotionEvent): Boolean { | ||
val action = event.actionMasked | ||
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) { | ||
var x = event.x.toInt() | ||
var y = event.y.toInt() | ||
x -= widget.totalPaddingLeft | ||
y -= widget.totalPaddingTop | ||
x += widget.scrollX | ||
y += widget.scrollY | ||
val layout = widget.layout | ||
val line = layout.getLineForVertical(y) | ||
val off = layout.getOffsetForHorizontal(line, x.toFloat()) | ||
val link = buffer.getSpans(off, off, ClickableSpan::class.java) | ||
if (link.isNotEmpty()) { | ||
if (action == MotionEvent.ACTION_UP) { | ||
link[0].onClick(widget) | ||
} else { | ||
Selection.setSelection(buffer, buffer.getSpanStart(link[0]), | ||
buffer.getSpanEnd(link[0])) | ||
} | ||
return true | ||
} else { | ||
Selection.removeSelection(buffer) | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
override fun initialize(widget: TextView?, text: Spannable?) { | ||
Selection.removeSelection(text) | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
app/src/main/java/com/huanchengfly/tieba/post/components/LinkTouchMovementMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.huanchengfly.tieba.post.components; | ||
|
||
import android.text.Layout; | ||
import android.text.Selection; | ||
import android.text.Spannable; | ||
import android.text.method.LinkMovementMethod; | ||
import android.text.style.ClickableSpan; | ||
import android.view.MotionEvent; | ||
import android.widget.TextView; | ||
|
||
public class LinkTouchMovementMethod extends LinkMovementMethod { | ||
|
||
private static LinkTouchMovementMethod sInstance; | ||
|
||
private ClickableSpan mPressedSpan; | ||
|
||
@Override | ||
public boolean onTouchEvent(TextView textView, Spannable spannable, MotionEvent event) { | ||
|
||
if (event.getAction() == MotionEvent.ACTION_DOWN) { | ||
mPressedSpan = getPressedSpan(textView, spannable, event); | ||
if (mPressedSpan != null) { | ||
Selection.setSelection(spannable, spannable.getSpanStart(mPressedSpan), | ||
spannable.getSpanEnd(mPressedSpan)); | ||
} | ||
} else if (event.getAction() == MotionEvent.ACTION_MOVE) { | ||
ClickableSpan touchedSpan = getPressedSpan(textView, spannable, event); | ||
if (mPressedSpan != null && touchedSpan != mPressedSpan) { | ||
mPressedSpan = null; | ||
Selection.removeSelection(spannable); | ||
} | ||
} else { | ||
if (mPressedSpan != null) { | ||
super.onTouchEvent(textView, spannable, event); | ||
} | ||
mPressedSpan = null; | ||
Selection.removeSelection(spannable); | ||
} | ||
return mPressedSpan != null; | ||
} | ||
|
||
/** | ||
* Copy from: | ||
* http://stackoverflow.com/questions/20856105/change-the-text-color-of-a-single-clickablespan-when-pressed-without-affecting-o | ||
* By: | ||
* Steven Meliopoulos | ||
*/ | ||
private ClickableSpan getPressedSpan(TextView textView, Spannable spannable, MotionEvent event) { | ||
|
||
int x = (int) event.getX(); | ||
int y = (int) event.getY(); | ||
|
||
x -= textView.getTotalPaddingLeft(); | ||
y -= textView.getTotalPaddingTop(); | ||
|
||
x += textView.getScrollX(); | ||
y += textView.getScrollY(); | ||
|
||
Layout layout = textView.getLayout(); | ||
int line = layout.getLineForVertical(y); | ||
int off = layout.getOffsetForHorizontal(line, x); | ||
|
||
ClickableSpan[] link = spannable.getSpans(off, off, ClickableSpan.class); | ||
ClickableSpan touchedSpan = null; | ||
if (link.length > 0) { | ||
touchedSpan = link[0]; | ||
} | ||
return touchedSpan; | ||
} | ||
|
||
public boolean isPressedSpan() { | ||
return mPressedSpan != null; | ||
} | ||
|
||
public static LinkTouchMovementMethod getInstance() { | ||
if (sInstance == null) { | ||
sInstance = new LinkTouchMovementMethod(); | ||
} | ||
return sInstance; | ||
} | ||
} |
Oops, something went wrong.