preferencefragment - 滚动侦听器上的 PreferenceFragmentCompat
问题描述
我正在使用 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
(它实际上是一个RecyclerView
较新的 API)。唯一的问题是当onCreatePreferences
被调用时,getListView()
返回null
。通常我会使用doOnPreDraw
,但没有视图可以轻松访问。相反,您可以使用 aHandler
将呼叫延迟到Views
布局之后。
Handler(Looper.getMainLooper()).post {
listView.setOnScrollChangeListener { view, x, y, dX, dY ->
// Code here
}
}
推荐阅读
- swift - Xcode 13 中的 Tabview 行为更改
- javascript - 使用 Soundcloud javascript API 播放私人曲目
- javascript - Firestore 监听器不会取消订阅 - VUE JS 3
- javascript - Google Apps 脚本:HTML 模板上的脚本
- c# - 如何使用两个 DbContext 将 IdentityUser 链接到 EF Core 中的实体
- python - Plotly 在 Python 中不尊重颜色字典
- google-apps-script - 我的 GSuite 域中的用户可以访问应用程序脚本代码,但不能访问已部署的 Web 应用程序?
- sql - PRIMARY KEY 仅在 PostgreSQL 中用于重复值
- python - 删除列表列表中的空格
- javascript - 使用 Chart.js 找不到上下文