PreferenceActivity以及PreferenceFragment的源码解析

最近研究了Preference及其子类的使用,对这一系列的控件有了一定的了解。本篇博客主要是针对PreferenceActivity和PreferenceFragment的源码进行学习。

一、PreferenceActivity.java类实现的接口

PreferenceManager.OnPreferenceTreeClickListener接口的内容

PreferenceActivity.java中对OnPreferenceTreeClickListener的实现

PerferenceFragment.OnPreferenceStartFragmentCallback接口的内容

PreferenceActivity.java中对OnPreferenceStartFragmentCallback的实现

startPreferencePanel方法的实现

PreferenceActivity.java中的onPreferenceStartFragment方法在此被调用,同时这里也是
PreferenceManager.OnPreferenceTreeClickListener的实现

<!--preference_list_content.xml源码文件-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <!--比例为1-->
    <LinearLayout
        android:id="@+id/prefs_container"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1">

        <!--6-->
        <LinearLayout
            style="?attr/preferenceHeaderPanelStyle"
            android:id="@+id/headers"
            android:orientation="vertical"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="@integer/preferences_left_pane_weight">

            <ListView android:id="@android:id/list"
                style="?attr/preferenceListStyle"
                android:layout_width="match_parent"
                android:layout_height="0px"
                android:layout_weight="1"
                android:clipToPadding="false"
                android:drawSelectorOnTop="false"
                android:cacheColorHint="@android:color/transparent"
                android:listPreferredItemHeight="48dp"
                android:scrollbarAlwaysDrawVerticalTrack="true" />

            <FrameLayout android:id="@+id/list_footer"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="0" />

        </LinearLayout>

        <!--4-->
        <LinearLayout
                android:id="@+id/prefs_frame"
                style="?attr/preferencePanelStyle"
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="@integer/preferences_right_pane_weight"
                android:orientation="vertical">


            <include layout="@layout/breadcrumbs_in_fragment" />

            <android.preference.PreferenceFrameLayout android:id="@+id/prefs"
                    android:layout_width="match_parent"
                    android:layout_height="0dip"
                    android:layout_weight="1"
                />
        </LinearLayout>
    </LinearLayout>

    <!--wrap_content:包裹内容-->
    <RelativeLayout android:id="@+id/button_bar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_weight="0"
        android:visibility="gone">

        <!--居左-->
        <Button android:id="@+id/back_button"
            android:layout_width="150dip"
            android:layout_height="wrap_content"
            android:layout_margin="5dip"
            android:layout_alignParentStart="true"
            android:text="@string/back_button_label"
        />

        <!--居右-->
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true">

            <Button android:id="@+id/skip_button"
                android:layout_width="150dip"
                android:layout_height="wrap_content"
                android:layout_margin="5dip"
                android:text="@string/skip_button_label"
                android:visibility="gone"
            />

            <Button android:id="@+id/next_button"
                android:layout_width="150dip"
                android:layout_height="wrap_content"
                android:layout_margin="5dip"
                android:text="@string/next_button_label"
            />
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>
<!--preference_header_item.xml源码文件-->
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout of a header item in PreferenceActivity. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="48dp"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:gravity="center_vertical"
    android:paddingEnd="?android:attr/scrollbarSize">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="6dip"
        android:layout_marginEnd="6dip"
        android:layout_gravity="center" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="2dip"
        android:layout_marginEnd="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <TextView android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignStart="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:ellipsize="end"
            android:maxLines="2" />

    </RelativeLayout>

</LinearLayout>
<!--preference_list_content_single文件源码-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1">

        <LinearLayout
            android:id="@+id/headers"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ListView android:id="@android:id/list"
                style="?attr/preferenceFragmentStyle"
                android:layout_width="match_parent"
                android:layout_height="0px"
                android:layout_weight="1"
                android:drawSelectorOnTop="false"
                android:scrollbarStyle="@integer/preference_fragment_scrollbarStyle"
                android:cacheColorHint="@android:color/transparent"
                android:listPreferredItemHeight="48dp"
                android:scrollbarAlwaysDrawVerticalTrack="true" />

            <FrameLayout android:id="@+id/list_footer"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="0" />

        </LinearLayout>

    </LinearLayout>

    <RelativeLayout android:id="@+id/button_bar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_weight="0"
        android:visibility="gone">

        <Button android:id="@+id/back_button"
            android:layout_width="150dip"
            android:layout_height="wrap_content"
            android:layout_margin="5dip"
            android:layout_alignParentStart="true"
            android:text="@string/back_button_label"
        />
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true">

            <Button android:id="@+id/skip_button"
                android:layout_width="150dip"
                android:layout_height="wrap_content"
                android:layout_margin="5dip"
                android:text="@string/skip_button_label"
                android:visibility="gone"
            />

            <Button android:id="@+id/next_button"
                android:layout_width="150dip"
                android:layout_height="wrap_content"
                android:layout_margin="5dip"
                android:text="@string/next_button_label"
            />
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

二、PreferenceFragment.java类实现的接口

PreferenceFragment.java类中添加mPreferenceManager的OnPreferenceTreeClickListener的实例

三、PreferenceManager.java的处理

PreferenceManager(Activity activity,int firstRequestCode)此构造函数的使用
(一)、PreferenceActivity中的使用

(二)、PreferenceFragment中的使用

四、Preference.java的处理

五、PreferenceScreen.java的处理

通过preferenceManager.getOnPreferenceTreeClickListener()方法获取到的监听实例是
PreferenceFragment中实现的

此处的onPreferenceTreeClick方法调用的是PreferenceActivity中的onPreferenceStartFragment的方法

使用Preference中条目进行Fragment的跳转

package org.chromium.chrome.browser.preferencedemo;

import android.app.Fragment;
import android.content.Intent;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends PreferenceActivity implements
        PreferenceFragment.OnPreferenceStartFragmentCallback {


    public static final String EXTRA_SHOW_FRAGMENT = "show_fragment";
    public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = "show_fragment_args";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //由此处开启初始参数
        String initialFragment = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
        Bundle initialArguments = getIntent().getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);



        if (savedInstanceState == null) {
            //如果初始参数为null,initialFragment为Fragment
            if (initialFragment == null)
                initialFragment = TestPreferenceFragment.class.getName();
            Fragment fragment = Fragment.instantiate(this, initialFragment, initialArguments);
            getFragmentManager().beginTransaction()
                    .replace(android.R.id.content, fragment)
                    .commit();
        }
    }

    @Override
    public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
        startFragment(pref.getFragment(), pref.getExtras());
        return true;
    }

    /**
     * 通过传递不同的参数来进行Fragment的切换,其实质还是通过Activity的切换
     * @param fragmentClass
     * @param args
     */
    public void startFragment(String fragmentClass, Bundle args) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setClass(this, getClass());
        intent.putExtra(EXTRA_SHOW_FRAGMENT, fragmentClass);
        intent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
        startActivity(intent);
    }

}

推荐文章

Android进阶——Preference详解之初识Preference及Preference系(一)

Preferencescreen中利用intent跳转activity

注意:文章中提到的intent跳转android:package 和 android:targetClass两个属性必须按照文章中模板来

猜你喜欢

转载自blog.csdn.net/Duckdan/article/details/81457660