Skip to content

Commit

Permalink
Merge pull request #1204 from AllenCoder/master
Browse files Browse the repository at this point in the history
1. fix #1223
  • Loading branch information
CymChad authored Jun 23, 2017
2 parents b773955 + 53f1c5e commit fc4c282
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.chad.baserecyclerviewadapterhelper.adapter;

import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import com.chad.baserecyclerviewadapterhelper.R;
import com.chad.baserecyclerviewadapterhelper.data.DataServer;
import com.chad.baserecyclerviewadapterhelper.entity.Status;
import com.chad.baserecyclerviewadapterhelper.util.ClickableMovementMethod;
import com.chad.baserecyclerviewadapterhelper.util.SpannableStringUtils;
import com.chad.baserecyclerviewadapterhelper.util.ToastUtils;
import com.chad.baserecyclerviewadapterhelper.util.Utils;
Expand All @@ -25,28 +25,31 @@
*/
public class AnimationAdapter extends BaseQuickAdapter<Status, BaseViewHolder> {
public AnimationAdapter() {
super( R.layout.layout_animation, DataServer.getSampleData(100));
super(R.layout.layout_animation, DataServer.getSampleData(100));
}

@Override
protected void convert(BaseViewHolder helper, Status item) {
helper.addOnClickListener(R.id.img).addOnClickListener(R.id.tweetText).addOnClickListener(R.id.tweetName);
switch (helper.getLayoutPosition()%
3){
helper.addOnClickListener(R.id.img).addOnClickListener(R.id.tweetName);
switch (helper.getLayoutPosition() %
3) {
case 0:
helper.setImageResource(R.id.img,R.mipmap.animation_img1);
helper.setImageResource(R.id.img, R.mipmap.animation_img1);
break;
case 1:
helper.setImageResource(R.id.img,R.mipmap.animation_img2);
helper.setImageResource(R.id.img, R.mipmap.animation_img2);
break;
case 2:
helper.setImageResource(R.id.img,R.mipmap.animation_img3);
helper.setImageResource(R.id.img, R.mipmap.animation_img3);
break;
}
helper.setText(R.id.tweetName,"Hoteis in Rio de Janeiro");
String msg="\"He was one of Australia's most of distinguished artistes, renowned for his portraits\"";
( (TextView)helper.getView(R.id.tweetText)).setText(SpannableStringUtils.getBuilder(msg).append("landscapes and nedes").setClickSpan(clickableSpan).create());
( (TextView)helper.getView(R.id.tweetText)).setMovementMethod(LinkMovementMethod.getInstance());
helper.setText(R.id.tweetName, "Hoteis in Rio de Janeiro");
String msg = "\"He was one of Australia's most of distinguished artistes, renowned for his portraits\"";
((TextView) helper.getView(R.id.tweetText)).setText(SpannableStringUtils.getBuilder(msg).append("landscapes and nedes").setClickSpan(clickableSpan).create());
((TextView) helper.getView(R.id.tweetText)).setMovementMethod(ClickableMovementMethod.getInstance());
((TextView) helper.getView(R.id.tweetText)).setFocusable(false);
((TextView) helper.getView(R.id.tweetText)).setClickable(false);
((TextView) helper.getView(R.id.tweetText)).setLongClickable(false);
}

ClickableSpan clickableSpan = new ClickableSpan() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.chad.baserecyclerviewadapterhelper.util;

import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.BaseMovementMethod;
import android.text.style.ClickableSpan;
import android.view.MotionEvent;
import android.widget.TextView;

public class ClickableMovementMethod extends BaseMovementMethod {

private static ClickableMovementMethod sInstance;

public static ClickableMovementMethod getInstance() {
if (sInstance == null) {
sInstance = new ClickableMovementMethod();
}
return sInstance;
}

@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {

int action = event.getActionMasked();
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {

int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();

Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);

ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
if (link.length > 0) {
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
public void initialize(TextView widget, Spannable text) {
Selection.removeSelection(text);
}
}
12 changes: 7 additions & 5 deletions app/src/main/res/layout/activity_item_click.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/activity_item_click"
xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_item_click"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context="com.chad.baserecyclerviewadapterhelper.ItemClickActivity">

<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"/>
android:layout_height="match_parent" />
</RelativeLayout>

0 comments on commit fc4c282

Please sign in to comment.