我正在使用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)
}