【问题标题】:Auto Scroll of theTableViewTableView 的自动滚动
【发布时间】:2012-10-04 11:49:29
【问题描述】:

我有一个要求,我必须自动滚动我的 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。如何调试问题?请帮帮我

【问题讨论】:

  • 您是否还希望它在添加一行时自动滚动到表格末尾?
  • 是的...在视图上,模拟器一次只能出现 6 行。添加第 7 行时,tableView 必须自动向下滚动才能自动显示添加的行。
  • 嘿,接受对您有帮助的答案...
  • @Shruti:没有一个答案对你有帮助。 :( 我做不到。

标签: android android-tablelayout


【解决方案1】:


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

【讨论】:

  • 感谢您的回复。但是这些行是动态添加的,甚至添加了 ScrollViews。这不是问题。但我的要求是当新行添加时,ScrollBar 应该自动滚动以显示新添加的行。我怎样才能做到这一点?你能帮帮我吗!
【解决方案2】:

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

【讨论】:

  • 感谢您的回复。但我不是从 Xml 创建 tableView。我通过代码本身创建它。那么你能告诉我如何为动态添加的 TableView 启用自动滚动吗?
  • 动态创建滚动视图并开始动态添加子视图表格布局到滚动视图
  • @Pratibha:如果有帮助,请查看我的答案
  • @Shruti:是的,我正在努力。谢谢
【解决方案3】:

在你的 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类中,使用TableLayout的id动态添加行,当行数超过屏幕高度时,会显示滚动条。

只需添加:

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

参考这个urls

要删除异常试试这个

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

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

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

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

看到这个答案Android TableRow - How to add View dynamically to certain postion?

【讨论】:

  • 当我通过 ID 引用我的表格布局时,程序崩溃了。
  • 你的表格布局不是空的吗?
  • 我在其中添加了一个 tableRow。
  • 我以同样的方式添加了行。在某个地方我错过了一些东西。所以我得到了例外。我正在尝试调试。如果成功了会告诉你的:)
  • 您在行上添加了一些视图,这些视图之前添加在一行上,这就是为什么您会收到此异常..通过这些答案stackoverflow.com/questions/8132867/…stackoverflow.com/questions/8729573/…,尝试为您要添加到表中的表行或视图
【解决方案4】:

自动滚动到末尾(假设您的 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>

【讨论】:

  • 在使用您发布的代码时,我得到了异常10-05 11:44:51.674: E/AndroidRuntime(538): java.lang.IllegalStateException: ScrollView can host only one direct child 我的代码是-VSC.fullScroll(tableLayout.FOCUS_DOWN); VSC 是 ScrollView 而 tableLayout 是我的 Table.. 你能帮我吗?如何调试它??
  • ScrollView 只能有一个孩子。请检查我的更新答案。
猜你喜欢
  • 1970-01-01
  • 2020-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-05
相关资源
最近更新 更多