Skip to content

Commit

Permalink
bugfix: QMUIDialog built by MessageDialogBuilder should scroll when t…
Browse files Browse the repository at this point in the history
…he message is long #114
  • Loading branch information
cgspine committed Dec 21, 2017
1 parent f729e39 commit 077aa61
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.qmuiteam.qmui.widget;

import android.content.Context;
import android.util.AttributeSet;

/**
* height is wrapContent but limited by maxHeight
* <p>
* 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);
}
}
42 changes: 21 additions & 21 deletions qmui/src/main/java/com/qmuiteam/qmui/widget/dialog/QMUIDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -79,15 +81,18 @@ private void initDialogWidth() {
*/
public static class MessageDialogBuilder extends QMUIDialogBuilder<MessageDialogBuilder> {
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);
}

/**
Expand All @@ -108,19 +113,19 @@ 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),
QMUIResHelper.getAttrDimen(mContext, hasTitle() ? R.attr.qmui_dialog_content_padding_top : R.attr.qmui_dialog_content_padding_top_when_no_title),
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;
}
}
Expand All @@ -130,19 +135,21 @@ public TextView getTextView() {
*/
public static class CheckBoxMessageDialogBuilder extends QMUIDialogBuilder<CheckBoxMessageDialogBuilder> {

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);
}

/**
Expand Down Expand Up @@ -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),
Expand All @@ -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;
}

Expand Down Expand Up @@ -348,11 +355,9 @@ private static class MenuBaseDialogBuilder<T extends QMUIDialogBuilder> extends
protected ArrayList<QMUIDialogMenuItemView> 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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -453,7 +453,7 @@ public MenuDialogBuilder addItems(CharSequence[] items, OnClickListener listener
/**
* 添加单个菜单项
*
* @param item 菜单项的文字
* @param item 菜单项的文字
* @param listener 菜单项的点击事件
*/
public MenuDialogBuilder addItem(CharSequence item, OnClickListener listener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,10 +50,26 @@ public abstract class QMUIDialogBuilder<T extends 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;
}

/**
Expand Down Expand Up @@ -371,5 +387,4 @@ public List<QMUIDialogAction> getPositiveAction() {
}
return output;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ private void initListView() {
String[] listItems = new String[]{
"消息类型对话框(蓝色按钮)",
"消息类型对话框(红色按钮)",
"消息类型对话框 (很长文案)",
"菜单类型对话框",
"带 Checkbox 的消息确认框",
"单选菜单类型对话框",
Expand All @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 077aa61

Please sign in to comment.