自定义首选项 Android Kotlin

jun*_*dev 6 android android-preferences preferencescreen kotlin android-recyclerview

我想子类化Preference以在 Kotlin 中创建自定义首选项。我无法让自定义首选项在首选项屏幕中膨胀。如果我从我的首选项屏幕中删除这个自定义首选项,我实现的其余首选项(此处未显示)工作正常。 还有很多类似的表面上的问题在这里,但没有那些我已经找到了直接处理创建科特林实现自定义偏好的问题。

请帮助我提供一个您已经测试过的工作示例,该示例显示了三件事:

  1. custom_preference.xml
  2. CustomPreference.kt
  3. preference_screen.xml (显示自定义首选项的父首选项屏幕)

这是我的代码:一个xml显示字符串的自定义首选项(让我们在示例中保持简单,尽管我的首选项最终会具有更多的功能)

custom_preference.xml

<Preference 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/widget_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CustomPreference">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This is a custom preference" />
</Preference>
Run Code Online (Sandbox Code Playgroud)

一个扩展Preference并包含适当构造函数的类。

自定义首选项.kt

package com.example.myApp

import android.content.Context
import android.support.v7.preference.Preference
import android.support.v7.preference.PreferenceViewHolder
import android.util.AttributeSet
import com.example.myApp.R
import com.example.myApp.R.layout.custom_preference

class CustomPreference (context: Context,
                            attrs: AttributeSet? = null,
                            defStyleAttr: Int = R.attr.preferenceStyle,
                            defStyleRes: Int = defStyleAttr)
    : Preference(context, attrs, defStyleAttr, defStyleRes) {
    override fun onBindViewHolder(holder: PreferenceViewHolder?) {
        super.onBindViewHolder(holder)
        layoutResource = custom_preference
    }
}
Run Code Online (Sandbox Code Playgroud)

中的自定义首选项声明PreferenceScreen

首选项_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <com.example.CustomPreference
        app:key="custom_preference_key"
        app:title="This is a custom preference" />
</android.support.v7.preference.PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)

注意:我在这里为示例手动重命名了类名。此外,我必须为此项目使用支持库而不是 Androidx。

ami*_*phy 9

尝试在类的初始化中设置您的布局资源,而不是onBindViewHolder. 此外,您应该使用普通的小部件元素(不是Preference)创建布局。

自定义首选项.kt

import android.content.Context
import android.support.v7.preference.Preference
import android.support.v7.preference.PreferenceViewHolder
import android.util.AttributeSet
import kotlinx.android.synthetic.main.custom_preference_layout.view.*

class CustomPreference @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet,
        defStyleAttr: Int = 0
) : Preference(context, attrs, defStyleAttr) {

    init {
        widgetLayoutResource = R.layout.custom_preference_layout
    }

    override fun onBindViewHolder(holder: PreferenceViewHolder) {
        super.onBindViewHolder(holder)
        with(holder.itemView) {
            // do the view initialization here...

            textView.text = "Another Text"
        }
    }

} 
Run Code Online (Sandbox Code Playgroud)

res/layout/custom_preference_layout.xml

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="This is a custom preference" />

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

首选项_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.preference.PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <com.example.CustomPreference
        app:key="custom_preference_key"
        app:title="This is a custom preference" />

</android.support.v7.preference.PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)