0

我知道如何使用XML佈局開發的標籤活動,也是我知道如何在Android Market應用被認爲是做尋呼的Android實現分頁選項卡上的佈局

,但我沒有得到如何合併這個分頁概念與標籤活性和如何從Java代碼創建tabactivity:

見下面的代碼我使用線性佈局用於尋呼:

sample.java

public class Sample extends Activity { 

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

     // creates the Pagination Layout 
     PaginationLayout paginationLayout = new PaginationLayout(this); 
     paginationLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

     // creates content only for sample 
     TableLayout table = new TableLayout(this); 
     table.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
     table.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL); 

     TableRow row = new TableRow(this); 
     row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
     table.addView(row); 

     TableRow row2 = new TableRow(this); 
     row2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
     table.addView(row2); 

     for(int i = 0; i< 50;i++){ 
      Button button = new Button(this); 
      button.setText("Button " + i); 
      if (i%2==0) { 
       row.addView(button); 
      } else { 
       row2.addView(button); 
      } 
     } 

     // add the content in pagination 
     paginationLayout.addView(table); 
     // set pagination layout 
     setContentView(paginationLayout); 
    } 
} 

和分頁佈局的代碼是:

public class PaginationLayout extends LinearLayout { 

    private int mPageActive = 0; 
    private HorizontalScrollView mScroll; 
    private LinearLayout mBar; 

    public PaginationLayout(Context context) { 
     super(context); 

     setOrientation(LinearLayout.VERTICAL); 

     // creates the class that will control the gestures and apply it in the 
     // scroll 
     final GestureDetector mGestureDetector = new GestureDetector(new MySimpleOnGestureListener()); 

     mScroll = new HorizontalScrollView(context); 
     mScroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
     mScroll.setOnTouchListener(new View.OnTouchListener() { 

      public boolean onTouch(View v, MotionEvent event) { 
       if (mGestureDetector.onTouchEvent(event)) { 
        return true; 
       } else { 
        return false; 
       } 
      } 
     }); 

     // creates Previous and Next buttons 
     Button btnPrevious = new Button(getContext()); 
     btnPrevious.setLayoutParams(new LayoutParams(150, LayoutParams.FILL_PARENT)); 
     btnPrevious.setText("Previous"); 
     btnPrevious.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       previous(); 
      } 
     }); 

     Button btnMid = new Button(getContext()); 
     btnMid.setLayoutParams(new LayoutParams(150, LayoutParams.FILL_PARENT)); 
     btnMid.setText("Mid"); 
     btnMid.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       next(); 
      } 
     }); 

     Button btnNext = new Button(getContext()); 
     btnNext.setLayoutParams(new LayoutParams(150, LayoutParams.FILL_PARENT)); 
     btnNext.setText("Next"); 
     btnNext.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       next(); 
      } 
     }); 

     // bar that include the buttons 
     mBar = new LinearLayout(getContext()); 
     mBar.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
     mBar.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL); 
     mBar.setBackgroundColor(Color.GRAY); 
     mBar.addView(btnPrevious); 
     mBar.addView(btnNext); 
     //mBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.android_1080p)); 
     mBar.setVisibility(LinearLayout.INVISIBLE); 

     // add bar and scroll in the super (LinearLayout) 
     super.addView(mBar); 
     super.addView(mScroll); 
    } 

    /** 
    * All that the user include is added in the scroll 
    */ 
    @Override 
    public void addView(View child) { 
     mScroll.addView(child); 
    } 

    /** 
    * Controls if the top bar should appear or not 
    */ 
    @Override 
    protected void onSizeChanged(int arg0, int arg1, int arg2, int arg3) { 
     super.onSizeChanged(arg0, arg1, arg2, arg3); 
     View chield = mScroll.getChildAt(0); 
     if (chield != null) { 
      if (chield.getMeasuredWidth() > getWidth()) { 
       mBar.setVisibility(LinearLayout.VISIBLE); 
      } else { 
       mBar.setVisibility(LinearLayout.INVISIBLE); 
      } 
     } 
    } 

    /** 
    * does the effect "back a page" 
    */ 
    public void previous() { 
     mPageActive = (mPageActive > 0) ? mPageActive - 1 : 0; 
     mScroll.smoothScrollTo(mPageActive * mScroll.getWidth(), 0); 
    } 

    /** 
    * does the effect "forward a page" 
    */ 
    public void next() { 
     int pageWidth = mScroll.getWidth(); 
     int nextPage = (mPageActive + 1) * pageWidth; 
     if (nextPage - mScroll.getScrollX() <= pageWidth) { 
      mScroll.smoothScrollTo(nextPage, 0); 
      mPageActive++; 
     } else { 
      mScroll.smoothScrollTo(mScroll.getScrollX(), 0); 
     } 
    } 

    /** 
    * Private class that controls the gestures, forward or back a page. 
    */ 
    private class MySimpleOnGestureListener extends SimpleOnGestureListener { 

     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 

      if (e1 != null && e2 != null) { 
       if (e1.getX() - e2.getX() > 0) { 
        // forward... 
        next(); 
        return true; 
       } else if (e2.getX() - e1.getX() > 0) { 
        // back... 
        previous(); 
        return true; 
       } 
      } 

      return false; 
     } 
    } 
} 

回答

1

我發現this它可能有助於;)

+0

感謝回答angie..i didnt找到了解決這個我會嘗試你的代碼,並會告訴我現在能否做到這一點 – Shruti

+0

你能否提供一個鏈接到源代碼? – Shruti

+0

我測試過這一個http://thepseudocoder.wordpress.com/2011/10/13/android-tabs-viewpager-swipe-able-tabs-ftw/它爲我工作,希望這就是你想要的 – Angie

相關問題