From 077aa61d9cdfb253d590965cac431dca155aad6c Mon Sep 17 00:00:00 2001 From: cgspine Date: Thu, 21 Dec 2017 11:42:05 +0800 Subject: [PATCH] bugfix: QMUIDialog built by MessageDialogBuilder should scroll when the message is long #114 --- .../qmui/widget/QMUIWrapContentListView.java | 7 +++ .../widget/QMUIWrapContentScrollView.java | 45 +++++++++++++++++++ .../qmui/widget/dialog/QMUIDialog.java | 42 ++++++++--------- .../qmui/widget/dialog/QMUIDialogBuilder.java | 19 +++++++- .../fragment/components/QDDialogFragment.java | 42 ++++++++++++++--- 5 files changed, 125 insertions(+), 30 deletions(-) create mode 100644 qmui/src/main/java/com/qmuiteam/qmui/widget/QMUIWrapContentScrollView.java diff --git a/qmui/src/main/java/com/qmuiteam/qmui/widget/QMUIWrapContentListView.java b/qmui/src/main/java/com/qmuiteam/qmui/widget/QMUIWrapContentListView.java index f756e287c..aa3897a44 100644 --- a/qmui/src/main/java/com/qmuiteam/qmui/widget/QMUIWrapContentListView.java +++ b/qmui/src/main/java/com/qmuiteam/qmui/widget/QMUIWrapContentListView.java @@ -28,6 +28,13 @@ public QMUIWrapContentListView(Context context, AttributeSet attrs, int defStyle super(context, attrs, defStyleAttr); } + public void setMaxHeight(int maxHeight) { + if(mMaxHeight != maxHeight){ + mMaxHeight = maxHeight; + requestLayout(); + } + } + @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(mMaxHeight, diff --git a/qmui/src/main/java/com/qmuiteam/qmui/widget/QMUIWrapContentScrollView.java b/qmui/src/main/java/com/qmuiteam/qmui/widget/QMUIWrapContentScrollView.java new file mode 100644 index 000000000..2fb08e26c --- /dev/null +++ b/qmui/src/main/java/com/qmuiteam/qmui/widget/QMUIWrapContentScrollView.java @@ -0,0 +1,45 @@ +package com.qmuiteam.qmui.widget; + +import android.content.Context; +import android.util.AttributeSet; + +/** + * height is wrapContent but limited by maxHeight + *

+ * Created by cgspine on 2017/12/21. + */ + +public class QMUIWrapContentScrollView extends QMUIObservableScrollView { + private int mMaxHeight = Integer.MAX_VALUE >> 2; + + public QMUIWrapContentScrollView(Context context) { + super(context); + } + + public QMUIWrapContentScrollView(Context context, int maxHeight) { + super(context); + mMaxHeight = maxHeight; + } + + public QMUIWrapContentScrollView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public QMUIWrapContentScrollView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + public void setMaxHeight(int maxHeight) { + if (mMaxHeight != maxHeight) { + mMaxHeight = maxHeight; + requestLayout(); + } + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + int expandSpec = MeasureSpec.makeMeasureSpec(mMaxHeight, + MeasureSpec.AT_MOST); + super.onMeasure(widthMeasureSpec, expandSpec); + } +} diff --git a/qmui/src/main/java/com/qmuiteam/qmui/widget/dialog/QMUIDialog.java b/qmui/src/main/java/com/qmuiteam/qmui/widget/dialog/QMUIDialog.java index 828900120..d13058d3a 100644 --- a/qmui/src/main/java/com/qmuiteam/qmui/widget/dialog/QMUIDialog.java +++ b/qmui/src/main/java/com/qmuiteam/qmui/widget/dialog/QMUIDialog.java @@ -31,6 +31,8 @@ import com.qmuiteam.qmui.util.QMUIDisplayHelper; import com.qmuiteam.qmui.util.QMUIResHelper; import com.qmuiteam.qmui.util.QMUIViewHelper; +import com.qmuiteam.qmui.widget.QMUIWrapContentScrollView; +import com.qmuiteam.qmui.widget.textview.QMUISpanTouchFixTextView; import java.util.ArrayList; @@ -79,15 +81,18 @@ private void initDialogWidth() { */ public static class MessageDialogBuilder extends QMUIDialogBuilder { protected CharSequence mMessage; - - private TextView mTextView; + private final QMUIWrapContentScrollView mScrollContainer; + private QMUISpanTouchFixTextView mTextView; public MessageDialogBuilder(Context context) { super(context); - mTextView = new TextView(mContext); + mTextView = new QMUISpanTouchFixTextView(mContext); mTextView.setTextColor(QMUIResHelper.getAttrColor(mContext, R.attr.qmui_config_color_gray_4)); mTextView.setLineSpacing(QMUIDisplayHelper.dpToPx(2), 1.0f); mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, QMUIResHelper.getAttrDimen(mContext, R.attr.qmui_dialog_content_message_text_size)); + + mScrollContainer = new QMUIWrapContentScrollView(mContext); + mScrollContainer.addView(mTextView); } /** @@ -108,7 +113,7 @@ public MessageDialogBuilder setMessage(int resId) { @Override protected void onCreateContent(QMUIDialog dialog, ViewGroup parent) { if (mMessage != null && mMessage.length() != 0) { - + mScrollContainer.setMaxHeight(getContentAreaMaxHeight()); mTextView.setText(mMessage); mTextView.setPadding( QMUIResHelper.getAttrDimen(mContext, R.attr.qmui_dialog_padding_horizontal), @@ -116,11 +121,11 @@ protected void onCreateContent(QMUIDialog dialog, ViewGroup parent) { QMUIResHelper.getAttrDimen(mContext, R.attr.qmui_dialog_padding_horizontal), QMUIResHelper.getAttrDimen(mContext, R.attr.qmui_dialog_content_padding_bottom) ); - parent.addView(mTextView); + parent.addView(mScrollContainer); } } - public TextView getTextView() { + public QMUISpanTouchFixTextView getTextView() { return mTextView; } } @@ -130,19 +135,21 @@ public TextView getTextView() { */ public static class CheckBoxMessageDialogBuilder extends QMUIDialogBuilder { + private final QMUIWrapContentScrollView mScrollContainer; protected String mMessage; private boolean mIsChecked = false; private Drawable mCheckMarkDrawable; - private TextView mTextView; + private QMUISpanTouchFixTextView mTextView; public CheckBoxMessageDialogBuilder(Context context) { super(context); mCheckMarkDrawable = QMUIResHelper.getAttrDrawable(context, R.attr.qmui_s_checkbox); - - mTextView = new TextView(mContext); + mScrollContainer = new QMUIWrapContentScrollView(mContext); + mTextView = new QMUISpanTouchFixTextView(mContext); mTextView.setTextColor(QMUIResHelper.getAttrColor(mContext, R.attr.qmui_config_color_gray_4)); mTextView.setLineSpacing(QMUIDisplayHelper.dpToPx(2), 1.0f); mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, QMUIResHelper.getAttrDimen(mContext, R.attr.qmui_dialog_content_message_text_size)); + mScrollContainer.addView(mTextView); } /** @@ -184,7 +191,7 @@ public CheckBoxMessageDialogBuilder setChecked(boolean checked) { @Override protected void onCreateContent(QMUIDialog dialog, ViewGroup parent) { if (mMessage != null && mMessage.length() != 0) { - + mScrollContainer.setMaxHeight(getContentAreaMaxHeight()); mTextView.setText(mMessage); mTextView.setPadding( QMUIResHelper.getAttrDimen(mContext, R.attr.qmui_dialog_padding_horizontal), @@ -202,11 +209,11 @@ public void onClick(View v) { } }); mTextView.setSelected(mIsChecked); - parent.addView(mTextView); + parent.addView(mScrollContainer); } } - public TextView getTextView() { + public QMUISpanTouchFixTextView getTextView() { return mTextView; } @@ -348,11 +355,9 @@ private static class MenuBaseDialogBuilder extends protected ArrayList mMenuItemViews; protected LinearLayout mMenuItemContainer; protected LinearLayout.LayoutParams mMenuItemLp; - private int mContentAreaMaxHeight; public MenuBaseDialogBuilder(Context context) { super(context); - mContentAreaMaxHeight = (int) (QMUIDisplayHelper.getScreenHeight(context) * 0.75); mMenuItemViews = new ArrayList<>(); mMenuItemLp = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, @@ -385,11 +390,6 @@ protected void onItemClick(int index) { } - public T setContentAreaMaxHeight(int contentAreaMaxHeight) { - mContentAreaMaxHeight = contentAreaMaxHeight; - return (T) this; - } - @Override protected void onCreateContent(QMUIDialog dialog, ViewGroup parent) { mMenuItemContainer = new LinearLayout(mContext); @@ -418,7 +418,7 @@ protected void onCreateContent(QMUIDialog dialog, ViewGroup parent) { ScrollView scrollView = new ScrollView(mContext) { @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - heightMeasureSpec = MeasureSpec.makeMeasureSpec(mContentAreaMaxHeight, + heightMeasureSpec = MeasureSpec.makeMeasureSpec(getContentAreaMaxHeight(), MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @@ -453,7 +453,7 @@ public MenuDialogBuilder addItems(CharSequence[] items, OnClickListener listener /** * 添加单个菜单项 * - * @param item 菜单项的文字 + * @param item 菜单项的文字 * @param listener 菜单项的点击事件 */ public MenuDialogBuilder addItem(CharSequence item, OnClickListener listener) { diff --git a/qmui/src/main/java/com/qmuiteam/qmui/widget/dialog/QMUIDialogBuilder.java b/qmui/src/main/java/com/qmuiteam/qmui/widget/dialog/QMUIDialogBuilder.java index 9e2e6f45f..8129f3a90 100644 --- a/qmui/src/main/java/com/qmuiteam/qmui/widget/dialog/QMUIDialogBuilder.java +++ b/qmui/src/main/java/com/qmuiteam/qmui/widget/dialog/QMUIDialogBuilder.java @@ -12,8 +12,8 @@ import android.widget.LinearLayout; import android.widget.TextView; -import com.qmuiteam.qmui.util.QMUIDisplayHelper; import com.qmuiteam.qmui.R; +import com.qmuiteam.qmui.util.QMUIDisplayHelper; import com.qmuiteam.qmui.util.QMUIResHelper; import java.util.ArrayList; @@ -50,10 +50,26 @@ public abstract class QMUIDialogBuilder { protected TextView mTitleView; protected LinearLayout mActionContainer; + private int mContentAreaMaxHeight; public QMUIDialogBuilder(Context context) { this.mContext = context; mInflater = LayoutInflater.from(context); + mContentAreaMaxHeight = (int) (QMUIDisplayHelper.getScreenHeight(mContext) * 0.75); + } + + protected int getContentAreaMaxHeight() { + return mContentAreaMaxHeight; + } + + /** + * 设置内容区域最高的高度 + * + * @param contentAreaMaxHeight + */ + public T setContentAreaMaxHeight(int contentAreaMaxHeight) { + mContentAreaMaxHeight = contentAreaMaxHeight; + return (T) this; } /** @@ -371,5 +387,4 @@ public List getPositiveAction() { } return output; } - } diff --git a/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/QDDialogFragment.java b/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/QDDialogFragment.java index d965fd373..6f15f8594 100644 --- a/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/QDDialogFragment.java +++ b/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/QDDialogFragment.java @@ -74,6 +74,7 @@ private void initListView() { String[] listItems = new String[]{ "消息类型对话框(蓝色按钮)", "消息类型对话框(红色按钮)", + "消息类型对话框 (很长文案)", "菜单类型对话框", "带 Checkbox 的消息确认框", "单选菜单类型对话框", @@ -98,24 +99,27 @@ public void onItemClick(AdapterView parent, View view, int position, long id) showMessageNegativeDialog(); break; case 2: - showMenuDialog(); + showLongMessageDialog(); break; case 3: - showConfirmMessageDialog(); + showMenuDialog(); break; case 4: - showSingleChoiceDialog(); + showConfirmMessageDialog(); break; case 5: - showMultiChoiceDialog(); + showSingleChoiceDialog(); break; case 6: - showNumerousMultiChoiceDialog(); + showMultiChoiceDialog(); break; case 7: - showEditTextDialog(); + showNumerousMultiChoiceDialog(); break; case 8: + showEditTextDialog(); + break; + case 9: showAutoDialog(); break; } @@ -164,10 +168,34 @@ public void onClick(QMUIDialog dialog, int index) { .show(); } + private void showLongMessageDialog() { + new QMUIDialog.MessageDialogBuilder(getActivity()) + .setTitle("标题") + .setMessage("这是一段很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长" + + "很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很" + + "很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很" + + "很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很" + + "长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长" + + "很长很长很长很长很很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长" + + "很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长" + + "很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长" + + "很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长" + + "很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长" + + "很长很长很长很长很长很长很长很长很长很长很长很长很长很长长很长的文案") + .addAction("取消", new QMUIDialogAction.ActionListener() { + @Override + public void onClick(QMUIDialog dialog, int index) { + dialog.dismiss(); + } + }) + .show(); + } + private void showConfirmMessageDialog() { new QMUIDialog.CheckBoxMessageDialogBuilder(getActivity()) .setTitle("退出后是否删除账号信息?") - .setMessage("删除账号信息").setChecked(true) + .setMessage("删除账号信息") + .setChecked(true) .addAction("取消", new QMUIDialogAction.ActionListener() { @Override public void onClick(QMUIDialog dialog, int index) {