0

我有一个要求,我必须自动滚动我的 tableView .. 也就是说,行是动态添加的,我必须在添加行时启用 autoScroll 功能。

帮帮我。提前致谢。

日志猫:

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动态添加行,当行数超过屏幕高度时,将显示滚动条。

或者

只需添加:

安卓:isScrollContainer="真"

如下表所示

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

参考这个网址

并删除异常试试这个

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

如果您不想每次都删除所有视图并添加新视图

您可以像这样使用索引添加行

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

看到这个答案Android TableRow - 如何动态地将视图添加到某个位置?

于 2012-10-04T11:54:57.017 回答
1

将表格布局放入 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);              
    }
});

编辑:确保您的滚动视图只有一个孩子。

假设你有这样的布局:

<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 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 中的所有内容分组到一个布局中,并将其用作 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 回答