今天首次看到Android PreferenceScreen,还不是很明白,先记录一下,代码不是很完整,大致意思
1:效果图
2:xml文件
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 事故初始报告 IncidentInitialData 应急处理 EmergencyHandling 安全检查 SecurityInspection 安全活动 SafetyActivities 月安全数据 MonthlySafetyData 安全业绩报告 SafetyPerformance 会议记录 Meetings 事故观察卡 ObservationCard 工作危险分析 TaskRiskAssessment 整改行动跟踪 ActionTracking 事故初始报告,安全业绩报告,安全检查,整改行动跟踪 --> <Preference android:key="IncidentInitialData" android:title="@string/module_incident_initial_data" android:layout="@layout/sliding_menu_preference"/> <Preference android:key="EmergencyHandling" android:title="@string/module_emergency_handling" android:layout="@layout/sliding_menu_preference"/> <Preference android:key="SecurityInspection" android:title="@string/module_security_inspection" android:layout="@layout/sliding_menu_preference"/> <Preference android:key="SafetyActivities" android:title="@string/module_safety_activities" android:layout="@layout/sliding_menu_preference"/> <Preference android:key="MonthlySafetyData" android:title="@string/module_monthly_safety_data" android:layout="@layout/sliding_menu_preference"/> <Preference android:key="SafetyPerformance" android:title="@string/module_safety_performance" android:layout="@layout/sliding_menu_preference"/> <Preference android:key="Meetings" android:title="@string/module_meetings" android:layout="@layout/sliding_menu_preference"/> <Preference android:key="ObservationCard" android:title="@string/module_observation_card" android:layout="@layout/sliding_menu_preference"/> <Preference android:key="TaskRiskAssessment" android:title="@string/module_task_risk_assessment" android:layout="@layout/sliding_menu_preference"/> <Preference android:key="ActionTracking" android:title="@string/module_action_tracking" android:layout="@layout/sliding_menu_preference"/>
3:对应fragment
public class MenuFragment extends PreferenceFragment implements OnPreferenceClickListener{ private int index = -1; private ViewPager mViewPager = null; private FrameLayout mFrameLayout = null; private MainActivity mActivity = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); mActivity = (MainActivity)getActivity(); mViewPager = (ViewPager)mActivity.findViewById(R.id.viewpager); mFrameLayout = (FrameLayout)mActivity.findViewById(R.id.content); //set the preference xml to the content view addPreferencesFromResource(R.xml.menu_left); //add listener findPreference("IncidentInitialData").setOnPreferenceClickListener(this); findPreference("EmergencyHandling").setOnPreferenceClickListener(this); findPreference("SecurityInspection").setOnPreferenceClickListener(this); findPreference("SafetyActivities").setOnPreferenceClickListener(this); findPreference("MonthlySafetyData").setOnPreferenceClickListener(this); findPreference("SafetyPerformance").setOnPreferenceClickListener(this); findPreference("Meetings").setOnPreferenceClickListener(this); findPreference("ObservationCard").setOnPreferenceClickListener(this); findPreference("TaskRiskAssessment").setOnPreferenceClickListener(this); findPreference("ActionTracking").setOnPreferenceClickListener(this); } @Override public boolean onPreferenceClick(Preference preference) { String key = preference.getKey(); //=========================== 事故初始报告 ================================= if("IncidentInitialData".equals(key)) { //以下注释代码处理,就算是当前模块,也会从新加载 //if the content view is that we need to show . show directly // if(index == 0) { // ((MainActivity)getActivity()).getSlidingMenu().toggle(); // return true; // } mActivity.setTitle(R.string.module_incident_initial_data); //otherwise , replace the content view via a new Content fragment index = 0; mFrameLayout.setVisibility(View.GONE); mViewPager.setVisibility(View.VISIBLE);