gpt4 book ai didi

在触摸之前,Android 布局不会在底部表中更新/计算

转载 作者:行者123 更新时间:2023-11-30 01:17:59 25 4
gpt4 key购买 nike

我有一个位于 android-design(23.4.0) 底页中的布局。我为底页内容设置的内容 View 有一个“下注”按钮和一个“接受更改” View 。它应该看起来像这样:

Place bet button view (is ~ 80dp high)

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

Accept changes view (is ~ 160dp high)

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

enter image description here

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

我如何模仿 Android 触摸系统/键盘所做的任何事情来正确初始化此对话框并正确处理 View 布局?

代码:

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 View 代码(我对 forceLayout 进行了一些不同的尝试 - 但没有一个真正起作用)。最初,viewModel 只是通过数据绑定(bind)根据 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();
}

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

更新:此日志显示在触摸 View 之前未调用 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

最佳答案

我遇到过类似的问题。如果您想以展开状态开始,请扩展 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);

我一直在为类似的问题而苦苦挣扎。这个问题的根源是你不能在 View 布局之前将状态设置为展开。这似乎是谷歌的疏忽,默认情况下不允许展开状态。初始状态标记为私有(private),因此必须使用反射来设置没有动画的状态。我希望这有帮助!我仍在与与此相关的其他问题作斗争,但对于您的用例,我相信这应该足够了。

如果这不起作用,请看这里: https://code.google.com/p/android/issues/detail?id=205226#c18此解决方案也可能有帮助

关于在触摸之前,Android 布局不会在底部表中更新/计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37551148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com