gpt4 book ai didi

滚动监听器上的 PreferenceFragmentCompat

转载 作者:行者123 更新时间:2023-12-05 06:18:11 27 4
gpt4 key购买 nike

我正在使用 PreferenceFragmentCompat 创建一个设置屏幕,我想在用户上下滚动时执行一些功能,因此我需要检测用户滚动,我如何才能听到偏好屏幕滚动?下面是我的布局代码

<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">

<PreferenceCategory
app:allowDividerAbove="false"
app:allowDividerBelow="false"
app:iconSpaceReserved="false"
app:title="General">

<SwitchPreference
app:iconSpaceReserved="false"
app:key="@string/theme_dark_key"
app:title="@string/theme_dark" />

</PreferenceCategory>

<PreferenceCategory
app:allowDividerAbove="false"
app:allowDividerBelow="false"
app:iconSpaceReserved="false"
app:title="Account">

<Preference
app:iconSpaceReserved="false"
app:key="@string/changeEmailKey"
app:title="@string/changeEmailTitle" />

<Preference
app:iconSpaceReserved="false"
app:key="@string/changePassKey"
app:title="@string/changePassTitle" />

<Preference
app:iconSpaceReserved="false"
app:key="@string/sign_out_key"
app:title="@string/sign_out" />

</PreferenceCategory>

<PreferenceCategory
app:allowDividerAbove="false"
app:allowDividerBelow="false"
app:iconSpaceReserved="false"
app:title="Social">

<Preference
app:iconSpaceReserved="false"
app:key="@string/blockListKey"
app:summary="@string/blockListSummery"
app:title="@string/blockListTitle" />

<Preference
app:iconSpaceReserved="false"
app:key="@string/contactKey"
app:summary="@string/contactSummery"
app:title="@string/contactTitle" />


<SwitchPreference
app:iconSpaceReserved="false"
app:key="@string/notificationKey"
app:title="@string/notificationTitle" />

</PreferenceCategory>

<PreferenceCategory
app:allowDividerAbove="false"
app:allowDividerBelow="false"
app:iconSpaceReserved="false"
app:title="Others">

<Preference
app:iconSpaceReserved="false"
app:key="@string/reportProblemKey"
app:summary="@string/reportProblemSummery"
app:title="@string/reportProblemTitle" />

</PreferenceCategory>

</PreferenceScreen>

这是我的事件布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/backgroundColor"
android:orientation="vertical">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/toolbarLayout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/backgroundColor"
app:elevation="0dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:animateLayoutChanges="true"
android:orientation="horizontal">

<ImageView
android:id="@+id/settingBack"
android:layout_width="35dp"
android:layout_height="match_parent"
android:contentDescription="@string/none"
android:src="?attr/mic_back" />

<TextView
android:id="@+id/settingTitle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:contentDescription="@string/none"
android:gravity="center_vertical"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textColor="?attr/textColor"
android:textSize="16sp" />

</LinearLayout>

</com.google.android.material.appbar.AppBarLayout>

<FrameLayout
android:id="@+id/setting_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

这是我的事件课

class SettingActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContentView(R.layout.activity_setting)

supportFragmentManager.beginTransaction().replace(R.id.setting_container, settingFragment).commit()

}
}

这是偏好片段

class SettingPreferenceFragment: PreferenceFragmentCompat() {


override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preference, rootKey)
}

最佳答案

onCreatePreferences 中,您可以将滚动监听器添加到首选项的“ListView”(在较新的 API 上,它实际上是一个 RecyclerView)。唯一的问题是当 onCreatePreferences 被调用时,getListView() 返回 null。通常我会使用 doOnPreDraw,但没有 View 可以轻松访问。相反,您可以使用 Handler 来延迟调用,直到 Views 布局完毕。

Handler(Looper.getMainLooper()).post {
listView.setOnScrollChangeListener { view, x, y, dX, dY ->
// Code here
}
}

关于滚动监听器上的 PreferenceFragmentCompat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61366507/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com