您的位置:首页 > 其它

通过手势切换Fragment

2015-11-23 00:38 260 查看
上篇文章讲到图片的自动切换,这篇讲下怎么通过手势来切换Fragment,就像微信一样,既可以点击底部的四个按钮来切换,也可以通过手势的滑动来切换。如图



手势检测用到了GestureDetector类,GestureDetector实例代表一个手势检测器,创建GestureDetector时需要传入一个GestureDetector.OnGestureListener实例,GestureDetector.OnGestureListener是一个监听器,负责对用户的手势行为提供响应。

GestureDetector.OnGestureListener里面包含的事件处理方法如下:

boolean onDown(MotionEvent e):当触碰事件按下时触发该方法。

boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY):当用户在触摸屏上拖过时触发该方法,其中velocityX,velocityX代表拖过动作在横向纵向上的速度。

onLongPress(MotionEvent e):当用户在屏幕上长按时触发该方法。

onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY):当用户在屏幕上滚动时触发该方法。

onShowPress(MotionEvent e):当用户在触屏上按下,而且还没有移动和松开时触发该方法。

onSingleTapUp(MotionEvent e):用户在触摸屏上的轻击事件将会触发该方法。

下面是代码:

public class MainActivity extends FragmentActivity implements OnClickListener,
OnGestureListener {

private TextView txtHome, txtSearch, txtFriend, txtMe;
private GestureDetector gestureDetector;
private static final int FLING_MIN_DISTANCE = 50;
private static final int FLING_MIN_VELOCITY = 0;
private SharedPreferences preferences;

@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_main);
gestureDetector = new GestureDetector(this, this);
//记录滑动前在哪个Fragment
preferences = getSharedPreferences("local",Context.MODE_PRIVATE);
preferences.getInt("localFlag", 0);
initView();
initOnclick();
if (bundle == null) {
mainTabUtil(new HomeFragment());
}
}

private void initOnclick() {
txtHome.setOnClickListener(this);
txtSearch.setOnClickListener(this);
txtFriend.setOnClickListener(this);
txtMe.setOnClickListener(this);
}

private void initView() {
txtHome = (TextView) findViewById(R.id.txtHome);
txtSearch = (TextView) findViewById(R.id.txtSearch);
txtFriend = (TextView) findViewById(R.id.txtFriend);
txtMe = (TextView) findViewById(R.id.txtMe);
}

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = preferences.edit();
switch (v.getId()) {
case R.id.txtHome:
mainTabUtil(new HomeFragment());
editor.putInt("localFlag", 0);
editor.commit();
break;
case R.id.txtSearch:
mainTabUtil(new FindFragment());
editor.putInt("localFlag", 1);
editor.commit();
break;
case R.id.txtFriend:
mainTabUtil(new FriendFragment());
editor.putInt("localFlag", 2);
editor.commit();
break;
case R.id.txtMe:
mainTabUtil(new MeInfoFragment());
editor.putInt("localFlag", 3);
editor.commit();
break;
default:
break;
}
}

public void mainTabUtil(Fragment fragment) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, fragment).commit();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}

@Override
public boolean onDown(MotionEvent e) {
//Toast.makeText(this, "触碰事件按下时触发onDown()", 0).show();
return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
int localFlag = preferences.getInt("localFlag", 0);
if (e1.getX()-e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {  //向左
turnToFragment(localFlag,"left");
} else if (e2.getX()-e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {   //向右
turnToFragment(localFlag,"right");
}
return false;
}

private void turnToFragment(int localFlag,String direction ) {
//向右边
SharedPreferences.Editor editor = preferences.edit();
if(direction.equals("right")){
if(localFlag == 0){
mainTabUtil(new FindFragment());
editor.putInt("localFlag", 1);
editor.commit();
}else if(localFlag == 1){
mainTabUtil(new FriendFragment());
editor.putInt("localFlag", 2);
editor.commit();
}else if(localFlag == 2){
mainTabUtil(new MeInfoFragment());
editor.putInt("localFlag", 3);
editor.commit();
}
}else if(direction.equals("left")){
if(localFlag == 3){
mainTabUtil(new FindFragment());
editor.putInt("localFlag", 2);
editor.commit();
}else if(localFlag == 2){
mainTabUtil(new FriendFragment());
editor.putInt("localFlag", 1);
editor.commit();
}else if(localFlag == 1){
mainTabUtil(new HomeFragment());
editor.putInt("localFlag", 0);
editor.commit();
}
}
}

@Override
public void onLongPress(MotionEvent e) {
//Toast.makeText(this, "长按时触发onLongPress()", 0).show();
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {
//Toast.makeText(this, "当用户在屏幕上滚动时触发onScroll()", 0).show();
return false;
}

@Override
public void onShowPress(MotionEvent e) {
//Toast.makeText(this, "当用户在触屏上按下,还未移动和松开时触动onShowPress()", 0).show();
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
//Toast.makeText(this, "用户在触摸屏上的轻击事件触发onSingleTapUp()", 0).show();
return false;
}

}


Demo下载地址:http://download.csdn.net/detail/barryyanggoing/9291693
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: