Android PreferenceScreen

今天首次看到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);















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值