Skip to content

Commit

Permalink
bugfix: make content area of menu dialog scrollable when there are mu…
Browse files Browse the repository at this point in the history
…ch items #54 #67
  • Loading branch information
cgspine committed Nov 15, 2017
1 parent dd714ab commit a17e807
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
20 changes: 18 additions & 2 deletions qmui/src/main/java/com/qmuiteam/qmui/widget/dialog/QMUIDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import android.widget.ScrollView;
import android.widget.TextView;

import com.qmuiteam.qmui.R;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.util.QMUIResHelper;
import com.qmuiteam.qmui.R;
import com.qmuiteam.qmui.util.QMUIViewHelper;

import java.util.ArrayList;
Expand Down Expand Up @@ -349,9 +349,11 @@ 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 @@ -384,6 +386,11 @@ 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 All @@ -409,7 +416,16 @@ protected void onCreateContent(QMUIDialog dialog, ViewGroup parent) {
for (QMUIDialogMenuItemView itemView : mMenuItemViews) {
mMenuItemContainer.addView(itemView, mMenuItemLp);
}
parent.addView(mMenuItemContainer);
ScrollView scrollView = new ScrollView(mContext) {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(mContentAreaMaxHeight,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
};
scrollView.addView(mMenuItemContainer);
parent.addView(scrollView);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmuidemo.QDDataManager;
import com.qmuiteam.qmuidemo.model.QDItemDescription;
import com.qmuiteam.qmuidemo.R;
import com.qmuiteam.qmuidemo.base.BaseFragment;
import com.qmuiteam.qmuidemo.lib.annotation.Widget;
import com.qmuiteam.qmuidemo.model.QDItemDescription;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -78,6 +78,7 @@ private void initListView() {
"带 Checkbox 的消息确认框",
"单选菜单类型对话框",
"多选菜单类型对话框",
"多选菜单类型对话框(item 数量很多)",
"带输入框的对话框",
"高度适应键盘升降的对话框"
};
Expand Down Expand Up @@ -109,9 +110,12 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
showMultiChoiceDialog();
break;
case 6:
showEditTextDialog();
showNumerousMultiChoiceDialog();
break;
case 7:
showEditTextDialog();
break;
case 8:
showAutoDialog();
break;
}
Expand Down Expand Up @@ -237,6 +241,40 @@ public void onClick(QMUIDialog dialog, int index) {
builder.show();
}

private void showNumerousMultiChoiceDialog() {
final String[] items = new String[]{
"选项1", "选项2", "选项3", "选项4", "选项5", "选项6",
"选项7", "选项8", "选项9", "选项10", "选项11", "选项12",
"选项13", "选项14", "选项15", "选项16", "选项17", "选项18"
};
final QMUIDialog.MultiCheckableDialogBuilder builder = new QMUIDialog.MultiCheckableDialogBuilder(getActivity())
.setCheckedItems(new int[]{1, 3})
.addItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
});
builder.addAction("取消", new QMUIDialogAction.ActionListener() {
@Override
public void onClick(QMUIDialog dialog, int index) {
dialog.dismiss();
}
});
builder.addAction("提交", new QMUIDialogAction.ActionListener() {
@Override
public void onClick(QMUIDialog dialog, int index) {
String result = "你选择了 ";
for (int i = 0; i < builder.getCheckedItemIndexes().length; i++) {
result += "" + builder.getCheckedItemIndexes()[i] + "; ";
}
Toast.makeText(getActivity(), result, Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
builder.show();
}

private void showEditTextDialog() {
final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(getActivity());
builder.setTitle("标题")
Expand All @@ -256,7 +294,7 @@ public void onClick(QMUIDialog dialog, int index) {
Toast.makeText(getActivity(), "您的昵称: " + text, Toast.LENGTH_SHORT).show();
dialog.dismiss();
} else {
Toast.makeText(getActivity(), "请填入昵称" , Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(), "请填入昵称", Toast.LENGTH_SHORT).show();
}
}
})
Expand Down

0 comments on commit a17e807

Please sign in to comment.