自动滚动theTableView

时间:2012-10-04 11:49:29

标签: android android-tablelayout

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

帮帮我。提前谢谢。

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 when adding a View in debug mode。如何调试问题?请帮帮我

4 个答案:

答案 0 :(得分:4)


在android tableView中调用TableLayout here作为教程,将向您展示如何动态地向TableLayout添加行。
对于滚动功能,您始终可以使用旧答案中提到的ScrollView 祝你好运!

答案 1 :(得分: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 TableLayout动态添加行,当行数超过屏幕高度时,将显示滚动条。

OR

只需添加:

<强>机器人:isScrollContainer = “真”

如下面的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);

参考此urls

要删除异常,请尝试此

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

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

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

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

请参阅此回答Android TableRow - How to add View dynamically to certain postion?

答案 2 :(得分: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>

答案 3 :(得分: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。它不应该是这样的:

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