修正带有链接的评论无选中反应,长按仍会打开链接的问题

This commit is contained in:
seven332 committed 2014-06-25 21:12:13 +08:00
1 parent 07d1cc6bf6
commit 151c16ad83
3 files changed
+123 -5

No files matched your search

+2 -3
View File
@@ -36,7 +36,7 @@
android:layout_marginLeft="12.0dp"
android:layout_marginRight="12.0dp" />
<TextView
<com.hippo.ehviewer.widget.LinkifyTextView
android:id="@+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@@ -44,7 +44,6 @@
android:layout_below="@id/user"
android:layout_marginTop="8.0dp"
android:layout_marginRight="12.0dp"
android:layout_marginBottom="8.0dp"
android:autoLink="web" />
android:layout_marginBottom="8.0dp" />
</RelativeLayout>
@@ -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);
@@ -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);
}
}