0

tableViewを自動スクロールする必要があるという要件があります。つまり、行が動的に追加され、行が追加されるときに自動スクロール機能を有効にする必要があります。

手伝ってください。前もって感謝します。

LOGCAT:

10-05 11:26:39.474: E/AndroidRuntime(373): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.view.ViewGroup.addViewInner(ViewGroup.java:1970) 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.view.ViewGroup.addView(ViewGroup.java:1865) 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.widget.ScrollView.addView(ScrollView.java:231) 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.view.ViewGroup.addView(ViewGroup.java:1822) 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.widget.ScrollView.addView(ScrollView.java:213) 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.view.ViewGroup.addView(ViewGroup.java:1802) 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.widget.ScrollView.addView(ScrollView.java:204) 
10-05 11:26:39.474: E/AndroidRuntime(373): at com.example.animation_linear.Animation$LongOperation$1.run(Animation.java:89) 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.os.Handler.handleCallback(Handler.java:587) 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.os.Handler.dispatchMessage(Handler.java:92) 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.os.Looper.loop(Looper.java:123) 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-05 11:26:39.474: E/AndroidRuntime(373): at java.lang.reflect.Method.invokeNative(Native Method) 
10-05 11:26:39.474: E/AndroidRuntime(373): at java.lang.reflect.Method.invoke(Method.java:521)
10-05 11:26:39.474: E/AndroidRuntime(373): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

問題は、このようにasyncTask内で開始することです。これは、私の要件が画像を次々にアニメーション化することであるためです。

protected Void doInBackground(Void... params) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {             
                    int a =0;
                    for (int row = 0; row < rows; row++) {

                        tableRow = new TableRow(getApplicationContext());
                        for (int col = 0; col < layout; col++) {

                            image = new ImageView(getApplicationContext());
                            iv[a]   =   image;
                            image.setPadding(20, 20, 20, 20);
                            android.view.animation.Animation animation =    animate(a);
                            image.setAnimation(animation);
                            image.setImageResource(R.drawable.images);

                            tableRow.addView(image);

                            a++;

                            VSC.post(new Runnable() {
                                @Override
                                public void run() {
                                   VSC.fullScroll(tableLayout.FOCUS_DOWN);
                                }

                            });
                        }
                        tableLayout.addView(tableRow);
                    }
                    //VSC.addView(tableLayout);
                    HSC.addView(VSC);
                    setContentView(HSC);





                }
            });

            return null;
        }
        @Override
        protected void onPostExecute(Void result) {  
            super.onPostExecute(result);
        }
    }

デバッグモードでビューを追加する場合は、リンクjava.lang.IllegalStateExceptionを参照してください。問題をデバッグする方法は?私を助けてください

4

4 に答える 4

4


TableLayout と呼ばれる Android tableView では、ここにチュートリアルがあり、TableLayout に行を動的に追加する方法を示します。
スクロール機能については、古い回答で言及されている ScrollView をいつでも使用できます。
幸運を!

于 2012-10-04T11:57:09.687 に答える
1

あなたのxmlでこのようにしてみてください

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:scrollbars="vertical" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent">

    <TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/tLayout"
        android:scrollbars="vertical"
        >   
</TableLayout>
</Scrollview>

.java クラスで、id ofTableLayoutを使用して行を動的に追加します。行数が画面の高さを超えると、スクロールバーが表示されます。

また

追加するだけです:

android:isScrollContainer="true"

あなたのTableLayoutで以下に示すように

<TableLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/tLayout"
            android:isScrollContainer="true"
            >   
    </TableLayout>

編集

おっしゃる通り

行は動的に追加されています

そのために使用できます

TableLayout tl=(TableLayout)findViewById(R.id.tLayout);    
TableRow tr1 = new TableRow(this);
//to add row on table ,you can use loops to add multiple rows on table
tl.addView(tr1);

このURLを参照してください

例外を削除するには、これを試してください

if(tl !=null)
tl.removeAllViews();

すべてのビューを削除して毎回新しいビューを追加したくない場合

このようなインデックスを使用して行を追加できます

tl.addView(child, index); //where child is row 

この回答を参照してくださいAndroid TableRow - ビューを特定の位置に動的に追加する方法は?

于 2012-10-04T11:54:57.017 に答える
1

Table Layout を ScrollView レイアウト内に次のように配置します。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TableLayout android:id="@+id/score_table"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"> 
        <TableRow android:id="@+id/header"/>                    
    </TableLayout>
</ScrollView>
于 2012-10-04T11:55:15.177 に答える
1

最後まで自動的にスクロールするには (TableView が ScrollView に含まれていると仮定します):

scrollView.post(new Runnable() {            
    @Override
    public void run() {
           scroll.fullScroll(View.FOCUS_DOWN);              
    }
});

編集:スクロールビューに子が1つだけあることを確認してください。

このレイアウトがあるとしましょう:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >

        <TextView
            android:id="@+id/textView1"
            android:text="Column 1"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <Button
            android:id="@+id/button1"
            android:text="Column 2" />
    </TableRow>

</TableLayout>

そして、scrollView を使用したいと考えています。次のようになってはいけません。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/settingsContactInformationScrollView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dip" >

            <TextView
                android:id="@+id/textView1"
                android:text="Column 1"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <Button
                android:id="@+id/button1"
                android:text="Column 2" />
        </TableRow>

    </TableLayout>

    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
</ScrollView>

代わりに、次のように、scrollView に含めたいものすべてを 1 つのレイアウトにグループ化し、それを scrollView の子として使用する必要があります。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myScrollView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical" >

        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/tableLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dip" >

                <TextView
                    android:id="@+id/textView1"
                    android:text="Column 1"
                    android:textAppearance="?android:attr/textAppearanceLarge" />

                <Button
                    android:id="@+id/button1"
                    android:text="Column 2" />
            </TableRow>

        </TableLayout>

        <Button
            android:id="@+id/myButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />

    </LinearLayout>
</ScrollView>
于 2012-10-04T11:57:05.723 に答える