2013-04-13 83 views
0

我有一個類來顯示一個表,我希望它是水平和垂直滾動,我已經看到了一些解決方案,但我無法實現他們,我把我的觀點在Horizo​​ntalScrollView,這讓我做了水平滾動,現在我想設置Horizo​​ntalScrollView爲滾動型的孩子。像這樣的東西。垂直滾動和水平滾動的Android

ApuestasScoreCard draw; 

    public class ApuestasScore extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    draw = new ApuestasScoreCard(this); 
    draw.setBackgroundColor(Color.WHITE); 

     ScrollView sv = new ScrollView(this) 
    HorizontalScrollView hsv = new HorizontalScrollView(this); 
     sv.addView(hsv); 
    hsv.addView(draw); 

    setContentView(hsv); 
    } 

然後在我的視圖類,我實現OnMeasure()方法來渲染視圖。我的問題是:這是一個解決方案,或者我想要做的是不可能的。順便說一下,當我運行該解決方案的logcat的說:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 

這裏是我的視圖類

public class ApuestasScoreCard extends View { 
ApuestasScore apuestas; 

public ApuestasScoreCard(Context context) { 
    super(context); 
    this.apuestas = (ApuestasScore) context; 
    setFocusable(true); 
    setFocusableInTouchMode(true); 

      //..Not Important Code 

/* (non-Javadoc) 
* @see android.widget.HorizontalScrollView#onMeasure(int, int) 
*/ 
@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    // TODO Auto-generated method stub 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     int width = MeasureSpec.getSize(widthMeasureSpec+2000); 
     int height = MeasureSpec.getSize(heightMeasureSpec+500); 
     setMeasuredDimension(width, height); 

} 

    @Override 
protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
    width = w/26f; 
    height = h/29f; 
    width2 = w/6.5f; 
    height2 = h/5f; 
    getRect(selX, selY, selRect); 

    super.onSizeChanged(w, h, oldw, oldh); 
} 

    @Override 
protected void onDraw(Canvas canvas) { 
    // Draw the background...FFFF00 23F607 



      for (int i = 0; i < 29; i++) { 
       if (i < 3) { 
        canvas.drawLine(0, i * height, getWidth(), i * height, light);// Horizontales 

        canvas.drawLine(0, i * height + 1, width * 2, i * height + 1, 
          hilite); 
       } 
       if (i == 2 || i == 5 || i == 8 || i == 11 || i == 14 || i == 17 
         || i == 20 || i == 23 || i == 26 || i == 29) { 
        canvas.drawLine(0, i * height, getWidth(), i * height, dark);// Horizontales 

        canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1, 
          hilite); 
       } 
      } 
      for (int i = 0; i < 29; i++) { 

       canvas.drawLine(width * 2, i * height, getWidth() + 100, 
         i * height, light);// Horizontales 

       canvas.drawLine(width * 2, i * height + 1, getWidth() + 100, i 
         * height + 1, hilite); 

       if (i > 1 && i < 12) { 
        canvas.drawLine(i * width, 0, i * width, getHeight(), light); 

        canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(), 
          hilite); 
       } 
    } 

} 
    } 

回答

0

請發表您的版式文件了。

發生了什麼我認爲你已經添加了一個孩子到你的ScrollView。現在,滾動型只能包含一個孩子,這可以是另一個容器,這反過來又可以包含其他的看法。

你可以做的是滾動型刪除任何一個孩子,然後添加Horizo​​ntalScrollView與已經加入到它(到HSV)的兒童。

查看關於ScrollView的文檔& HorizontalScrollView

0

拍攝橫向滾動視圖中的XML覆蓋在一個版面爲線性或相對該水平滾動視圖後,之後把垂直滾動視圖中的XML,所以最終你可以在水平和垂直也移動你的XML。嘗試一下。

0

謝謝你,我找到了解決辦法,必須是按以下順序:

ScrollView sv = new ScrollView(this); 
HorizontalScrollView hsv = new HorizontalScrollView(this); 
hsv.addView(sv); 
sv.addView(draw); 

setContentView(hsv); 
1

我知道這個職位是舊的,但因爲我無法在其他地方找到解決辦法,不得不做我自己,我決定分享我的解決方案,我爲此做了一個自定義組件,隨時可以使用它:

https://gist.github.com/androidseb/9902093