From 3da25a4c4982d9176a1efefdbb98d37a34a9a83b Mon Sep 17 00:00:00 2001 From: HuanChengFly <609486518@qq.com> Date: Thu, 20 Aug 2020 16:20:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=95=BF=E6=8C=89?= =?UTF-8?q?=E8=B4=B4=E4=B8=AD=E6=96=87=E6=9C=AC=E6=97=A0=E6=B3=95=E5=BC=B9?= =?UTF-8?q?=E5=87=BA=E8=8F=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/adapters/RecyclerFloorAdapter.java | 4 +- .../post/adapters/RecyclerThreadAdapter.java | 74 ++++++++--------- .../components/LinkMovementClickMethod.kt | 52 ++++++++++++ .../components/LinkTouchMovementMethod.java | 81 +++++++++++++++++++ .../post/widgets/MySpannableTextView.java | 38 +++++++++ .../theme/TintMySpannableTextView.java | 78 ++++++++++++++++++ 6 files changed, 285 insertions(+), 42 deletions(-) create mode 100644 app/src/main/java/com/huanchengfly/tieba/post/components/LinkMovementClickMethod.kt create mode 100644 app/src/main/java/com/huanchengfly/tieba/post/components/LinkTouchMovementMethod.java create mode 100644 app/src/main/java/com/huanchengfly/tieba/post/widgets/MySpannableTextView.java create mode 100644 app/src/main/java/com/huanchengfly/tieba/post/widgets/theme/TintMySpannableTextView.java diff --git a/app/src/main/java/com/huanchengfly/tieba/post/adapters/RecyclerFloorAdapter.java b/app/src/main/java/com/huanchengfly/tieba/post/adapters/RecyclerFloorAdapter.java index ebdd406d..c99da22f 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/adapters/RecyclerFloorAdapter.java +++ b/app/src/main/java/com/huanchengfly/tieba/post/adapters/RecyclerFloorAdapter.java @@ -7,7 +7,6 @@ import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.TextUtils; import android.text.format.DateUtils; -import android.text.method.LinkMovementMethod; import android.util.DisplayMetrics; import android.util.Log; import android.view.Gravity; @@ -26,6 +25,7 @@ import com.huanchengfly.tieba.post.api.TiebaApi; import com.huanchengfly.tieba.post.api.models.CommonResponse; import com.huanchengfly.tieba.post.api.models.SubFloorListBean; import com.huanchengfly.tieba.post.api.models.ThreadContentBean; +import com.huanchengfly.tieba.post.components.LinkMovementClickMethod; import com.huanchengfly.tieba.post.components.spans.MyURLSpan; import com.huanchengfly.tieba.post.components.spans.MyUserSpan; import com.huanchengfly.tieba.post.fragments.ConfirmDialogFragment; @@ -236,7 +236,7 @@ public class RecyclerFloorAdapter extends CommonBaseAdapter userInfoBeanMap; private NavigationHelper navigationHelper; - private RequestOptions avatarRequestOptions; private LinearLayout.LayoutParams defaultLayoutParams; - private RequestOptions defaultRequestOptions; - private DrawableTransitionOptions transitionOptions; + private LinearLayout.LayoutParams defaultLayoutParamsWithNoMargins; private ThreadContentBean.ThreadBean threadBean; private ThreadContentBean dataBean; private TreeMap> photoViewBeansMap; @@ -112,19 +110,12 @@ public class RecyclerThreadAdapter extends MultiBaseAdapter(); - avatarRequestOptions = new RequestOptions() - .placeholder(R.drawable.bg_placeholder_circle) - .circleCrop() - .skipMemoryCache(true); navigationHelper = NavigationHelper.newInstance(mContext); immersive = false; this.screenWidthPx = BaseApplication.ScreenInfo.EXACT_SCREEN_WIDTH; defaultLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); defaultLayoutParams.setMargins(0, 8, 0, 8); - defaultRequestOptions = new RequestOptions() - .placeholder(R.drawable.bg_placeholder) - .skipMemoryCache(true); - transitionOptions = DrawableTransitionOptions.withCrossFade(); + defaultLayoutParamsWithNoMargins = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); photoViewBeansMap = new TreeMap<>(); blockCacheMap = new HashMap<>(); } @@ -217,7 +208,7 @@ public class RecyclerThreadAdapter extends MultiBaseAdapter postListItemBeans) { - photoViewBeansMap = new TreeMap>(); + photoViewBeansMap = new TreeMap<>(); addPic(postListItemBeans); } @@ -260,24 +251,22 @@ public class RecyclerThreadAdapter extends MultiBaseAdapter= 1 && "10".equals(subPostListItemBean.getContent().get(0).getType())) { + if (subPostListItemBean.getContent().size() > 0 && "10".equals(subPostListItemBean.getContent().get(0).getType())) { String voiceUrl = "http://c.tieba.baidu.com/c/p/voice?voice_md5=" + subPostListItemBean.getContent().get(0).getVoiceMD5() + "&play_from=pb_voice_play"; RelativeLayout container = new RelativeLayout(mContext); - container.setLayoutParams(defaultLayoutParams); + container.setLayoutParams(defaultLayoutParamsWithNoMargins); container.setPadding(DisplayUtil.dp2px(mContext, 8), - 1, + 8, DisplayUtil.dp2px(mContext, 8), - 1); + 8); container.setBackground(Util.getDrawableByAttr(mContext, R.attr.selectableItemBackground)); container.setOnClickListener(view -> mContext.startActivity(new Intent(mContext, ReplyActivity.class) .putExtra("data", new ReplyInfoBean(dataBean.getThread().getId(), @@ -303,7 +292,7 @@ public class RecyclerThreadAdapter extends MultiBaseAdapter mContext.startActivity(new Intent(mContext, ReplyActivity.class) .putExtra("data", new ReplyInfoBean(dataBean.getThread().getId(), @@ -393,10 +382,6 @@ public class RecyclerThreadAdapter extends MultiBaseAdapter showMenu(data, position)); - if (data.getFloor().equals("1")) + if ("1".equals(data.getFloor())) holder.setVisibility(R.id.thread_list_item_reply, View.GONE); else holder.setVisibility(R.id.thread_list_item_reply, View.VISIBLE); @@ -591,11 +576,11 @@ public class RecyclerThreadAdapter extends MultiBaseAdapter getContentViews(ThreadContentBean.PostListItemBean postListItemBean) { + private List getContentViews(ThreadContentBean.PostListItemBean postListItemBean, int position) { List views = new ArrayList<>(); for (ThreadContentBean.ContentBean contentBean : postListItemBean.getContent()) { switch (contentBean.getType()) { @@ -895,10 +889,10 @@ public class RecyclerThreadAdapter extends MultiBaseAdapter 0) { + touchedSpan = link[0]; + } + return touchedSpan; + } + + public boolean isPressedSpan() { + return mPressedSpan != null; + } + + public static LinkTouchMovementMethod getInstance() { + if (sInstance == null) { + sInstance = new LinkTouchMovementMethod(); + } + return sInstance; + } +} diff --git a/app/src/main/java/com/huanchengfly/tieba/post/widgets/MySpannableTextView.java b/app/src/main/java/com/huanchengfly/tieba/post/widgets/MySpannableTextView.java new file mode 100644 index 00000000..e20f3a73 --- /dev/null +++ b/app/src/main/java/com/huanchengfly/tieba/post/widgets/MySpannableTextView.java @@ -0,0 +1,38 @@ +package com.huanchengfly.tieba.post.widgets; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.MotionEvent; + +import androidx.annotation.Nullable; +import androidx.appcompat.widget.AppCompatTextView; + +import com.huanchengfly.tieba.post.components.LinkTouchMovementMethod; + +public class MySpannableTextView extends AppCompatTextView { + + private LinkTouchMovementMethod mLinkTouchMovementMethod; + + public MySpannableTextView(Context context) { + super(context); + } + + public MySpannableTextView(Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + } + + public MySpannableTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + @Override + public boolean onTouchEvent(MotionEvent event) { + boolean result = super.onTouchEvent(event); + return mLinkTouchMovementMethod != null ? mLinkTouchMovementMethod.isPressedSpan() : result; + } + + public void setLinkTouchMovementMethod(LinkTouchMovementMethod linkTouchMovementMethod) { + mLinkTouchMovementMethod = linkTouchMovementMethod; + setMovementMethod(linkTouchMovementMethod); + } +} diff --git a/app/src/main/java/com/huanchengfly/tieba/post/widgets/theme/TintMySpannableTextView.java b/app/src/main/java/com/huanchengfly/tieba/post/widgets/theme/TintMySpannableTextView.java new file mode 100644 index 00000000..2b06495a --- /dev/null +++ b/app/src/main/java/com/huanchengfly/tieba/post/widgets/theme/TintMySpannableTextView.java @@ -0,0 +1,78 @@ +package com.huanchengfly.tieba.post.widgets.theme; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.content.res.ColorStateList; +import android.content.res.TypedArray; +import android.util.AttributeSet; + +import com.huanchengfly.tieba.post.R; +import com.huanchengfly.tieba.post.ui.theme.interfaces.Tintable; +import com.huanchengfly.tieba.post.ui.theme.utils.ColorStateListUtils; +import com.huanchengfly.tieba.post.ui.theme.utils.ThemeUtils; +import com.huanchengfly.tieba.post.widgets.MySpannableTextView; + +@SuppressLint("CustomViewStyleable") +public class TintMySpannableTextView extends MySpannableTextView implements Tintable { + private int mBackgroundTintResId; + private int mTintResId; + private int mTintListResId; + + public TintMySpannableTextView(Context context) { + this(context, null); + } + + public TintMySpannableTextView(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public TintMySpannableTextView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + if (isInEditMode()) { + return; + } + if (attrs == null) { + mBackgroundTintResId = 0; + mTintResId = 0; + mTintListResId = 0; + applyTintColor(); + return; + } + TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.TintView, defStyleAttr, 0); + mBackgroundTintResId = array.getResourceId(R.styleable.TintView_backgroundTint, 0); + mTintResId = array.getResourceId(R.styleable.TintView_tint, 0); + mTintListResId = array.getResourceId(R.styleable.TintView_tintList, 0); + array.recycle(); + applyTintColor(); + } + + public void setBackgroundTintResId(int mBackgroundTintResId) { + this.mBackgroundTintResId = mBackgroundTintResId; + applyTintColor(); + } + + public void setTintResId(int mTintResId) { + this.mTintResId = mTintResId; + applyTintColor(); + } + + private void applyTintColor() { + if (mBackgroundTintResId != 0) { + if (getBackground() == null) { + setBackgroundColor(ThemeUtils.getColorById(getContext(), mBackgroundTintResId)); + } else { + setBackgroundTintList(ColorStateListUtils.createColorStateList(getContext(), mBackgroundTintResId)); + } + } + if (mTintResId != 0 && mTintListResId == 0) { + setTextColor(ColorStateList.valueOf(ThemeUtils.getColorById(getContext(), mTintResId))); + } else if (mTintListResId != 0) { + setTextColor(ColorStateListUtils.createColorStateList(getContext(), mTintListResId)); + } + } + + @Override + public void tint() { + applyTintColor(); + } +}