微信滑动导航界面有3种方式实现,本文介绍利用ViewPager+PagerAdapter实现滑动导航:
主Activity的XML文件如下:activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.tangj.clock.MainActivity"> <include layout="@layout/top" /> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/viewpager"> </android.support.v4.view.ViewPager> </LinearLayout>top.xml内容如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp"> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="@color/c2" android:text="时钟" android:id="@+id/time" /> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="#00000000" android:text="闹钟" android:id="@+id/alarm" android:layout_gravity="center_horizontal" /> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="#00000000" android:text="计时器" android:id="@+id/timer" /> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="#00000000" android:text="秒表" android:id="@+id/stopwatch" /> </LinearLayout>MainActivityjava代码如下
public class MainActivity extends Activity implements View.OnClickListener{ private ViewPager mviewPager; private List<View> viewList; private Button bt_time; private Button bt_timer; private Button bt_stopwatch; private Button bt_alarm; private ListView content; private List<Alarm> alarmList= new ArrayList<Alarm>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initView(); initEvent(); } private void initView(){ mviewPager=(ViewPager) findViewById(R.id.viewpager); LayoutInflater layoutInflater=LayoutInflater.from(this); View time=layoutInflater.inflate(R.layout.time,null); View timer=layoutInflater.inflate(R.layout.timer,null); View alarm=layoutInflater.inflate(R.layout.alarm,null); View stopwatch=layoutInflater.inflate(R.layout.stopwatch,null); bt_time=(Button)findViewById(R.id.time); bt_timer=(Button)findViewById(R.id.timer); bt_stopwatch=(Button)findViewById(R.id.stopwatch); bt_alarm=(Button)findViewById(R.id.alarm); bt_time.setBackgroundColor(R.color.c2); content=(ListView)findViewById(R.id.list_alarm); viewList=new ArrayList<View>(); viewList.add(time); viewList.add(timer); viewList.add(alarm); viewList.add(stopwatch); MyPagerAdapter myPagerAdapter=new MyPagerAdapter(viewList); mviewPager.setAdapter(myPagerAdapter); } public void initEvent(){ bt_time.setOnClickListener(this); bt_timer.setOnClickListener(this); bt_stopwatch.setOnClickListener(this); bt_alarm.setOnClickListener(this); mviewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { restColor(); int currentItem=mviewPager.getCurrentItem(); switch (currentItem){ case 0: bt_time.setBackgroundColor(R.color.c2); break; case 1: bt_alarm.setBackgroundColor(R.color.c2); break; case 2: bt_timer.setBackgroundColor(R.color.c2); break; default: bt_stopwatch.setBackgroundColor(R.color.c2); break; } } @Override public void onPageScrollStateChanged(int state) { } }); } @Override public void onClick(View view) { restColor(); switch(view.getId()) { case R.id.time: bt_time.setBackgroundColor(R.color.c2); mviewPager.setCurrentItem(0); break; case R.id.timer: bt_timer.setBackgroundColor(R.color.c2); mviewPager.setCurrentItem(2); break; case R.id.stopwatch: bt_stopwatch.setBackgroundColor(R.color.c2); mviewPager.setCurrentItem(3); break; default: bt_alarm.setBackgroundColor(R.color.c2); mviewPager.setCurrentItem(1); } } private void restColor(){ bt_alarm.setBackgroundColor(Color.WHITE); bt_stopwatch.setBackgroundColor(Color.WHITE); bt_timer.setBackgroundColor(Color.WHITE); bt_time.setBackgroundColor(Color.WHITE); } }