2

是否可以在應用中使用多個活動而不是片段創建一個滑動的TabLayout?我有兩個活動正在嘗試使用可滑動的TabLayout創建應用程序。我正在尋找網絡,但還找不到。如果可以構建,任何人都可以提供一些鏈接或教程嗎?使用活動代替片段的TabLayout

+4

爲什麼我的問題是downvoted?作爲一名學習者,對我不熟悉的事情感到好奇並不意味着我應該低估我的想法! – Sri

+0

爲什麼要使用活動而不是碎片? – yennsarah

+0

因爲我不想在這個應用程序中使用片段。 – Sri

回答

2

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/ 這應該有助於你理解。

+0

但是無法刷卡。只能點擊。我想要一個可以滑動的 – Sri