督促学习——ViewPager实现滑动的Preference页面且带ActionBar

2024-04-25 03:32

本文主要是介绍督促学习——ViewPager实现滑动的Preference页面且带ActionBar,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

直接上代码有时间具体分析:

1、MainActivity.java

public class MainActivity extends FragmentActivity implementsOnPageChangeListener {private int mActionBarOptions;private ViewPager mViewPager;private View mCustomView;private ImageView mScroll1;private ImageView mScroll2;private ImageView mScroll3;private ImageView mScroll4;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.custom_tab_on_top_content);mViewPager = (ViewPager) findViewById(R.id.pager);// mViewPager// .setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));mViewPager.setAdapter(new PagerAdapter(getSupportFragmentManager(),this));mViewPager.setOnPageChangeListener(this);// 设置页面分隔线mViewPager.setPageMargin(6);mViewPager.setPageMarginDrawable(android.R.drawable.divider_horizontal_bright);// 自定义 Tab View,并添加到ActionBar上(未设置DisplayOptions,不会显示)mCustomView = LayoutInflater.from(this).inflate(R.layout.custom_tab_view, null);// getParent().getActionBar().setCustomView(mCustomView);getActionBar().setCustomView(mCustomView);mScroll1 = (ImageView) mCustomView.findViewById(R.id.scroll_1);mScroll2 = (ImageView) mCustomView.findViewById(R.id.scroll_2);mScroll3 = (ImageView) mCustomView.findViewById(R.id.scroll_3);mScroll4 = (ImageView) mCustomView.findViewById(R.id.scroll_4);}@Overrideprotected void onResume() {super.onResume();ActionBar actionBar = getActionBar();mActionBarOptions = actionBar.getDisplayOptions();// 设置DisplayOptions,显示ActionBar自定义的ViewactionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,ActionBar.DISPLAY_SHOW_CUSTOM | mActionBarOptions);setCurrentScroll(mViewPager.getCurrentItem());}@Overrideprotected void onPause() {super.onPause();getActionBar().setDisplayOptions(mActionBarOptions,ActionBar.DISPLAY_SHOW_CUSTOM | mActionBarOptions);}public void onTabClick(View view) {Log.d("TAG", "onTabClick" + view.getId());switch (view.getId()) {case R.id.tab_text_1:mViewPager.setCurrentItem(0, false);break;case R.id.tab_text_2:mViewPager.setCurrentItem(1, false);break;case R.id.tab_text_3:mViewPager.setCurrentItem(2, false);break;case R.id.tab_text_4:mViewPager.setCurrentItem(3, false);break;}}private void setCurrentScroll(int selection) {if (mScroll1 != null && mScroll2 != null && mScroll3 != null&& mScroll4 != null) {mScroll1.setVisibility(selection == 0 ? View.VISIBLE: View.INVISIBLE);mScroll2.setVisibility(selection == 1 ? View.VISIBLE: View.INVISIBLE);mScroll3.setVisibility(selection == 2 ? View.VISIBLE: View.INVISIBLE);mScroll4.setVisibility(selection == 3 ? View.VISIBLE: View.INVISIBLE);}}@Overridepublic void onPageScrollStateChanged(int arg0) {Log.d("ActiontBar", "onPageScrollStateChanged");}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {Log.d("ActiontBar", "onPageScrolled");}@Overridepublic void onPageSelected(int arg0) {setCurrentScroll(arg0);Log.d("ActiontBar", "onPageSelected");}public class PagerAdapter extends FragmentPagerAdapter {PreferenceFragment[] fragments;String[] titles;public PagerAdapter(FragmentManager fm, Context context) {super(fm);fragments = new PreferenceFragment[4];fragments[0] = new PreferenceFragment(R.xml.settings_security);fragments[1] = new PreferenceFragment(R.xml.settings_delay);fragments[2] = new PreferenceFragment(R.xml.settings_others);fragments[3] = new PreferenceFragment(R.xml.settings_about);titles = new String[4];titles[0] = context.getString(R.string.security);titles[1] = context.getString(R.string.delay);titles[2] = context.getString(R.string.others);titles[3] = context.getString(R.string.about);}@Overridepublic Fragment getItem(int position) {return (Fragment) fragments[position];}@Overridepublic int getCount() {return fragments.length;}@Overridepublic String getPageTitle(int position) {return titles[position];}}
}

PreferenceFragment.java

public class PreferenceFragment extends ListFragment {private PreferenceManager mPreferenceManager;/*** The starting request code given out to preference framework.*/private static final int FIRST_REQUEST_CODE = 100;private static final int MSG_BIND_PREFERENCES = 0;private ListView lv;private int xmlId;private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case MSG_BIND_PREFERENCES:bindPreferences();break;}}};// must be provided// public PreferenceFragment() {//// }public PreferenceFragment(int xmlId) {this.xmlId = xmlId;}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle b) {postBindPreferences();return lv;}@Overridepublic void onDestroyView() {super.onDestroyView();ViewParent p = lv.getParent();if (p != null) {((ViewGroup) p).removeView(lv);}}@Overridepublic void onCreate(Bundle b) {super.onCreate(b);if (b != null) {xmlId = b.getInt("xml");}mPreferenceManager = onCreatePreferenceManager();lv = (ListView) LayoutInflater.from(getActivity()).inflate(R.layout.preference_list_content, null);lv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);addPreferencesFromResource(xmlId);postBindPreferences();}@Overridepublic void onStop() {super.onStop();try {Method method = PreferenceManager.class.getDeclaredMethod("dispatchActivityStop");method.setAccessible(true);method.invoke(mPreferenceManager);} catch (Exception e) {e.printStackTrace();}}@Overridepublic void onDestroy() {super.onDestroy();lv = null;try {Method method = PreferenceManager.class.getDeclaredMethod("dispatchActivityDestroy");method.setAccessible(true);method.invoke(mPreferenceManager);} catch (Exception e) {e.printStackTrace();}}@Overridepublic void onSaveInstanceState(Bundle outState) {outState.putInt("xml", xmlId);super.onSaveInstanceState(outState);}@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);try {Method method = PreferenceManager.class.getDeclaredMethod("dispatchActivityResult", int.class, int.class,Intent.class);method.setAccessible(true);method.invoke(mPreferenceManager, requestCode, resultCode, data);} catch (Exception e) {e.printStackTrace();}}/*** Posts a message to bind the preferences to the list view.* <p>* Binding late is preferred as any custom preference types created in* {@link #onCreate(Bundle)} are able to have their views recycled.*/private void postBindPreferences() {if (mHandler.hasMessages(MSG_BIND_PREFERENCES)) {return;}mHandler.obtainMessage(MSG_BIND_PREFERENCES).sendToTarget();}private void bindPreferences() {final PreferenceScreen preferenceScreen = getPreferenceScreen();if (preferenceScreen != null) {preferenceScreen.bind(lv);}}/*** Creates the {@link PreferenceManager}.* * @return The {@link PreferenceManager} used by this activity.*/private PreferenceManager onCreatePreferenceManager() {try {Constructor<PreferenceManager> c = PreferenceManager.class.getDeclaredConstructor(Activity.class, int.class);c.setAccessible(true);PreferenceManager preferenceManager = c.newInstance(this.getActivity(), FIRST_REQUEST_CODE);return preferenceManager;} catch (Exception e) {e.printStackTrace();return null;}}/*** Returns the {@link PreferenceManager} used by this activity.* * @return The {@link PreferenceManager}.*/public PreferenceManager getPreferenceManager() {return mPreferenceManager;}/*** Sets the root of the preference hierarchy that this activity is showing.* * @param preferenceScreen*            The root {@link PreferenceScreen} of the preference hierarchy.*/public void setPreferenceScreen(PreferenceScreen preferenceScreen) {try {Method method = PreferenceManager.class.getDeclaredMethod("setPreferences", PreferenceScreen.class);method.setAccessible(true);boolean result = (Boolean) method.invoke(mPreferenceManager,preferenceScreen);if (result && preferenceScreen != null) {postBindPreferences();}} catch (Exception e) {e.printStackTrace();}}/*** Gets the root of the preference hierarchy that this activity is showing.* * @return The {@link PreferenceScreen} that is the root of the preference*         hierarchy.*/public PreferenceScreen getPreferenceScreen() {try {Method method = PreferenceManager.class.getDeclaredMethod("getPreferenceScreen");method.setAccessible(true);return (PreferenceScreen) method.invoke(mPreferenceManager);} catch (Exception e) {e.printStackTrace();return null;}}/*** Adds preferences from activities that match the given {@link Intent}.* * @param intent*            The {@link Intent} to query activities.*/public void addPreferencesFromIntent(Intent intent) {throw new RuntimeException("too lazy to include this bs");}/*** Inflates the given XML resource and adds the preference hierarchy to the* current preference hierarchy.* * @param preferencesResId*            The XML resource ID to inflate.*/public void addPreferencesFromResource(int preferencesResId) {try {Method method = PreferenceManager.class.getDeclaredMethod("inflateFromResource", Context.class, int.class,PreferenceScreen.class);method.setAccessible(true);PreferenceScreen prefScreen = (PreferenceScreen) method.invoke(mPreferenceManager, getActivity(), preferencesResId,getPreferenceScreen());setPreferenceScreen(prefScreen);} catch (Exception e) {e.printStackTrace();}}/*** Finds a {@link Preference} based on its key.* * @param key*            The key of the preference to retrieve.* @return The {@link Preference} with the key, or null.* @see PreferenceGroup#findPreference(CharSequence)*/public Preference findPreference(CharSequence key) {if (mPreferenceManager == null) {return null;}return mPreferenceManager.findPreference(key);}public interface OnPreferenceAttachedListener {public void onPreferenceAttached(PreferenceScreen root, int xmlId);}}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.ttdevs.actionbarandpreferencefragmentdemo"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="15"android:targetSdkVersion="17" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@android:style/Theme.DeviceDefault.Light" ><activityandroid:name=".MainActivity"android:label="@string/app_name"android:launchMode="singleTask" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>
其他配置文件略


2、效果图如下:


3、demo下载:

http://download.csdn.net/detail/ttdevs/5864875

这篇关于督促学习——ViewPager实现滑动的Preference页面且带ActionBar的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/933595

相关文章

python使用watchdog实现文件资源监控

《python使用watchdog实现文件资源监控》watchdog支持跨平台文件资源监控,可以检测指定文件夹下文件及文件夹变动,下面我们来看看Python如何使用watchdog实现文件资源监控吧... python文件监控库watchdogs简介随着Python在各种应用领域中的广泛使用,其生态环境也

el-select下拉选择缓存的实现

《el-select下拉选择缓存的实现》本文主要介绍了在使用el-select实现下拉选择缓存时遇到的问题及解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录项目场景:问题描述解决方案:项目场景:从左侧列表中选取字段填入右侧下拉多选框,用户可以对右侧

Python pyinstaller实现图形化打包工具

《Pythonpyinstaller实现图形化打包工具》:本文主要介绍一个使用PythonPYQT5制作的关于pyinstaller打包工具,代替传统的cmd黑窗口模式打包页面,实现更快捷方便的... 目录1.简介2.运行效果3.相关源码1.简介一个使用python PYQT5制作的关于pyinstall

使用Python实现大文件切片上传及断点续传的方法

《使用Python实现大文件切片上传及断点续传的方法》本文介绍了使用Python实现大文件切片上传及断点续传的方法,包括功能模块划分(获取上传文件接口状态、临时文件夹状态信息、切片上传、切片合并)、整... 目录概要整体架构流程技术细节获取上传文件状态接口获取临时文件夹状态信息接口切片上传功能文件合并功能小

python实现自动登录12306自动抢票功能

《python实现自动登录12306自动抢票功能》随着互联网技术的发展,越来越多的人选择通过网络平台购票,特别是在中国,12306作为官方火车票预订平台,承担了巨大的访问量,对于热门线路或者节假日出行... 目录一、遇到的问题?二、改进三、进阶–展望总结一、遇到的问题?1.url-正确的表头:就是首先ur

C#实现文件读写到SQLite数据库

《C#实现文件读写到SQLite数据库》这篇文章主要为大家详细介绍了使用C#将文件读写到SQLite数据库的几种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下... 目录1. 使用 BLOB 存储文件2. 存储文件路径3. 分块存储文件《文件读写到SQLite数据库China编程的方法》博客中,介绍了文

Redis主从复制实现原理分析

《Redis主从复制实现原理分析》Redis主从复制通过Sync和CommandPropagate阶段实现数据同步,2.8版本后引入Psync指令,根据复制偏移量进行全量或部分同步,优化了数据传输效率... 目录Redis主DodMIK从复制实现原理实现原理Psync: 2.8版本后总结Redis主从复制实

JAVA利用顺序表实现“杨辉三角”的思路及代码示例

《JAVA利用顺序表实现“杨辉三角”的思路及代码示例》杨辉三角形是中国古代数学的杰出研究成果之一,是我国北宋数学家贾宪于1050年首先发现并使用的,:本文主要介绍JAVA利用顺序表实现杨辉三角的思... 目录一:“杨辉三角”题目链接二:题解代码:三:题解思路:总结一:“杨辉三角”题目链接题目链接:点击这里

基于Python实现PDF动画翻页效果的阅读器

《基于Python实现PDF动画翻页效果的阅读器》在这篇博客中,我们将深入分析一个基于wxPython实现的PDF阅读器程序,该程序支持加载PDF文件并显示页面内容,同时支持页面切换动画效果,文中有详... 目录全部代码代码结构初始化 UI 界面加载 PDF 文件显示 PDF 页面页面切换动画运行效果总结主

SpringBoot实现基于URL和IP的访问频率限制

《SpringBoot实现基于URL和IP的访问频率限制》在现代Web应用中,接口被恶意刷新或暴力请求是一种常见的攻击手段,为了保护系统资源,需要对接口的访问频率进行限制,下面我们就来看看如何使用... 目录1. 引言2. 项目依赖3. 配置 Redis4. 创建拦截器5. 注册拦截器6. 创建控制器8.