如果您愿意,可以使用 Android 支持包 - http://developer.android.com/sdk/compatibility-library.html
您可以毫不费力地修改您的活动以使用片段,以便您的标签可以像 YouTube 应用程序一样具有过渡动画。这是如何实现它的示例代码 - http://developer.android.com/sdk/compatibility-library.html
编辑:如果您不想使用支持包,也许这个实现会有所帮助
私有类 MyGestureDetector 扩展 SimpleOnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//get density
final DisplayMetrics metrics = getResources().getDisplayMetrics();
final float density = metrics.density;
//System.out.println(" in onFling() :: ");
//off path
if (Math.abs(e1.getY() - e2.getY()) > density*SWIPE_MAX_OFF_PATH)
return false;
//fling from right to left
if (e1.getX() - e2.getX() > density*SWIPE_MIN_DISTANCE && Math.abs(velocityX) > density*SWIPE_THRESHOLD_VELOCITY) {
//if the first tab is selected
if(currentSelection.equalsIgnoreCase(getString(R.string.tab_details_info))) {
//switch to second tab and save current selection
tabs.setCurrentTab(1);
currentSelection = tabs.getCurrentTabTag();
}
//if the second tab is selected
else if(currentSelection.equalsIgnoreCase(getString(R.string.tab_details_details))) {
//switch to second tab and save current selection
tabs.setCurrentTab(2);
currentSelection = tabs.getCurrentTabTag();
}
}
//fling from left to right
else if (e2.getX() - e1.getX() > density*SWIPE_MIN_DISTANCE && Math.abs(velocityX) > density*SWIPE_THRESHOLD_VELOCITY) {
//if the second tab is selected
if(currentSelection.equalsIgnoreCase(getString(R.string.tab_details_details))) {
//switch to second tab and save current selection
tabs.setCurrentTab(0);
currentSelection = tabs.getCurrentTabTag();
}
//if the third tab is selected
else if(currentSelection.equalsIgnoreCase(getString(R.string.tab_details_company))) {
//switch to second tab and save current selection
tabs.setCurrentTab(1);
currentSelection = tabs.getCurrentTabTag();
}
}
return super.onFling(e1, e2, velocityX, velocityY);
}
}
然后在您的选项卡上更改侦听器只需加载适当的动画,因为您知道在手势之前选择了哪一个,以及我们在之后切换到的那个。
@Override
public void onTabChanged(String tabId) {
//if the first tab is selected
if(currentSelection.equalsIgnoreCase(getResources().getString(R.string.tab_details_info))) {
//if we switch to second
if(tabId.equalsIgnoreCase(getResources().getString(R.string.tab_details_details))) {
linearInfo.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_left_out));
linearDetails.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_left_in));
linearCompany.setAnimation(null);
}
//if switch to third
else if(tabId.equalsIgnoreCase(getResources().getString(R.string.tab_details_company))) {
linearInfo.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_left_out));
linearDetails.setAnimation(null);
linearCompany.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_left_in));
}
}
//if the second tab is selected
else if(currentSelection.equalsIgnoreCase(getResources().getString(R.string.tab_details_details))) {
//if we switch to first
if(tabId.equalsIgnoreCase(getResources().getString(R.string.tab_details_info))) {
linearInfo.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_right_in));
linearDetails.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_right_out));
linearCompany.setAnimation(null);
}
//if switch to third
else if(tabId.equalsIgnoreCase(getResources().getString(R.string.tab_details_company))) {
linearInfo.setAnimation(null);
linearDetails.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_left_out));
linearCompany.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_left_in));
}
}
//if the third tab is selected
else if(currentSelection.equalsIgnoreCase(getResources().getString(R.string.tab_details_company))) {
//if we switch to first
if(tabId.equalsIgnoreCase(getResources().getString(R.string.tab_details_info))) {
linearInfo.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_right_in));
linearDetails.setAnimation(null);
linearCompany.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_right_out));
}
//if switch to second
else if(tabId.equalsIgnoreCase(getResources().getString(R.string.tab_details_details))) {
linearInfo.setAnimation(null);
linearDetails.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_right_in));
linearCompany.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_right_out));
}
}
currentSelection = tabId;
}
};
您还需要通过使用自定义手势检测器覆盖 onTouchListener 来捕捉手势(并且在确定手势是否为滑动动作时可能会考虑不同的屏幕密度)
对不起,答案很长,但我希望它有所帮助:)