简体   繁体   中英

How to implement scroll view in java code for android?

I have this java code that creates buttons on the draw that exist in a table. I am trying to make the whole table be able to scroll vertical but every way I've tried gives me an error. When I uncomment out my 2 lines of scroll view my app crashes. Any help would be appreciated.

public void drawAllGames(UserData u){
    int nGames = u.lastGame;
    TableLayout table = (TableLayout) findViewById(R.id.TableForButtons);
    //ScrollView scroll = new ScrollView(this);
    //((ScrollView) scroll).addView(table);

    if(nGames != 0) {
        TableRow tableRow = new TableRow(this);
        table.addView(tableRow);
        TextView title1 = new TextView(this);
        title1.setText("Awaiting you");
        tableRow.addView(title1);

        //forloops fills games awaiting my turn
        for (int i = 0; i < nGames; i++) {
            if (u.isMyTurn(u.allGames[i].gameid)) {
                tableRow = new TableRow(this);
                table.addView(tableRow);
                Button gameButton = new Button(this);
                gameButton.setText("Play");
                tableRow.addView(gameButton);
                TextView label = new TextView(this);
                String partner;
                if (u.allGames[i].partner_A == u.getName())
                    partner = u.allGames[i].partner_B;
                else
                    partner = u.allGames[i].partner_A;
                label.setText("      " + partner + "             Round: " + u.allGames[i].getRound());
                tableRow.addView(label);
                final int game = i;
                gameButton.setOnClickListener(
                        new Button.OnClickListener() {
                            public void onClick(View v) {
                                resumeGame(game);
                            }
                        }
                );
            }
        }

        tableRow = new TableRow(this);
        table.addView(tableRow);
        TextView title2 = new TextView(this);
        title2.setText("Waiting for partner");
        tableRow.addView(title2);

        //forloops fills games awaiting my turn
        for (int i = 0; i < nGames; i++) {
            if (!u.isMyTurn(u.allGames[i].gameid)) {
                tableRow = new TableRow(this);
                table.addView(tableRow);
                TextView label = new TextView(this);
                String partner;
                if (u.allGames[i].partner_A == u.getName())
                    partner = u.allGames[i].partner_B;
                else
                    partner = u.allGames[i].partner_A;
                label.setText("      " + partner + "             Round: " + u.allGames[i].getRound());
                tableRow.addView(label);
                final int game = i;
            }
        }
    }
    else{
        TableRow tableRow = new TableRow(this);
        table.addView(tableRow);
        TextView title3 = new TextView(this);
        title3.setText("You have no games, start a new one now!");
        tableRow.addView(title3);
    }
}

You have to use ScrollView in your layout. This is the use of ScrollView:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="1">

    <!-- everything you already have -->

</TableLayout>
</ScrollView>

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