Geo*_*tic 22 android kotlin kotlin-android-extensions
假设我有这样的布局:
<?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>
Run Code Online (Sandbox Code Playgroud)
我在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()
}
}
Run Code Online (Sandbox Code Playgroud)
如果我使用合成来引用小部件:
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
dep_list.layoutManager = LinearLayoutManager(activity)
dep_list.itemAnimator = DefaultItemAnimator()
dep_list.setHasFixedSize(true)
}
Run Code Online (Sandbox Code Playgroud)
我在运行时遇到这个错误:
java.lang.NullPointerException:尝试在MyDialog上的空对象引用上调用虚拟方法'android.view.View android.view.View.findViewById(int)'._ $ _ findCachedViewById(DepartmentChoiceDialog.kt:0)
我不明白如何在DialogFragment案例中使用合成.它在Fragment和Activity中工作正常.
我发现了一种适用于自定义对话框的方法.
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
}
})
}
}
Run Code Online (Sandbox Code Playgroud)
看起来默认情况下不支持此功能,但我发现最简单的方法就是这样.在基础对话框类中:
protected abstract val containerView: View
override fun getView() = containerView
Run Code Online (Sandbox Code Playgroud)
在子类中:
override val containerView by unsafeLazy {
View.inflate(context, R.layout.dialog_team_details, null) as ViewGroup
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以像往常一样使用合成视图,并将其containerView用作对话框的视图.
以前的答案不起作用,因为在使用 onCreateDialog 时不会调用 onViewCreated。您应该首先导入 kotlinx...department_chooser_dialog。查看.dep_list,然后按如下方式使用它:
import kotlinx.android.synthetic.main.department_chooser_dialog.view.dep_list
...
class DepartmentChoiceDialog : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder(activity)
val dialog = inflater.inflate(R.layout.department_chooser_dialog, null)
dialog.dep_list.layoutManager = LinearLayoutManager(activity)
dialog.dep_list.itemAnimator = DefaultItemAnimator()
dialog.dep_list.setHasFixedSize(true)
builder.setTitle(R.string.choose_or_create_dep)
.setView(dialog)
...
Run Code Online (Sandbox Code Playgroud)
所以我不确定这个问题是否已经解决......我刚刚遇到这个问题。如果您有自定义对话框视图,请创建一个扩展 DialogFragment 的类,并使用“对话框”对象在布局中导入视图。在撰写本文时,我正在使用Android Studio 3.1.3and 。Kotlin version 1.2.41
import kotlinx.android.synthetic.main.your_custom_layout.*
class SelectCountryBottomSheet : BottomSheetDialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
dialog.setContentView(R.layout.your_custom_layout)
dialog.some_custom_close_button.setOnClickListener { dismiss() }
return dialog
}
}
Run Code Online (Sandbox Code Playgroud)
小智 0
将代码从onActivityCreated方法移至onViewCreated方法。像这样:
import kotlinx.android.synthetic.main.department_chooser_dialog.dep_list
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
dep_list.apply {
layoutManager = LinearLayoutManager(activity)
itemAnimator = DefaultItemAnimator()
setHasFixedSize(true)
}
}
Run Code Online (Sandbox Code Playgroud)
实际上我没有更深入地研究生成的代码,也许存在错误。
| 归档时间: |
|
| 查看次数: |
11895 次 |
| 最近记录: |