基于Android的计步器(Pedometer)的讲解(一)——Fragment页面跳转
来源:互联网 发布:如何在淘宝网上买课件 编辑:程序博客网 时间:2024/06/07 01:11
首先需要做的是实现几个页面的跳转,既类似微信的点击按钮,切换页面。
话不多说,这是一个简单的页面切换的Demo,先贴几张实现的效果:
图片的底部是5个RadioButton,用来进行页面之间的切换。剩下的部分是一个LinearLayout的布局,用来镶嵌5个Fragment(碎片)的页面。
一共用到5个类:MainActivity、MyAdapter、FragmentHistory、FragmentAnalysis、FragmentPedometer、FragmentPK、FragmentSet
MainActivity的主要代码如下:
package com.example.changepage1;import java.util.ArrayList;import java.util.List;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.widget.RadioGroup;public class MainActivity extends FragmentActivity {private RadioGroup rg;private List<Fragment> fragments;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.page_mian);rg = (RadioGroup) findViewById(R.id.radioGroup);//实例化radiogroupfragments = new ArrayList<Fragment>();//分别添加5个fragmentfragments.add(new FragmentHistory());fragments.add(new FragmentAnalysis());fragments.add(new FragmentPedometer());fragments.add(new FragmentPK());fragments.add(new FragmentSet());new MyAdapter(this, fragments, R.id.Fragment, rg);//设置适配器}}
MyAdapter的代码如下:
package com.example.changepage1;import java.util.List;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentTransaction;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;/** * 这是自己写的一个适配器 * * @author 李垭超 Date: 2015-1-2 Time: 下午3点 */public class MyAdapter implements OnCheckedChangeListener {private List<Fragment> fragments; // 一个tab页面对应一个Fragmentprivate RadioGroup rgs; // 用于切换tabprivate FragmentActivity fragmentActivity; // Fragment所属的Activityprivate int fragmentContentId; // Activity中所要被替换的区域的idprivate int currentTab = 2; // 当前Tab页面索引public MyAdapter(FragmentActivity fragmentActivity,List<Fragment> fragments, int fragmentContentId, RadioGroup rgs) {this.fragments = fragments;this.rgs = rgs;this.fragmentActivity = fragmentActivity;this.fragmentContentId = fragmentContentId;// 默认显示第一页FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction();ft.add(fragmentContentId, fragments.get(2));ft.commit();this.rgs.setOnCheckedChangeListener(this);}/** * 监听radiobutton改变时间,当选择了不同的radiobutton会出发这个函数 */public void onCheckedChanged(RadioGroup group, int checkedId) {for (int i = 0; i < rgs.getChildCount(); i++) {if (rgs.getChildAt(i).getId() == checkedId) {Fragment fragment = fragments.get(i);FragmentTransaction ft = obtainFragmentTransaction(i);getCurrentFragment().onPause(); // 暂停当前tabif (fragment.isAdded()) {fragment.onResume(); // 启动目标tab的onResume()} else {ft.add(fragmentContentId, fragment);}showTab(i); // 显示目标tabft.commit();}}}public Fragment getCurrentFragment() {return fragments.get(currentTab);}private void showTab(int idx) {for (int i = 0; i < fragments.size(); i++) {Fragment fragment = fragments.get(i);FragmentTransaction ft = obtainFragmentTransaction(idx);if (idx == i) {ft.show(fragment);} else {ft.hide(fragment);}ft.commit();}currentTab = idx; // 更新目标tab为当前tab}private FragmentTransaction obtainFragmentTransaction(int index) {FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction();// 设置切换动画if (index > currentTab) {ft.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_left_out);} else {ft.setCustomAnimations(R.anim.slide_right_in,R.anim.slide_right_out);}return ft;}}
FragmentHistory、FragmentAnalysis、FragmentPedometer、FragmentPK、FragmentSet的代码依次为:
package com.example.changepage1;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * 这是分析七天步数的碎片 * Author: 李垭超 email:296777513@qq.com * Date: 2015-1-2 * Time: 下午2:39 */public class FragmentAnalysis extends Fragment{private View view;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {view = inflater.inflate(R.layout.analysis, container, false);return view;}}
package com.example.changepage1;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * 这是历史记录的碎片 * Author: 李垭超 email:296777513@qq.com * Date: 2015-1-2 * Time: 下午2:39 */public class FragmentHistory extends Fragment{private View view;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {view = inflater.inflate(R.layout.history, container, false);return view;}}
package com.example.changepage1;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * 这是记步的碎片 * Author: 李垭超 email:296777513@qq.com * Date: 2015-1-2 * Time: 下午2:39 */public class FragmentPedometer extends Fragment{private View view;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {view = inflater.inflate(R.layout.pedometer, container, false);return view;}}
package com.example.changepage1;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * 这是记步的碎片 * Author: 李垭超 email:296777513@qq.com * Date: 2015-1-2 * Time: 下午2:39 */public class FragmentPK extends Fragment{private View view;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {view = inflater.inflate(R.layout.pk, container, false);return view;}}
package com.example.changepage1;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * 这是设置的碎片 * Author: 李垭超 email:296777513@qq.com * Date: 2015-1-2 * Time: 下午2:39 */public class FragmentSet extends Fragment{private View view;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {view = inflater.inflate(R.layout.set, container, false);return view;}}
这个例子的的完整demo在:
http://download.csdn.net/detail/a296777513/8320619
完整的Pedometer项目在github和CSDN上:
github下载
CSDN下载
4 0
- 基于Android的计步器(Pedometer)的讲解(一)——Fragment页面跳转
- 基于Android的计步器(Pedometer)的讲解(一)——Fragment页面跳转
- 基于Android的计步器(Pedometer)的讲解(六)——ListView下拉刷新页面
- 基于Android的计步器(Pedometer)的讲解
- 基于Android的计步器(Pedometer)的讲解(五)——跟随界面滑动的指示器
- 基于Android的计步器(Pedometer)的讲解(二)——柱状图分析
- 基于Android的计步器(Pedometer)的讲解(二)——柱状图分析2
- 基于Android的计步器(Pedometer)的讲解(三)——CircleBar(圆形进度条)
- 基于Android的计步器(Pedometer)的讲解(四)——后台记步
- 基于Android的计步器(Pedometer)的讲解(七)——ExpandableListView
- 基于Android的计步器(Pedometer)的讲解-序
- 基于Android的计步器(Pedometer)的讲解-序(修改)
- android的fragment讲解
- Android activity跳转到另一个activity的指定fragment页面
- 【页面】手机页面跳转的实现方法(三)——Fragment
- android fragment 之间的跳转
- #android# eventbus fragment的跳转
- android 的页面跳转
- 数据平台调度系统的设计
- hoj 1081 Scramble Sort
- dell md3200i 磁盘阵列
- 守护进程与僵尸进程
- VS之动态库实现和调用
- 基于Android的计步器(Pedometer)的讲解(一)——Fragment页面跳转
- Leetcode -- 3sum
- 123茶楼,众筹...
- Linux内核设计与实现 学习笔记(5)进程地址空间
- 像建筑设计师一样去写程序
- 【嵌入式linux】(第五步):使用eclipse集成开发环境开发第一个嵌入式Linux驱动
- 【嵌入式linux】(第四步):安装NFS服务,并挂载到开发板
- Linux内核设计与实现 学习笔记(6)页高速缓存和页回写
- 多路径属主自己更改的问题