在触摸

时间:2016-05-31 16:33:24

标签: android android-layout

我有一个在android-design(23.4.0)底层表中的布局。我为底部内容设置的内容视图有一个“下注”按钮和一个“接受更改”视图。它应该是这样的:

Place bet button view (is ~ 80dp high)

当价格发生变化时,用户必须接受更改 - 单击“接受”按钮会再次显示placebet按钮。

Accept changes view (is ~ 160dp high)

问题是BottomSheet对话框由于某种原因没有正确初始化 - 开始时接受更改视图(处于状态visibility==gone)没有显示,我得到下面的结果。如果我点击其中一个edittexts来显示键盘,那么当设置placeBet和AcceptChangesView的可见性时,它似乎正确初始化并且布局正确更新。但是,如果键盘尚未显示 - 则不会发生布局更改,并且视图的布局数据不会更改。自动显示和隐藏键盘似乎无法工作,我必须手动触摸编辑文本。我甚至在所有不会更新的视图上调用forceLayout - 但即便这样也不会调用布局(参见下面的日志)

enter image description here

  • 在层次结构查看器AcceptChangesView中,布局属性(mTop,mLeft,mBottom,mRight)都显示为0.测量的属性(getMeasuredWidth(),getMeasuredHeight())是正确的。
  • 即使调用ViewTreeObserver().addOnGlobalLayoutListener并且StraightBetDialogView.getMeasuredHeight()高度正确,对话框高度也不会改变。

如何模仿Android触控系统/键盘的作用,使此对话框正确初始化并正确处理视图布局?

CODE:

package com.gtech.liquidsportsbook.ui.dialog;


import android.content.Context;
import android.content.DialogInterface;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.util.Log;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;

import StraightBetDialogView; 

/**
 * Created by robert on 25/04/16.
 */
public class StraightBetDialog implements ViewTreeObserver.OnGlobalLayoutListener, DialogInterface.OnShowListener, StraightBetView.LayoutListener {

private final BottomSheetDialog dialog;
private final StraightBetView dialogView;

private final DialogInterface.OnDismissListener onDismissListener = new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(final DialogInterface dialog) {
        dialogView.getViewTreeObserver().removeOnGlobalLayoutListener(StraightBetDialog.this);
        ViewServer.get(getDialogView().getContext()).removeWindow(dialogView);
        if (dialogView.getListener() != null) {
            dialogView.getListener().onCancel();
        }
    }
};

public StraightBetDialog(final Context c) {
    dialogView = new StraightBetView(c);
    dialog = new BottomSheetDialog(c);
    dialog.setContentView(dialogView);
    dialog.setOnDismissListener(onDismissListener);
    dialog.setCancelable(true);
    dialog.setOnShowListener(this);
    dialogView.getViewTreeObserver().addOnGlobalLayoutListener(this);
    dialogView.setLayoutListener(this);
    ViewServer.get(c).addWindow(dialogView, StraightBetView.class.getSimpleName());
}

public StraightBetView getDialogView() {
    return dialogView;
}

public BottomSheetDialog getDialog() {
    return dialog;
}

@Override
public void onGlobalLayout() {
    adjustHeight();
}

public void adjustHeight() {
    final FrameLayout bottomSheetFrameLayout = (FrameLayout) dialog.getWindow().findViewById(R.id.design_bottom_sheet);
//        bottomSheetFrameLayout.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
//        bottomSheetFrameLayout.forceLayout();
        dialogView.invalidate();
        final BottomSheetBehavior<FrameLayout> dialogBehaviour = BottomSheetBehavior.from(bottomSheetFrameLayout);
        dialogBehaviour.setPeekHeight(dialogView.getMeasuredHeight());
        dialogBehaviour.setState(BottomSheetBehavior.STATE_EXPANDED);
        Log.d(getClass().getSimpleName(), "adjustHeight(): " + dialogView.getMeasuredHeight(), new Exception());
}

@Override
public void onShow(final DialogInterface dialog) {
    if (dialogView.getParameters() != null) {
        dialogView.getParameters().setHasChanges(false);
        adjustHeight();
    }
}

@Override
public void onLayout() {
    adjustHeight();
}

}

相关布局xml如下:底部容器包含acceptChangesView和placeBetB​​utton。

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/botttomContainer"
    >

        <com.gtech.liquidsportsbook.ui.views.AcceptChangesView
            android:id="@+id/straightBetAcceptChanges"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            app:listener="@{listener}"
        />

        <Button
            android:id="@+id/placeBetButton"
            style="@style/style_mgm_button_betting"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:enabled="@{viewModel.placeBetEnabled}"
            android:text="@string/placeBet"
            android:onClick="@{listener.onPlaceBetClick}"
        />

    </FrameLayout>

cumstom视图代码在这里(我尝试过forceLayout有几种不同的尝试 - 其中没有一种实际工作)。最初,viewModel只是通过数据绑定根据hasChanges属性设置可见性。

public class StraightBetView extends FrameLayout {

private StraightBetViewModel viewModel;

private ViewStraightBetBinding binding;

private AmountListener amountListener;

private Listener listener;
private LayoutListener layoutListener;

@Inject
protected BettingUtils bettingUtils;

public interface AmountListener {
    void onRiskChanged(final BigDecimal bigDecimal);

    void onToWinChanged(final BigDecimal bigDecimal);

    void onUnfocus(AmountInputEditText v);
}


public interface Listener extends AcceptChangesView.Listener {
    void onPlaceBetClick(View v);

    void onCancel();
}

public interface LayoutListener {
    void onLayout();
}

private final AmountInputEditText.OnUnfocusListener editTextUnfocusListener = new AmountInputEditText.OnUnfocusListener() {
    @Override
    public void onUnfocus(final AmountInputEditText view) {
        if (!binding.riskEditText.hasFocus() && !binding.toWinEditText.hasFocus()) {
            UIUtils.closeKeyBoard(view);
        } else {
            UIUtils.showKeyBoard(view);
        }
        if (amountListener != null) {
            amountListener.onUnfocus(view);
        }
    }
};

private final android.databinding.Observable.OnPropertyChangedCallback propertyChangedCallback = new android.databinding.Observable.OnPropertyChangedCallback() {
    @Override
    public void onPropertyChanged(final android.databinding.Observable observable, final int i) {
        if (i == BR.hasChanges) {
            setAcceptChangesState();
        }
    }
};

public StraightBetView(final Context context) {
    super(context);
    init(context);
}

public StraightBetView(final Context context, final AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public StraightBetView(final Context context, final AttributeSet attrs, final int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public StraightBetView(final Context context, final AttributeSet attrs, final int defStyleAttr, final int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init(context);
}

private void init(final Context context) {
    MGMApplicationComponent.Injector.getComponent(context).inject(this);
    final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    binding = ViewStraightBetBinding.inflate(layoutInflater, this, true);
    setId(R.id.straightBetContainer);
    //setOrientation(VERTICAL);
    binding.toWinEditText.setOnUnfocusListener(editTextUnfocusListener);
    binding.riskEditText.setOnUnfocusListener(editTextUnfocusListener);
    setAcceptChangesState();

}

private void updateView() {
    binding.setViewModel(viewModel);
}

public StraightBetViewModel getParameters() {
    return viewModel;
}

public void setParameters(final StraightBetViewModel parameters) {
    if (viewModel != null) {
        viewModel.removeOnPropertyChangedCallback(propertyChangedCallback);
    }
    this.viewModel = parameters;
    viewModel.addOnPropertyChangedCallback(propertyChangedCallback);
    //setAcceptChangesState();
    updateView();
}

public AmountListener getAmountListener() {
    return amountListener;
}

public void setAmountListener(final AmountListener amountListener) {
    this.amountListener = amountListener;
    binding.setAmountListener(amountListener);
}

@BindingAdapter("bind:amountListener")
public static void setAmountListener(final StraightBetView view, final AmountListener amountListener) {
    view.setAmountListener(amountListener);
}

public Listener getListener() {
    return listener;
}

public void setListener(final Listener listener) {
    this.listener = listener;
    binding.setListener(listener);
}

@BindingAdapter("bind:listener")
public static void setListener(final StraightBetView view, final Listener listener) {
    view.setListener(listener);
}

public void localeChanged() {
    LocaleUiUtil.initEditTextLocale(binding.riskEditText);
    LocaleUiUtil.initEditTextLocale(binding.toWinEditText);
}

public void setAcceptChangesState() {
    if (viewModel != null) {
        binding.placeBetButton.setVisibility(!viewModel.isHasChanges() ? VISIBLE : GONE);
        binding.straightBetAcceptChanges.setVisibility(viewModel.isHasChanges() ? VISIBLE : GONE);
    }
    binding.straightBetAcceptChanges.measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    binding.straightBetAcceptChanges.forceLayout();
    binding.straightBetAcceptChanges.layout(0, binding.botttomContainer.getTop(), getMeasuredWidth(), binding.botttomContainer.getTop() + binding.straightBetAcceptChanges.getMeasuredHeight());
    binding.botttomContainer.measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    binding.botttomContainer.forceLayout();
    measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    forceLayout();
    if (layoutListener != null) {
        layoutListener.onLayout();
    }
}

public void setLayoutListener(final LayoutListener layoutListener) {
    this.layoutListener = layoutListener;
}
}

显示对话框的代码非常简单:

public void showStraightBetDialog(final StraightBetDialogParameters straightBetParams, final Event event) {


    straightBetDialog = new StraightBetDialog(getActivity());
    getPresenter().createStraightBetViewModel(getActivity(), straightBetParams, event);
    straightBetDialog.getDialogView().setParameters(getPresenter().getStraightBetViewModel());
    straightBetDialog.getDialogView().setListener(this);
    straightBetDialog.getDialogView().setAmountListener(getPresenter());

    straightBetDialog.getDialog().show();
}

任何帮助表示感谢 - 如果我可以添加更多信息,请在评论中告诉我。

更新:此日志显示在触摸视图之前未调用onLayout。

06-01 12:08:28.221 28768-28768/com.app.dev D/StraightBetView: onMeasure : mw:0 mh:1593
06-01 12:08:28.271 28768-28768/com.app.dev D/StraightBetView: onAttachedToWindow:
06-01 12:08:28.271 28768-28768/com.app.dev D/StraightBetView.AcceptChangesView: onAttachedToWindow:
06-01 12:08:28.281 28768-28768/com.app.dev D/StraightBetView: onMeasure : mw:1440 mh:1008
06-01 12:08:28.331 28768-28768/com.app.dev D/StraightBetView: onMeasure : mw:1440 mh:1008
06-01 12:08:28.331 28768-28768/com.app.dev D/StraightBetView: onLayout : true left:0 top:0 right:1440 bottom:1008
06-01 12:08:28.331 28768-28768/com.app.dev D/StraightBetDialog: adjustHeight(): 1008
06-01 12:08:28.361 28768-28768/com.app.dev D/StraightBetDialog: adjustHeight(): 1008
06-01 12:08:28.361 28768-28768/com.app.dev D/StraightBetView: onMeasure : mw:1440 mh:1008
06-01 12:08:28.361 28768-28768/com.app.dev D/StraightBetDialog: adjustHeight(): 1008
06-01 12:08:28.391 28768-28768/com.app.dev D/StraightBetView: onMeasure : mw:1440 mh:1008
06-01 12:08:28.391 28768-28768/com.app.dev D/StraightBetDialog: adjustHeight(): 1008
<!—- price changed should show accept changes but onLayout is NOT called (onMeasuere is) -—>
06-01 12:08:55.201 28768-28768/com.app.dev D/StraightBetView.AcceptChangesView: onMeasure : mw:1440 mh:636
06-01 12:08:55.201 28768-28768/com.app.dev D/StraightBetView.AcceptChangesView: onMeasure : mw:1440 mh:636
06-01 12:08:55.221 28768-28768/com.app.dev D/StraightBetView.AcceptChangesView: onMeasure : mw:1440 mh:636
06-01 12:08:55.231 28768-28768/com.app.dev D/StraightBetView.AcceptChangesView: onMeasure : mw:1440 mh:636
06-01 12:08:55.231 28768-28768/com.app.dev D/StraightBetView: onMeasure : mw:1440 mh:1372
06-01 12:08:55.231 28768-28768/com.app.dev D/StraightBetDialog: adjustHeight(): 1372
<!—- ANOTHER CHNAGE (again onLayout is not called)-—>
06-01 12:10:57.141 28768-28768/com.app.dev D/StraightBetView.AcceptChangesView: onMeasure : mw:1440 mh:636
06-01 12:10:57.151 28768-28768/com.app.dev D/StraightBetView.AcceptChangesView: onMeasure : mw:1440 mh:636
06-01 12:10:57.161 28768-28768/com.app.dev D/StraightBetView.AcceptChangesView: onMeasure : mw:1440 mh:636
06-01 12:10:57.171 28768-28768/com.app.dev D/StraightBetView.AcceptChangesView: onMeasure : mw:1440 mh:636
06-01 12:10:57.171 28768-28768/com.app.dev D/StraightBetView: onMeasure : mw:1440 mh:1372
06-01 12:10:57.171 28768-28768/com.app.dev D/StraightBetDialog: adjustHeight(): 1372
<!—- CLOSED -—>
06-01 12:11:52.091 28768-28768/com.app.dev D/StraightBetView.AcceptChangesView: onDetachedFromWindow:
06-01 12:11:52.091 28768-28768/com.app.dev D/StraightBetView: onDetachedFromWindow:
<!—- SHOW AGAIN -—>
06-01 12:12:18.091 28768-28768/com.app.dev D/StraightBetView: onMeasure : mw:0 mh:1593
06-01 12:12:18.161 28768-28768/com.app.dev D/StraightBetView: onAttachedToWindow:
06-01 12:12:18.161 28768-28768/com.app.dev D/StraightBetView.AcceptChangesView: onAttachedToWindow:
06-01 12:12:18.171 28768-28768/com.app.dev D/StraightBetView: onMeasure : mw:1440 mh:1008
06-01 12:12:18.241 28768-28768/com.app.dev D/StraightBetView: onMeasure : mw:1440 mh:1008
06-01 12:12:18.241 28768-28768/com.app.dev D/StraightBetView: onLayout : true left:0 top:0 right:1440 bottom:1008
06-01 12:12:18.241 28768-28768/com.app.dev D/StraightBetDialog: adjustHeight(): 1008
06-01 12:12:18.261 28768-28768/com.app.dev D/StraightBetDialog: adjustHeight(): 1008
06-01 12:12:18.261 28768-28768/com.app.dev D/StraightBetView: onMeasure : mw:1440 mh:1008
06-01 12:12:18.261 28768-28768/com.app.dev D/StraightBetDialog: adjustHeight(): 1008
06-01 12:12:18.291 28768-28768/com.app.dev D/StraightBetView: onMeasure : mw:1440 mh:1008
06-01 12:12:18.291 28768-28768/com.app.dev D/StraightBetDialog: adjustHeight(): 1008
<!—- TOUCH -—>
06-01 12:12:46.391 28768-28768/com.app.dev D/StraightBetView: onMeasure : mw:1440 mh:1008
06-01 12:12:46.401 28768-28768/com.app.dev D/StraightBetDialog: adjustHeight(): 1008
<!—- CLOSE KEYBOARD -—>
06-01 12:13:49.711 28768-28768/com.app.dev D/StraightBetView: onMeasure : mw:1440 mh:1008
06-01 12:13:49.721 28768-28768/com.app.dev D/StraightBetView: onLayout : false left:0 top:0 right:1440 bottom:1008
06-01 12:13:49.721 28768-28768/com.app.dev D/StraightBetDialog: adjustHeight(): 1008
<!—- price changed should show accept changes (since the view was touched onLayout IS called and it displays correctly) -—>
06-01 12:14:48.461 28768-28768/com.app.dev D/StraightBetView.AcceptChangesView: onMeasure : mw:1440 mh:636
06-01 12:14:48.461 28768-28768/com.app.dev D/StraightBetView.AcceptChangesView: onMeasure : mw:1440 mh:636
06-01 12:14:48.471 28768-28768/com.app.dev D/StraightBetView.AcceptChangesView: onMeasure : mw:1440 mh:636
06-01 12:14:48.481 28768-28768/com.app.dev D/StraightBetView.AcceptChangesView: onMeasure : mw:1440 mh:636
06-01 12:14:48.481 28768-28768/com.app.dev D/StraightBetView: onMeasure : mw:1440 mh:1372
06-01 12:14:48.481 28768-28768/com.app.dev D/StraightBetDialog: adjustHeight(): 1372
06-01 12:14:48.481 28768-28768/com.app.dev D/StraightBetView.AcceptChangesView: onMeasure : mw:1440 mh:636
06-01 12:14:48.481 28768-28768/com.app.dev D/StraightBetView: onMeasure : mw:1440 mh:1372
06-01 12:14:48.491 28768-28768/com.app.dev D/StraightBetView.AcceptChangesView: onLayout : true left:0 top:0 right:1440 bottom:636
06-01 12:14:48.491 28768-28768/com.app.dev D/StraightBetView: onLayout : true left:0 top:0 right:1440 bottom:1372
06-01 12:14:48.491 28768-28768/com.app.dev D/StraightBetDialog: adjustHeight(): 1372

2 个答案:

答案 0 :(得分:1)

我遇到过类似的问题。如果您想要在展开状态下启动,请扩展BottomSheetBehavior并添加此方法。

public void setInitialState(int state) {
try {
  Field field2 = BottomSheetBehavior.class.getDeclaredField("mState");
  field2.setAccessible(true);
  field2.set(this, state);
} catch (Exception e) {
  Log.e("REFLECTION", e.getMessage());
}

}

之后立即调用此方法
final BottomSheetBehavior<FrameLayout> dialogBehaviour = BottomSheetBehavior.from(bottomSheetFrameLayout);

我一直在努力解决类似的问题。此问题的根源是您无法在视图布局之前将状态设置为展开。这似乎是谷歌的疏忽,默认情况下不允许扩展状态。初始状态标记为私有,因此必须使用反射以便在没有动画的情况下设置状态。我希望这有帮助!我仍在与其他与此相关的问题进行斗争,但对于您的使用案例,我认为这应该足够了。

如果这不起作用,请看这里: http://docs.aws.amazon.com/mobileanalytics/latest/ug/PutEvents.html 这个解决方案也可以帮助

答案 1 :(得分:0)

我最后添加了一些代码来设置布局上的对话框高度dialog是我的BottomSheetDialogdialogView是内容视图。

dialog = new BottomSheetDialog(c);
dialog.setContentView(dialogView);
dialog.setOnShowListener(this);
dialogView.getViewTreeObserver().addOnGlobalLayoutListener(this);
dialogView.setLayoutListener(this);

这里定义了听众

@Override
public void onGlobalLayout() {
    adjustHeight();
}

public void adjustHeight() {
    final FrameLayout bottomSheetFrameLayout = (FrameLayout) dialog.getWindow().findViewById(R.id.design_bottom_sheet);
    if (dialogView.getMeasuredHeight() > 0) {
        bottomSheetFrameLayout.measure(View.MeasureSpec.makeMeasureSpec(dialogView.getMeasuredWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(dialogView.getMeasuredHeight(), View.MeasureSpec.EXACTLY));
        final int top = ((View) bottomSheetFrameLayout.getParent()).getMeasuredHeight() - dialogView.getMeasuredHeight();
        bottomSheetFrameLayout.layout(0, top, dialogView.getMeasuredWidth(), top + dialogView.getMeasuredHeight());
        Log.d(getClass().getSimpleName(), "bottomSheetFrameLayout.layout(" + 0 + ", " + top + ", " + dialogView.getMeasuredWidth() + ", " + (top + dialogView.getMeasuredHeight()) + ")");
    }
    dialogView.invalidate();
    final BottomSheetBehavior<FrameLayout> dialogBehaviour = BottomSheetBehavior.from(bottomSheetFrameLayout);
    dialogBehaviour.setPeekHeight(dialogView.getMeasuredHeight());
    dialogBehaviour.setState(BottomSheetBehavior.STATE_EXPANDED);
    Log.d(getClass().getSimpleName(), "adjustHeight(): " + dialogView.getMeasuredHeight());
}

@Override
public void onShow(final DialogInterface dialog) {
    if (dialogView.getParameters() != null) {
        adjustHeight();
    }
}

@Override
public void onLayout() {
    adjustHeight();
}