android 如何在Dialog中使用数据绑定?

bf1o4zei  于 2023-01-15  发布在  Android
关注(0)|答案(9)|浏览(331)

在对话框中实现数据绑定时遇到问题。是否可能?
下面是我的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>

我想把上面的布局绑定到一个对话框中。这怎么可能呢?下面是我的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);
ecbunoof

ecbunoof1#

可以在对话框中使用数据绑定,首先要使绑定在对话框上工作,您应该首先将其膨胀并将其传递给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));

现在你可以看到它在工作。

rggaifut

rggaifut2#

不应将DataBindingUtil用于**Android Documentation**中所述的生成类

您应该使用生成的绑定类的****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类的inflate method

仅当layoutId事先未知时使用此版本,否则使用生成的绑定的inflate方法进行类型安全的膨胀DataBindingUtil.inflate(LayoutInflater.from(getContext()),R.layout.my_info_dialog_layout,null,false);

这类似于查找绑定生成的类,但我们已经有了类。

改为使用此

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

或者你想再上一堂课。

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

gpnt7bae3#

以下是具有数据绑定的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();
    }

}
watbbzwu

watbbzwu4#

您可以在不调用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();
tcbh2hod

tcbh2hod5#

mBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.dialog_select, null, false);
    setContentView(mBinding.getRoot());
    SelectDialogBean data = new SelectDialogBean();
    mBinding.setData(data);
4szc88ey

4szc88ey6#

如果您不想扩展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();

希望有帮助。

gcuhipw9

gcuhipw97#

您应该在DialogFragment的上下文中使用Activity。
在DialogFragment中使用数据绑定时出现的问题是主题与暗模式颜色无关。如果遇到此问题,请在LayoutInflater.from(context)中使用LayoutInflater.from(activity)

plicqrtu

plicqrtu8#

如果对话框缩小,请尝试以下操作

我试着@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))
    }
}
xdyibdwo

xdyibdwo9#

val bottomViewBinding :AddProductNoPositionBottomPupUpBinding = AddProductNoPositionBottomPupUpBinding.inflate(layoutInflater) bottomSheetDialog = BottomSheetDialog(this , R.style.BottomSheetDialoTheme) bottomSheetDialog.setContentView(bottomViewBinding.root) bottomSheetDialog.show() bottomViewBinding.productDataBinding = product

相关问题