DialogFragment中的Kotlin合成和自定义布局

DialogFragment中的Kotlin合成和自定义布局

本文介绍了DialogFragment中的Kotlin合成和自定义布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这种布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageButton
    android:id="@+id/add_dep_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_marginEnd="5dp"
    android:layout_marginRight="5dp"
    android:src="@android:drawable/ic_input_add" />

<EditText
    android:id="@+id/add_dep_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/add_dep_btn"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignTop="@id/add_dep_btn"
    android:layout_marginLeft="5dp"
    android:layout_marginStart="5dp"
    android:layout_toLeftOf="@id/add_dep_btn"
    android:layout_toStartOf="@id/add_dep_btn" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/dep_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/add_dep_btn" />

<TextView
    android:id="@+id/empty_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/add_dep_text"
    android:layout_margin="20dp"
    android:gravity="center"
    android:text="@string/no_dep"
    android:textSize="22sp" />
</RelativeLayout>

我在DialogFragment中使用它:

And I use it in a DialogFragment:

class DepartmentChoiceDialog : DialogFragment() {
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val builder = AlertDialog.Builder(activity)
        builder.setTitle(R.string.choose_or_create_dep)
            .setView(R.layout.department_chooser_dialog)
            .setNegativeButton(android.R.string.cancel, { d, i ->
                d.cancel()
            })
        return builder.create()
    }
}

如果我使用合成材料来引用小部件:

if I refer to the widget using synthetic:

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    dep_list.layoutManager = LinearLayoutManager(activity)
    dep_list.itemAnimator = DefaultItemAnimator()
    dep_list.setHasFixedSize(true)
}

我在运行时遇到此错误:

I got this error at runtime:

我不明白如何在DialogFragment情况下使用人工合成.可以在片段和活动"中正常工作.

I don't understand how to use synthetic in DialogFragment case. It works fine in Fragment and Activity.

推荐答案

我找到了一种适用于自定义对话框的方法.

I found a way that works for custom dialogs.

class ServerPickerDialogFragment: AppCompatDialogFragment()
{
  // Save your custom view at the class level
  lateinit var customView: View;
  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                            savedInstanceState: Bundle?): View?
  {
       // Simply return the already inflated custom view
       return customView
  }

  override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
      // Inflate your view here
      customView = context!!.layoutInflater.inflate(R.layout.dialog_server_picker, null)
      // Create Alert Dialog with your custom view
      return AlertDialog.Builder(context!!)
             .setTitle(R.string.server_picker_dialog_title)
             .setView(customView)
             .setNegativeButton(android.R.string.cancel, null)
             .create()
  }

  override fun onViewCreated(view: View, savedInstanceState: Bundle?)
  {
    super.onViewCreated(view, savedInstanceState)
    // Perform remaining operations here. No null issues.
    rbgSelectType.setOnCheckedChangeListener({ _, checkedId ->
      if(checkedId == R.id.rbSelectFromList) {
             // XYZ
      } else {
             // ABC
      }
    })
  }
}

这篇关于DialogFragment中的Kotlin合成和自定义布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:40