博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NaviActivity实现
阅读量:4979 次
发布时间:2019-06-12

本文共 10847 字,大约阅读时间需要 36 分钟。

参考:慕课网《Android攻城狮的第二门课》和 :

该Activity采用了fragment作为数据源的ViewPager和自定义的title。现在开始实现该Activity:

 

1.在layout中添加控件,两个TextView用作头标,一个ImageView当做滑动条,再添加一个ViewPager控件。

1 
2
5
10 11
20 21
30
31
37 38
43 44 45

 

2.提取从LoginActivity中传来的值

1   /**2      * 上个页面传递过来的值3      */4     private int OldPagerNum(){5         oldpager = getIntent();6         return oldpager.getIntExtra("Authorization", 0);7     }

 

3.初始化头标textview,在这里我们给每个头标定义了一个点击事件,点击头标切换fragment

1 /** 2      * 初始化头标 3      */ 4     private void InitTextView() { 5         t1 = (TextView) findViewById(R.id.text1); 6         t2 = (TextView) findViewById(R.id.text2); 7         t1.setBackgroundColor(Color.RED); 8         t2.setBackgroundColor(Color.GREEN); 9         10         t1.setOnClickListener(new MyOnClickListener(0));11         t2.setOnClickListener(new MyOnClickListener(1));12     }

点击事件的实现

1  /** 2      * 头标点击监听 3      */ 4     public class MyOnClickListener implements View.OnClickListener { 5         private int index = 0;//页面索引 6  7         public MyOnClickListener(int i) { 8             index = i; 9         }10         //切换到对应页面11         @Override12         public void onClick(View v) {13             pager.setCurrentItem(index);14         }15     }

 

 

4.初始化动画

1     /** 2      * 初始化动画 3      */ 4 private void InitImageView() { 5 cursor = (ImageView) findViewById(R.id.cursor); 6 bmpWidth = BitmapFactory.decodeResource(getResources(), R.drawable.title_indicator) 7 .getWidth();// 获取图片宽度 8 DisplayMetrics dm = new DisplayMetrics(); 9 getWindowManager().getDefaultDisplay().getMetrics(dm); 10 int screenW = dm.widthPixels;// 获取分辨率宽度 11 offset = (screenW / 2 - bmpWidth) / 2;// 计算偏移量 12 Matrix matrix = new Matrix(); 13 matrix.postTranslate(offset, 0); 14 cursor.setImageMatrix(matrix);// 设置动画初始位置 15 }

 

5.初始化ViewPager

1     /** 2      * 初始化ViewPager 3      */ 4     private void InitViewPager(int num) { 5         pager = (ViewPager) findViewById(R.id.stuPager); 6         fragtList = new ArrayList
(); 7 //把LoginActivity传来的值传给所有的fragment 8 Bundle bundle = new Bundle(); 9 bundle.putInt("Authorization", num);10 machineInquiryActivity = new MachineInquiryActivity();11 machineInquiryActivity.setArguments(bundle);12 fragtList.add(machineInquiryActivity);13 if (num == 0) {14 stuInfoActivity = new StuInfoActivity();15 stuInfoActivity.setArguments(bundle);16 fragtList.add(stuInfoActivity);17 t2.setText("用户信息");18 }else{19 stuInquiryActivity = new StuInquiryActivity();20 stuInquiryActivity.setArguments(bundle);21 fragtList.add(stuInquiryActivity);22 t2.setText("学生信息查询");23 }24 //fragment适配器25 MyFragmentPagerAdapter adapterFrag = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragtList);26 pager.setAdapter(adapterFrag);27 pager.setCurrentItem(0);28 //换页监听器,改变ImageView位置29 pager.addOnPageChangeListener(new MyOnPageChangeListener(offset, bmpWidth, cursor));30 }

Fragment适配器

1 package com.example.wanderingzj.stumanage; 2  3 import android.support.v4.app.Fragment; 4 import android.support.v4.app.FragmentManager; 5 import android.support.v4.app.FragmentStatePagerAdapter; 6 import android.view.ViewGroup; 7  8 import java.util.List; 9 10 /**11  * Created by samsung on 2015/10/28.12  */13 public class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {14     private List
fragList;15 16 public MyFragmentPagerAdapter(FragmentManager fm, List
fragList){17 super(fm);18 this.fragList = fragList;19 }20 21 @Override22 public Fragment getItem(int position) {23 return fragList.get(position);24 }25 26 @Override27 public int getCount() {28 return fragList.size();29 }30 31 @Override32 public Object instantiateItem(ViewGroup container, int position) {33 return super.instantiateItem(container, position);34 }35 36 @Override37 public void destroyItem(ViewGroup container, int position, Object object) {38 super.destroyItem(container, position, object);39 }40 }

OnPageChangeListener类的实现,关于TranslateAnimation(float fromXDelta, float toXDelta,

                       float fromYDelta, float toYDelta)
第一个参数fromXDelta为动画起始时 X坐标上的移动位置    
第二个参数toXDelta为动画结束时 X坐标上的移动位置      
第三个参数fromYDelta为动画起始时Y坐标上的移动位置     
第四个参数toYDelta为动画结束时Y坐标上的移动位置

1 package com.example.wanderingzj.stumanage; 2  3 import android.support.v4.view.ViewPager.OnPageChangeListener; 4 import android.view.animation.Animation; 5 import android.view.animation.TranslateAnimation; 6 import android.widget.ImageView; 7  8 /** 9  * Created by samsung on 2015/11/3.10  */11 public class MyOnPageChangeListener implements OnPageChangeListener {12     private int page1_offset, currIndex, page0_offset;//page0偏移量,当前页卡编号,page1偏移量13     private ImageView cursor;//滑动条图片14 15 16     public MyOnPageChangeListener(int offset, int bmpWidth, ImageView cursor){17         currIndex = 0;18         this.cursor = cursor;19         this.page0_offset = offset;20         page1_offset = offset * 2 + bmpWidth;//滑动条图片宽度21     }22 23     @Override24     public void onPageSelected(int arg0) {25         Animation animation = null;26         switch (arg0) {27             case 0:28                 if (currIndex == 1) {29                     animation = new TranslateAnimation(page1_offset, page0_offset, 0, 0);30                 }31                 break;32             case 1:33                 if (currIndex == 0) {34                     animation = new TranslateAnimation(page0_offset, page1_offset, 0, 0);35                 }36                 break;37         }38         currIndex = arg0;39         animation.setFillAfter(true);// True:图片停在动画结束位置40         animation.setDuration(300);41         cursor.startAnimation(animation);42     }43 44     @Override45     public void onPageScrolled(int arg0, float arg1, int arg2) {46     }47 48     @Override49     public void onPageScrollStateChanged(int arg0) {50     }51 }

 完整代码:

1 package com.example.wanderingzj.stumanage;  2   3 import android.app.Activity;  4 import android.content.Intent;  5 import android.graphics.BitmapFactory;  6 import android.graphics.Color;  7   8 import android.graphics.Matrix;  9 import android.os.Bundle; 10 import android.support.v4.app.Fragment; 11 import android.support.v4.app.FragmentActivity; 12 import android.support.v4.view.ViewPager.OnPageChangeListener; 13 import android.support.v4.view.ViewPager; 14 import android.util.DisplayMetrics; 15 import android.view.View; 16 import android.view.animation.Animation; 17 import android.view.animation.TranslateAnimation; 18 import android.widget.ImageView; 19 import android.widget.TextView; 20  21 import java.util.ArrayList; 22 import java.util.List; 23  24  25 /** 26  * Created by samsung on 2015/10/28. 27  */ 28 public class NaviActivity extends FragmentActivity{ 29     private ViewPager pager; 30     private Intent oldpager; 31     private List
fragtList; 32 private StuInfoActivity stuInfoActivity; 33 private StuInquiryActivity stuInquiryActivity; 34 private MachineInquiryActivity machineInquiryActivity; 35 private TextView t1, t2;// 页卡头标 36 private int num; 37 private ImageView cursor;// 动画图片 38 private int offset = 0;// 动画图片偏移量 39 private int bmpWidth;// 动画图片宽度 40 41 @Override 42 protected void onCreate(Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 setContentView(R.layout.activity_navi); 45 num = OldPagerNum(); 46 InitTextView(); 47 InitImageView(); 48 InitViewPager(num); 49 } 50 51 /** 52 * 初始化头标 53 */ 54 private void InitTextView() { 55 t1 = (TextView) findViewById(R.id.text1); 56 t2 = (TextView) findViewById(R.id.text2); 57 t1.setBackgroundColor(Color.RED); 58 t2.setBackgroundColor(Color.GREEN); 59 60 t1.setOnClickListener(new MyOnClickListener(0)); 61 t2.setOnClickListener(new MyOnClickListener(1)); 62 } 63 64 /** 65 * 头标点击监听 66 */ 67 public class MyOnClickListener implements View.OnClickListener { 68 private int index = 0; 69 70 public MyOnClickListener(int i) { 71 index = i; 72 } 73 74 @Override 75 public void onClick(View v) { 76 pager.setCurrentItem(index); 77 } 78 } 79 80 /** 81 * 初始化ViewPager 82 */ 83 private void InitViewPager(int num) { 84 pager = (ViewPager) findViewById(R.id.stuPager); 85 fragtList = new ArrayList
(); 86 //把LoginActivity传来的值传给所有的fragment 87 Bundle bundle = new Bundle(); 88 bundle.putInt("Authorization", num); 89 machineInquiryActivity = new MachineInquiryActivity(); 90 machineInquiryActivity.setArguments(bundle); 91 fragtList.add(machineInquiryActivity); 92 if (num == 0) { 93 stuInfoActivity = new StuInfoActivity(); 94 stuInfoActivity.setArguments(bundle); 95 fragtList.add(stuInfoActivity); 96 t2.setText("用户信息"); 97 }else{ 98 stuInquiryActivity = new StuInquiryActivity(); 99 stuInquiryActivity.setArguments(bundle);100 fragtList.add(stuInquiryActivity);101 t2.setText("学生信息查询");102 }103 //fragment适配器104 MyFragmentPagerAdapter adapterFrag = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragtList);105 pager.setAdapter(adapterFrag);106 pager.setCurrentItem(0);107 //换页监听器,改变ImageView位置108 pager.addOnPageChangeListener(new MyOnPageChangeListener(offset, bmpWidth, cursor));109 }110 111 /**112 * 上个页面传递过来的值113 */114 private int OldPagerNum(){115 oldpager = getIntent();116 return oldpager.getIntExtra("Authorization", 0);117 }118 119 /**120 * 初始化动画121 */122 private void InitImageView() {123 cursor = (ImageView) findViewById(R.id.cursor);124 bmpWidth = BitmapFactory.decodeResource(getResources(), R.drawable.title_indicator)125 .getWidth();// 获取图片宽度126 DisplayMetrics dm = new DisplayMetrics();127 getWindowManager().getDefaultDisplay().getMetrics(dm);128 int screenW = dm.widthPixels;// 获取分辨率宽度129 offset = (screenW / 2 - bmpWidth) / 2;// 计算偏移量130 Matrix matrix = new Matrix();131 matrix.postTranslate(offset, 0);132 cursor.setImageMatrix(matrix);// 设置动画初始位置133 }134 }

 

转载于:https://www.cnblogs.com/wanderingzj/p/4932824.html

你可能感兴趣的文章
Atitit ftp原理与解决方案
查看>>
Atitit 项目的主体设计与结构文档 v3
查看>>
第10章:MongoDB-CRUD操作--文档--修改--修改器
查看>>
mysql备份sql,脚本
查看>>
二进制位处理
查看>>
学术之道-凌晓峰 读书笔记
查看>>
bcp 的一般用法
查看>>
C语言中volatilekeyword的作用
查看>>
Visual Studio 2010 配置Ogre
查看>>
ecstore小记
查看>>
【例3.6】过河卒(Noip2002)
查看>>
Spring 事务入门
查看>>
Codeigniter MongoDB类库
查看>>
Java设计模式——单例模式
查看>>
hdu 2732 Leapin' Lizards(最大流)Mid-Central USA 2005
查看>>
基于lnmp.org的xdebug安装
查看>>
redisTemplate如何注入到ValueOperations
查看>>
增加列并修改列的顺序
查看>>
国庆七天乐 Day2
查看>>
各种图论模型及其解答(转)
查看>>