如何在对话框中使用数据绑定?

How to use data-binding in Dialog?

我在对话框中实现数据绑定时遇到问题。可能吗?

下面是我的xml.

<data>

    <variable
        name="olaBooking"
        type="com.example.myapp.viewmodels.ViewModel" />
</data>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:id="@+id/cv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:elevation="4dp"
        android:padding="15dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimary"
                android:gravity="center"
                android:padding="15dp"
                android:text="OLA Cab Booked !"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@color/colorPrimaryDark" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="start|center"
                android:padding="15dp"
                android:text="Car Details" />

            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@color/colorPrimaryDark" />

            <TextView
                android:id="@+id/driverName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:text="@{olaBooking.driverName}" />

            <TextView
                android:id="@+id/carModel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:text="@{olaBooking.getCarName}" />

            <TextView
                android:id="@+id/carNo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:text="@{olaBooking.getCabNo}" />

            <TextView
                android:id="@+id/eta"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:text="@{olaBooking.getEta}" />
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

我想在Dialog中绑定上面的布局。这怎么可能?下面是我的 java 代码,我试过但它不起作用

        dialog.setContentView(R.layout.dialog_ola_booking_confirmed);
    DialogOlaBookingConfirmedBinding binding = DataBindingUtil.inflate(
            LayoutInflater.from(dialog.getContext()),
            R.layout.dialog_ola_booking_confirmed,
            (ViewGroup) dialog.findViewById(R.id.cv),
            false);
    ViewModel viewModel = new ViewModel(this, event.olaBooking);
  mBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.dialog_select, null, false);
    setContentView(mBinding.getRoot());
    SelectDialogBean data = new SelectDialogBean();
    mBinding.setData(data);

可以在 Dialog 中使用数据绑定,首先要使 Dialog 上的绑定正常工作,您应该先对其进行扩充,然后像这样将其传递给 setContentView。

DialogOlaBookingConfirmedBinding binding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout. dialog_ola_booking_confirmed, null, false);
setContentView(binding.getRoot());

然后你可以传递viewModel:

binding.setViewModel(new ViewModel(this, event.olaBooking));

现在您可以看到它正在运行。

这里是带有数据绑定的 AlertDialog 的完整示例:

import android.app.Dialog;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;


public class MyDialog extends DialogFragment {

    private static final String KEY_MY_INFO = "KEY_MY_INFO";

    private String myInfo;

    public static MyDialog newInstance(String myInfo) {
        MyDialog dialog = new MyDialog();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_MY_INFO, myInfo);
        dialog.setArguments(bundle);
        return dialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myInfo = getArguments().getString(KEY_MY_INFO);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        FragmentActivity activity = getActivity();
        
        MyInfoBinding binding = DataBindingUtil.inflate(LayoutInflater.from(getContext()),
                R.layout.my_info_dialog_layout, null, false);

        binding.setMyInfo(myInfo);

        return new AlertDialog.Builder(activity, R.style.AppCompatAlertDialogStyle)
                .setView(binding.getRoot())
                .create();
    }

}

你可以不调用 getRoot() 来做同样的事情。

View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_delete_confirmation, null, false);

mBinding = DialogDeleteConfirmationBinding.bind(view);

mBinding.setViewModel(viewModel);

builder.setView(view);

builder.create();

Android Documentation

中所述,您不应将DataBindingUtil用于生成的classes

您应该使用生成的绑定class的 inflate & bind 方法 (MyDialogBinding.inflate).

public void showDialog(final Context context) {
    Dialog dialog = new Dialog(context);
    MyDialogBinding binding = MyDialogBinding.inflate(LayoutInflater.from(context));
    dialog.setContentView(binding.getRoot());
    dialog.show();
}

能再简单点吗?没有!

Binding Document 表示 DataBindingUtil class 的 inflate method

Use this version only if layoutId is unknown in advance. Otherwise, use the generated Binding's inflate method to ensure type-safe inflation. DataBindingUtil.inflate(LayoutInflater.from(getContext()),R.layout.my_info_dialog_layout, null, false);

这就像发现绑定已生成 class,而我们已经有 class。

改用这个

MyDialogBinding binding = MyDialogBinding.inflate(LayoutInflater.from(context));

或者如果你想制作另一个 class。

public class MyDialog extends Dialog {
    public MyDialog(@NonNull Context context) {
        super(context);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyDialogBinding binding = MyDialogBinding.inflate(LayoutInflater.from(getContext()));
        setContentView(binding.getRoot());
    }
}

如果您不想延长 Dialog,另一个可能的解决方案是:

Dialog dialog = new Dialog(this); // where "this" is the context

YourClassNameBinding binding = DataBindingUtil.inflate(dialog.getLayoutInflater(), R.layout.your_layout, null, false);
binding.setYourData(yourData);

dialog.setContentView(binding.getRoot());
dialog.show();

希望对您有所帮助。

如果对话框缩小,试试这个

我尝试了@Dullahan 的回答,但是对话框似乎奇怪地缩小了。 所以我尝试了另一种方法,终于找到了解决方案。

<layout
    xmlns:android="http://schemas.android.com/apk/res/android">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/root"
        android:layout_width="300dp"
        android:layout_height="500dp">

        <!-- ... -->

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
class CustomDialog(context: Context) : Dialog(context) {
    private lateinit var binding: CustomDialogBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.custom_dialog)
        binding = CustomDialogBinding.bind(findViewById(R.id.root))
    }
}

**

您应该在 DialogFragment 的上下文中使用 activity。

**

在 DialogFragment 中使用数据绑定时的问题是主题不符合深色模式颜色。如果您对此有疑问,请在 LayoutInflater.from(context)

中使用 LayoutInflater.from(activity)