2011-06-24 13 views
0

在我的代碼中我實現了使用Horizo​​ntalScrollView刷卡爲了我有一個適當的工作代碼,它的工作正常刷左或右現在我的問題是我的魔杖,以顯示動態頁面沒有。爲此,我增加了一些額外的代碼在,但其不工作的代碼如下Horizo​​ntaScrollView中的問題和刷屏

public class PaginationLayout extends LinearLayout { 

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

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

     setOrientation(LinearLayout.VERTICAL); 

     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; 
       } 
      } 
     }); 
     super.addView(mScroll); 

    } 

    @Override 
    public void addView(View child) { 
     mScroll.addView(child); 
    } 

    @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); 
      } 
     } 
    } 


    public void previous() { 
     mPageActive = (mPageActive > 0) ? mPageActive - 1 : 0; 
     mScroll.smoothScrollTo(mPageActive * mScroll.getWidth(), 0); 
    } 


    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 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; 
     } 
    } 
public void setPage(int noOfPageToScroll ) { 

    int pageWidth = mScroll.getWidth(); //problem is coming in this line its coming 0 always 
    int nextPage = (noOfPageToScroll + 1) * pageWidth; 

    Log.v("trace","Pagination.setPage() called with noOfPageToScroll = "+noOfPageToScroll+"and nextPage = "+nextPage+ " and pageWidth = "+pageWidth); 
    mScroll.smoothScrollTo(nextPage, 0); 
} 
} 

// -------------使用此Java文件

了我的活動IM裏面

公共無效setUpViews(){

try { 
     TableLayout table = new TableLayout(getBaseContext()); 
     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); 
     LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

       //throught inflator i m adding multiple dynamic Views in row 
     paginationLayout.addView(table); 

     // set pagination layout 
     setContentView(paginationLayout); 

    //here i m calling want to go to specific page so i m calling 
       paginationLayout.setPage(gotopageno); 
      //but every time is showing the first page and of scroll view and the log 06-24 16:38:39.156: VERBOSE/trace(894): setPage() called with noOfPageToScroll = gotopageno and nextPage = 0 and pageWidth = 0 

//之多據我所知,問題是,我不是得到mscroll.width()== 0它被稱爲平局視圖之前,也可以是其他一些原因

} catch (Exception e) { 
     Log.e("Error", "Problem in setUpViews() code 4 "); 
     e.printStackTrace(); 
    } 

} 

//請幫助我,忽略任何語法錯誤。在此先感謝

回答

0

我解決了這個問題,使用viewflipper在HorrizontalScrollView視圖大小變得非常大。