From 151c16ad83fedae76e53f0ae29b3b7f1bc1e878e Mon Sep 17 00:00:00 2001 From: seven332 Date: Wed, 25 Jun 2014 21:12:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=B8=A6=E6=9C=89=E9=93=BE?= =?UTF-8?q?=E6=8E=A5=E7=9A=84=E8=AF=84=E8=AE=BA=E6=97=A0=E9=80=89=E4=B8=AD?= =?UTF-8?q?=E5=8F=8D=E5=BA=94=EF=BC=8C=E9=95=BF=E6=8C=89=E4=BB=8D=E4=BC=9A?= =?UTF-8?q?=E6=89=93=E5=BC=80=E9=93=BE=E6=8E=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- res/layout/comments_item.xml | 5 +- .../ehviewer/ui/CommentsSectionFragment.java | 22 +++- .../ehviewer/widget/LinkifyTextView.java | 101 ++++++++++++++++++ 3 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 src/com/hippo/ehviewer/widget/LinkifyTextView.java diff --git a/res/layout/comments_item.xml b/res/layout/comments_item.xml index 6cff2727f..550ea108b 100644 --- a/res/layout/comments_item.xml +++ b/res/layout/comments_item.xml @@ -36,7 +36,7 @@ android:layout_marginLeft="12.0dp" android:layout_marginRight="12.0dp" /> - + android:layout_marginBottom="8.0dp" /> \ No newline at end of file diff --git a/src/com/hippo/ehviewer/ui/CommentsSectionFragment.java b/src/com/hippo/ehviewer/ui/CommentsSectionFragment.java index 34c4a0454..d4536d3c5 100644 --- a/src/com/hippo/ehviewer/ui/CommentsSectionFragment.java +++ b/src/com/hippo/ehviewer/ui/CommentsSectionFragment.java @@ -22,6 +22,7 @@ import com.hippo.ehviewer.R; import com.hippo.ehviewer.data.Comment; import com.hippo.ehviewer.widget.DialogBuilder; import com.hippo.ehviewer.widget.FswView; +import com.hippo.ehviewer.widget.LinkifyTextView; import com.hippo.ehviewer.widget.OnFitSystemWindowsListener; import com.hippo.ehviewer.widget.SuperToast; @@ -31,6 +32,7 @@ import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; import android.content.Intent; +import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.text.Html; @@ -43,7 +45,7 @@ import android.widget.ListView; import android.widget.TextView; public class CommentsSectionFragment extends Fragment - implements AdapterView.OnItemLongClickListener { + implements AdapterView.OnItemLongClickListener, AdapterView.OnItemClickListener { @SuppressWarnings("unused") private static final String TAG = "CommentsSectionFragment"; @@ -93,6 +95,7 @@ public class CommentsSectionFragment extends Fragment }); mList.setAdapter(adapter); + mList.setOnItemClickListener(this); mList.setOnItemLongClickListener(this); mComments = mActivity.getComments(); @@ -111,6 +114,20 @@ public class CommentsSectionFragment extends Fragment mWaitView.setVisibility(View.GONE); } + @Override + public void onItemClick(AdapterView parent, View view, int position, + long id) { + // Handler url here + LinkifyTextView comment = ((LinkifyTextView)view.findViewById(R.id.comment)); + String url = comment.getTouchUrl(); + if (url != null) { + comment.clearTouchUrl(); + Uri uri = Uri.parse(url); + Intent intent = new Intent(Intent.ACTION_VIEW, uri); + startActivity(intent); + } + } + @Override public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { @@ -177,9 +194,10 @@ public class CommentsSectionFragment extends Fragment boolean isUserSame = true; boolean isTimeSame = true; - if (convertView == null) + if (convertView == null) { convertView = LayoutInflater.from(mActivity) .inflate(R.layout.comments_item, null); + } Comment c = mComments.get(position); TextView user = (TextView)convertView.findViewById(R.id.user); diff --git a/src/com/hippo/ehviewer/widget/LinkifyTextView.java b/src/com/hippo/ehviewer/widget/LinkifyTextView.java new file mode 100644 index 000000000..238102758 --- /dev/null +++ b/src/com/hippo/ehviewer/widget/LinkifyTextView.java @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2014 Hippo Seven + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hippo.ehviewer.widget; + +import android.content.Context; +import android.text.Layout; +import android.text.Spannable; +import android.text.style.ClickableSpan; +import android.text.style.URLSpan; +import android.text.util.Linkify; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.widget.TextView; + +public class LinkifyTextView extends TextView { + + private String mUrl; + + public LinkifyTextView(Context context) { + super(context); + init(); + } + + public LinkifyTextView(Context context, AttributeSet attrs) { + super(context, attrs); + init(); + } + + public LinkifyTextView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + init(); + } + + public void init() { + setAutoLinkMask(Linkify.WEB_URLS); + setLinksClickable(false); + } + + public String getTouchUrl() { + return mUrl; + } + + /** + * Call it when you do not need the url any more + */ + public void clearTouchUrl() { + mUrl = null; + } + + /** + * Get some code from android.text.method.LinkMovementMethod. Work fine ! + */ + @Override + public boolean onTouchEvent(MotionEvent event) { + if (getText() instanceof Spannable) { + int x = (int) event.getX(); + int y = (int) event.getY(); + boolean isGetUrl = false; + + x -= getTotalPaddingLeft(); + y -= getTotalPaddingTop(); + + x += getScrollX(); + y += getScrollY(); + + Layout layout = getLayout(); + int line = layout.getLineForVertical(y); + int off = layout.getOffsetForHorizontal(line, x); + + ClickableSpan[] links = ((Spannable)getText()).getSpans(off, off, ClickableSpan.class); + + for (ClickableSpan link : links) { + if (link instanceof URLSpan) { + mUrl = ((URLSpan)link).getURL(); + isGetUrl = true; + break; + } + } + if (!isGetUrl) + mUrl = null; + } else { + mUrl = null; + } + + return super.onTouchEvent(event); + } +}