android - TabLayout 使用 Activity 而不是 fragment

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

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

最佳答案

来自 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);
    }
});

关于android - TabLayout 使用 Activity 而不是 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33117814/

相关文章:

android - 如果我的 android webview 中没有互联网可用,如何显示消息

android - fragment 中 RecyclerView 上的 SearchView

Android ListView 返回空值?

android - 如果按下退出按钮使用汽车主页,如何完成() Activity ?

android - 如何创建允许个人资料图片上传/更改的应用程序?

android - 为什么 "Date d = new Date();"返回错误?

android - android-sdk 工具上标签不匹配太多,无法构建 docker 镜像

java - 抽屉导航中的按钮

android - 我如何判断 Activity 何时停止与 App 何时停止?

android - 为什么我的 AlarmManager 会立即触发?