1、首先介绍自动fragment+Viewpager
package com.ing.adapters;
import java.util.ArrayList;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
/**
* @date 2015-3-10
* @author DLX
* 自定义FragmentPagerAdapter,用于主页的Viewpager
*/
public class MainFragmentPagerAdapter extends FragmentPagerAdapter {
private ArrayList fragmentList;
public MainFragmentPagerAdapter(FragmentManager fm,ArrayList fragmentList) {
super(fm);
this.fragmentList = fragmentList;
}
@Override
public Fragment getItem(int i) {
return fragmentList.get(i);
}
@Override
public int getCount() {
return fragmentList.size();
}
}
package com.ing.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.carpool.R;
public class MessageFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_message, container,false);
}
}
其中我们出于按钮点击效果的选择,创建了一个selector
package com.ing.activities;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import com.example.carpool.R;
import com.ing.adapters.MainFragmentPagerAdapter;
import com.ing.fragments.CarpoolGroupFragment;
import com.ing.fragments.MessageFragment;
import com.ing.fragments.MineFragment;
import com.ing.fragments.SeekFragment;
public class MainActivity extends FragmentActivity{
//底部导航栏的4个按钮及布局
private Button btn_seek, btn_carpoolgroup, btn_message, btn_mine;
private LinearLayout ll;
//界面中的Viewpager
private ViewPager viewPager;
//存放Viewpager中的4个fragment的list
private ArrayList fragmentList = new ArrayList();
//自定义Viewpager切换fragment的适配器
private MainFragmentPagerAdapter fragmentPagerAdapter;
//等待waitDuration秒隐藏导航栏
private int waitDuration = 5000;
//导航栏隐藏的位移
private int animDistance = 120;
//标记导航栏是否被标记
private boolean weatherHide=false;
//动态隐藏导航栏用的Handler
private Handler hideHandler = new Handler();
//动态隐藏导航栏的自定义Runnable
private HideRunnable hideRunnable = new HideRunnable();
//标记现在是否移除了Runnable
private boolean hasRunnable=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
setViewPager();
}
/**
* 初始化
*/
private void init() {
//初始化计时Handler
hideHandler.postDelayed(hideRunnable, waitDuration);
hasRunnable=true;
//初始化Viewpager
viewPager = (ViewPager) findViewById(R.id.viewpager);
//初始化导航栏布局和按钮
ll = (LinearLayout) findViewById(R.id.ll);
btn_seek = (Button) findViewById(R.id.btn_seek);
btn_carpoolgroup = (Button) findViewById(R.id.btn_carpoolgrooup);
btn_message = (Button) findViewById(R.id.btn_message);
btn_mine = (Button) findViewById(R.id.btn_mine);
//初始化4个按钮的监听器
btn_seek.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(0);
}
});
btn_carpoolgroup.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(1);
}
});
btn_message.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(2);
}
});
btn_mine.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(3);
}
});
// 四个fragment的初始化
SeekFragment seekFragment = new SeekFragment();
CarpoolGroupFragment carpoolGroupFragment = new CarpoolGroupFragment();
MessageFragment messageFragment = new MessageFragment();
MineFragment mineFragment = new MineFragment();
fragmentList.add(seekFragment);
fragmentList.add(carpoolGroupFragment);
fragmentList.add(messageFragment);
fragmentList.add(mineFragment);
fragmentPagerAdapter = new MainFragmentPagerAdapter(
getSupportFragmentManager(), fragmentList);
}
/**
* 设置Viewpager的适配器和切换页面的监听器
*/
private void setViewPager() {
viewPager.setAdapter(fragmentPagerAdapter);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
btn_seek.performClick();
break;
case 1:
btn_carpoolgroup.performClick();
break;
case 2:
btn_message.performClick();
break;
case 3:
btn_mine.performClick();
break;
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
/**
* @date 2015-3-11
* @author DLX
* 自定义计时handler的Runnable类
*/
class HideRunnable implements Runnable{
@Override
public void run() {
hideHandler.removeCallbacks(hideRunnable);
weatherHide=true;
//设置动画
final Animation anim = new TranslateAnimation(0,0,0,animDistance);
anim.setDuration(400);
anim.setFillAfter(true);
anim.setInterpolator(new LinearInterpolator());
ll.setAnimation(anim);
ll.setVisibility(View.GONE);
anim.startNow();
}
}
/**
* 防止点击事件被Viewpager吃掉,故重新分配给onTouchEvent
*/
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
//给onTouchEvent发送事件
this.onTouchEvent(ev);
return super.dispatchTouchEvent(ev);
}
/**
* 重写屏幕点击事件
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN) {
//如果已经被隐藏了,则调用上浮动画,并且同时重启计时器(计时器在Handler中会关闭)
if (weatherHide) {
final Animation animUp = new TranslateAnimation(0,0,animDistance,0);
animUp.setDuration(400);
animUp.setFillAfter(true);
ll.setAnimation(animUp);
animUp.startNow();
ll.setVisibility(View.VISIBLE);
weatherHide=false;
hideHandler.postDelayed(hideRunnable, waitDuration);
}else {
//如果时间不够隐藏间隔,那么重置(先删除后重启)计时器
if (hasRunnable) {
hideHandler.removeCallbacks(hideRunnable);
hideHandler.postDelayed(hideRunnable, waitDuration);
}
}
}
return false;
}
package com.ing.activities;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import com.example.carpool.R;
import com.ing.adapters.MainFragmentPagerAdapter;
import com.ing.fragments.CarpoolGroupFragment;
import com.ing.fragments.MessageFragment;
import com.ing.fragments.MineFragment;
import com.ing.fragments.SeekFragment;
public class MainActivity extends FragmentActivity{
//底部导航栏的4个按钮及布局
private Button btn_seek, btn_carpoolgroup, btn_message, btn_mine;
private LinearLayout ll;
//界面中的Viewpager
private ViewPager viewPager;
//存放Viewpager中的4个fragment的list
private ArrayList fragmentList = new ArrayList();
//自定义Viewpager切换fragment的适配器
private MainFragmentPagerAdapter fragmentPagerAdapter;
//等待waitDuration秒隐藏导航栏
private int waitDuration = 5000;
//导航栏隐藏的位移
private int animDistance = 120;
//标记导航栏是否被标记
private boolean weatherHide=false;
//动态隐藏导航栏用的Handler
private Handler hideHandler = new Handler();
//动态隐藏导航栏的自定义Runnable
private HideRunnable hideRunnable = new HideRunnable();
//标记现在是否移除了Runnable
private boolean hasRunnable=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
setViewPager();
}
/**
* 防止点击事件被Viewpager吃掉,故重新分配给onTouchEvent
*/
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
//给onTouchEvent发送事件
this.onTouchEvent(ev);
return super.dispatchTouchEvent(ev);
}
/**
* 初始化
*/
private void init() {
//初始化计时Handler
hideHandler.postDelayed(hideRunnable, waitDuration);
hasRunnable=true;
//初始化Viewpager
viewPager = (ViewPager) findViewById(R.id.viewpager);
//初始化导航栏布局和按钮
ll = (LinearLayout) findViewById(R.id.ll);
btn_seek = (Button) findViewById(R.id.btn_seek);
btn_carpoolgroup = (Button) findViewById(R.id.btn_carpoolgrooup);
btn_message = (Button) findViewById(R.id.btn_message);
btn_mine = (Button) findViewById(R.id.btn_mine);
//初始化4个按钮的监听器
btn_seek.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(0);
}
});
btn_carpoolgroup.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(1);
}
});
btn_message.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(2);
}
});
btn_mine.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(3);
}
});
// 四个fragment的初始化
SeekFragment seekFragment = new SeekFragment();
CarpoolGroupFragment carpoolGroupFragment = new CarpoolGroupFragment();
MessageFragment messageFragment = new MessageFragment();
MineFragment mineFragment = new MineFragment();
fragmentList.add(seekFragment);
fragmentList.add(carpoolGroupFragment);
fragmentList.add(messageFragment);
fragmentList.add(mineFragment);
fragmentPagerAdapter = new MainFragmentPagerAdapter(
getSupportFragmentManager(), fragmentList);
}
/**
* 设置Viewpager的适配器和切换页面的监听器
*/
private void setViewPager() {
viewPager.setAdapter(fragmentPagerAdapter);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
btn_seek.performClick();
break;
case 1:
btn_carpoolgroup.performClick();
break;
case 2:
btn_message.performClick();
break;
case 3:
btn_mine.performClick();
break;
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
/**
* 重写屏幕点击事件
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN) {
//如果已经被隐藏了,则调用上浮动画,并且同时重启计时器(计时器在Handler中会关闭)
if (weatherHide) {
final Animation animUp = new TranslateAnimation(0,0,animDistance,0);
animUp.setDuration(400);
animUp.setFillAfter(true);
ll.setAnimation(animUp);
animUp.startNow();
ll.setVisibility(View.VISIBLE);
weatherHide=false;
hideHandler.postDelayed(hideRunnable, waitDuration);
}else {
//如果时间不够隐藏间隔,那么重置(先删除后重启)计时器
if (hasRunnable) {
hideHandler.removeCallbacks(hideRunnable);
hideHandler.postDelayed(hideRunnable, waitDuration);
}
}
}
return false;
}
/**
* @date 2015-3-11
* @author DLX
* 自定义计时handler的Runnable类
*/
class HideRunnable implements Runnable{
@Override
public void run() {
hideHandler.removeCallbacks(hideRunnable);
weatherHide=true;
//设置动画
final Animation anim = new TranslateAnimation(0,0,0,animDistance);
anim.setDuration(400);
anim.setFillAfter(true);
anim.setInterpolator(new LinearInterpolator());
ll.setAnimation(anim);
ll.setVisibility(View.GONE);
anim.startNow();
}
}
}