【问题标题】:TabLayout using activities instead of fragmentsTabLayout 使用活动而不是片段
【发布时间】:2015-10-14 06:08:25
【问题描述】:

是否可以使用多个活动而不是片段在应用中创建滑动的 TabLayout?我有两个活动,我试图创建一个使用可滑动 TabLayout 的应用程序。我正在网上搜索这个,但还没有找到。如果可以构建,任何人都可以提供一些链接或教程吗?

【问题讨论】:

  • 为什么我的问题被否决了?作为一名学习者并对我不太了解的事物感到好奇并不意味着我认为应该被低估!
  • 为什么要使用Activity而不是Fragments?
  • 因为我不想在这个应用中使用片段。
  • 您可以使用 onTouchEvent/GestureDetectorCompat,检测“向左/向右滑动”手势,然后根据意图开始下一个活动。但在大多数情况下,最好使用片段。
  • 您可以使用developer.android.com/training/gestures/detector.html 获取第一信息:)

标签: android android-fragments android-activity android-tablayout


【解决方案1】:

来自GestureDetectorOnSwipeTouchListener

public class MainActivity extends Activity { 

    private GestureDetectorCompat mDetector; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDetector = new GestureDetectorCompat(this, new MyGestureListener());
    }

    @Override 
    public boolean onTouchEvent(MotionEvent event){ 
        this.mDetector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }

    class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
        private static final String DEBUG_TAG = "Gestures"; 

       private static final int SWIPE_THRESHOLD = 100;
       private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

          @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
                result = true;
            } 
            else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                }
                result = true;

        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }


    public void onSwipeRight() {
    }

    public void onSwipeLeft() {
    }

    public void onSwipeTop() {
    }

    public void onSwipeBottom() {
    }        
}

第二来源的用法:

imageView.setOnTouchListener(new OnSwipeTouchListener() {
    public void onSwipeTop() {
        Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show();
    }
    public void onSwipeRight() {
        Toast.makeText(MyActivity.this, "right", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }
    public void onSwipeLeft() {
        Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this, FirstActivity.class);
        startActivity(intent);
    }
    public void onSwipeBottom() {
        Toast.makeText(MyActivity.this, "bottom", Toast.LENGTH_SHORT).show();
    }

    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
});

【讨论】:

    【解决方案2】:

    是的 - 这是可能的,但没有那么有效。这就像用 2-3 年的解决方案解决您的问题一样。如果您仍想继续,可以参考以下链接: http://www.mkyong.com/android/android-tablayout-example/ 这应该有助于你理解。

    【讨论】:

    • 但这不能刷卡。只能点击。我想要一个可滑动的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 2020-07-30
    • 2016-03-27
    • 1970-01-01
    • 2018-11-29
    相关资源
    最近更新 更多