android - 如何像 "Home", "News & Weather app"那样用手指移动滑动标签内容(TabActivity)?

标签 android animation android-tabhost gesture android-tabactivity

我有一个包含三个选项卡的选项卡 Activity 。我需要像在主页或“新闻和天气”应用程序中那样用手指移动的滑动动画。在文档和论坛的帮助下,我可以设法在某种程度上为选项卡内容设置动画,但不像上面提到的那样。

那么,你能帮我解决这个问题吗?

这是我的代码:

MyTabActivity.java

public class MyTabActivity extends TabActivity {

    int current_tab;
    TabHost tabHost;

    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;

    private GestureDetector gestureDetector;
    private Animation slideLeftIn;
    private Animation slideLeftOut;
    private Animation slideRightIn;
    private Animation slideRightOut;
    private ViewFlipper viewFlipper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.mains);

        viewFlipper = (ViewFlipper) findViewById(R.id.flipper);

        slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
        slideLeftOut = AnimationUtils
                .loadAnimation(this, R.anim.slide_left_out);
        slideRightIn = AnimationUtils
                .loadAnimation(this, R.anim.slide_right_in);
        slideRightOut = AnimationUtils.loadAnimation(this,
                R.anim.slide_right_out);

        gestureDetector = new GestureDetector(new MyGestureDetector());

        Resources res = getResources(); 
        tabHost = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;

        intent = new Intent().setClass(this, FirstTabContentAcitivity.class);
        spec = tabHost.newTabSpec("first").setIndicator("First",
                res.getDrawable(R.drawable.first)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, SecondTabContentActivity.class);
        spec = tabHost.newTabSpec("second").setIndicator("Second",
                res.getDrawable(R.drawable.second)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, ThirdTabContentAcitivity.class);
        spec = tabHost.newTabSpec("third").setIndicator("Third",
                res.getDrawable(R.drawable.third)).setContent(intent);
        tabHost.addTab(spec);


        tabHost.setCurrentTab(0);

    }


    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            Log.d("Gesture", "Detected inside class.");
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;

                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

                        viewFlipper.setInAnimation(slideLeftIn);
                        viewFlipper.setOutAnimation(slideLeftOut);
                        viewFlipper.showNext();                     

                        //overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out);
                    }
                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {


                        viewFlipper.setInAnimation(slideRightIn);
                        viewFlipper.setOutAnimation(slideRightOut);
                        viewFlipper.showPrevious();                     
                    }
                }
            } catch (Exception ex) {
                Log.d("onFling", ex.getMessage());
            }
            return false;
        }
    }

    /*
     * @Override public boolean onTouchEvent(MotionEvent event) { if
     * (gestureDetector.onTouchEvent(event)) { Log.d("onTouchEvent",
     * "screen touched"); return true; } else { return false; } }
     */

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

主.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:layout_weight="1">
        <LinearLayout android:orientation="vertical"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
            <TabWidget android:id="@android:id/tabs"
                android:layout_width="fill_parent" android:layout_height="wrap_content" />
            <ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/flipper" android:layout_width="fill_parent"
                android:layout_height="fill_parent">
                <FrameLayout android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent" android:layout_height="fill_parent">
                </FrameLayout>
            </ViewFlipper>
        </LinearLayout>
    </TabHost>
</LinearLayout>

slide_left_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="50%p" android:toXDelta="0"
        android:duration="800" />
</set>

slide_left_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-50%p"
        android:duration="800" />
</set>

slide_right_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate android:fromXDelta="-50%p" android:toXDelta="0"
        android:duration="800" />
</set>

slide_right_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="50%p"
        android:duration="800" />
</set>

最佳答案

用你的方法会非常困难和困惑...

我认为你应该使用 v4 支持包中的 ViewPager,它看起来很有用......

http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html


这是示例中的可翻转选项卡实现 (我想这正是您要找的):

http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabsPager.html

我试过了,效果很好。

关于android - 如何像 "Home", "News & Weather app"那样用手指移动滑动标签内容(TabActivity)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8015411/

相关文章:

Android adb显示 "Allow USB debugging?"窗口

android - 混淆 android.support.v7.widget.GridLayout 问题

Android - 将 TalkBack 辅助功能焦点设置到特定 View

ios - 使用 Swift 在 iOS 中平滑搜索栏动画

html - 动画返回 css 动画

java - Android - android.widget.TabHost 无法转换为 android.support.v4.app.FragmentTabHost

android - 使用 ant 使用库构建 Android 应用程序

javascript - javascript 的动画在 google T-rex 游戏中是如何工作的?

android-layout - 不想在 Android 中按下 EditText 时(键盘出现后)隐藏 EditText View ?

java - TabHost.TabSpec 内容未出现