Add ability to specify MovementMethod when applying markdown to a TextView
This commit is contained in:
parent
10b847a100
commit
27683ea11f
@ -31,6 +31,7 @@ dependencies {
|
||||
implementation project(':library-image-loader')
|
||||
|
||||
implementation 'ru.noties:debug:3.0.0@jar'
|
||||
implementation 'me.saket:better-link-movement-method:2.2.0'
|
||||
|
||||
implementation OK_HTTP
|
||||
|
||||
|
@ -4,11 +4,14 @@ import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import me.saket.bettermovementmethod.BetterLinkMovementMethod;
|
||||
import ru.noties.debug.Debug;
|
||||
|
||||
public class MainActivity extends Activity {
|
||||
@ -64,7 +67,7 @@ public class MainActivity extends Activity {
|
||||
markdownRenderer.render(MainActivity.this, uri(), text, new MarkdownRenderer.MarkdownReadyListener() {
|
||||
@Override
|
||||
public void onMarkdownReady(CharSequence markdown) {
|
||||
Markwon.setText(textView, markdown);
|
||||
Markwon.setText(textView, markdown, BetterLinkMovementMethod.getInstance());
|
||||
Views.setVisible(progress, false);
|
||||
}
|
||||
});
|
||||
@ -72,6 +75,7 @@ public class MainActivity extends Activity {
|
||||
});
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private AppBarItem.State appBarState() {
|
||||
|
||||
final String title;
|
||||
@ -100,6 +104,7 @@ public class MainActivity extends Activity {
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Uri uri() {
|
||||
final Intent intent = getIntent();
|
||||
return intent != null
|
||||
|
@ -17,6 +17,7 @@
|
||||
android:layout_margin="16dip"
|
||||
android:lineSpacingExtra="2dip"
|
||||
android:textSize="16sp"
|
||||
android:textIsSelectable="true"
|
||||
tools:context="ru.noties.markwon.MainActivity"
|
||||
tools:text="yo\nman" />
|
||||
|
||||
|
@ -2,7 +2,9 @@ package ru.noties.markwon;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.text.method.LinkMovementMethod;
|
||||
import android.text.method.MovementMethod;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension;
|
||||
@ -67,20 +69,39 @@ public abstract class Markwon {
|
||||
|
||||
/**
|
||||
* Helper method to apply parsed markdown.
|
||||
* <p>
|
||||
* Since 1.0.6 redirects it\'s call to {@link #setText(TextView, CharSequence, MovementMethod)}
|
||||
* with LinkMovementMethod as an argument to preserve current API.
|
||||
*
|
||||
* @param view {@link TextView} to set markdown into
|
||||
* @param text parsed markdown
|
||||
* @see #scheduleDrawables(TextView)
|
||||
* @see #scheduleTableRows(TextView)
|
||||
* @see #setText(TextView, CharSequence, MovementMethod)
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static void setText(@NonNull TextView view, CharSequence text) {
|
||||
setText(view, text, LinkMovementMethod.getInstance());
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to apply parsed markdown with additional argument of a MovementMethod. Used
|
||||
* to workaround problems that occur when using system LinkMovementMethod (for example:
|
||||
* https://issuetracker.google.com/issues/37068143). As a better alternative to it consider
|
||||
* using: https://github.com/saket/Better-Link-Movement-Method
|
||||
*
|
||||
* @param view TextView to set markdown into
|
||||
* @param text parsed markdown
|
||||
* @param movementMethod an implementation if MovementMethod or null
|
||||
* @see #scheduleDrawables(TextView)
|
||||
* @see #scheduleTableRows(TextView)
|
||||
* @since 1.0.6
|
||||
*/
|
||||
public static void setText(@NonNull TextView view, CharSequence text, @Nullable MovementMethod movementMethod) {
|
||||
|
||||
unscheduleDrawables(view);
|
||||
unscheduleTableRows(view);
|
||||
|
||||
// update movement method (for links to be clickable)
|
||||
view.setMovementMethod(LinkMovementMethod.getInstance());
|
||||
view.setMovementMethod(movementMethod);
|
||||
view.setText(text);
|
||||
|
||||
// schedule drawables (dynamic drawables that can change bounds/animate will be correctly updated)
|
||||
|
Loading…
x
Reference in New Issue
Block a user