简体   繁体   中英

Vertical Scroll and Horizontal Scroll Android

I have a class to display a table, and I want it to be Horizontally and Vertically Scrollable, I've seen a few solutions but I can't implement them, I put my view in a HorizontalScrollView, and that let me do the Horizontal Scroll, now I'm trying to set that HorizontalScrollView as a child of a ScrollView. something like this.

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

then in my View class, I implement the OnMeasure() method to render the View. my question is: Is this a solution or what i'm trying to do is imposible. by the way, when i run this solution the logcat says:

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

here is my View class

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

}
    }

Thank you, I found the solution, has to be in this order:

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

setContentView(hsv);

I know this post is old but since I could not find the solution elsewhere and had to do it myself, I decided to share my solution, I made a custom component for that, feel free to use it :

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

Please post your Layout file too.

What's happening I think is that you have already added a child to your ScrollView . Now ScrollView can contain only one child, which can be an another container, which in turn can contain other views.

What you can do is remove any child from ScrollView and then add HorizontalScrollView with children already added to it(to HSV).

Check out the documentation for ScrollView & HorizontalScrollView .

Take horizontal scrollview in xml after that covers this horizontal scrollview in one layout either linear or relative, after that put vertical scrollview in xml, so ultimate you can move your xml in both horizontal and also vertical. try it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM