Java 具有固定标题行的动态填充表格的对齐问题(计时问题)

Java 具有固定标题行的动态填充表格的对齐问题(计时问题),java,android,tableview,dynamic,Java,Android,Tableview,Dynamic,我试图动态构建一个具有固定标题行的表。构建从网络获取数据,因此它是从任务运行的。问题如下: 如果任务花费的时间超过一定的时间,则标头对齐代码无法正常工作 看这条线。睡眠呼叫。如果该值大于0,则标题行将不对齐,否则按预期工作 我已经没有主意了。尝试了根视图上的requestLayout()、invalidate()、forceLayout()以及我能想到的任何地方 有什么想法吗 布局如下: <?xml version="1.0" encoding="utf-8"?> <Relat

我试图动态构建一个具有固定标题行的表。构建从网络获取数据,因此它是从任务运行的。问题如下:

如果任务花费的时间超过一定的时间,则标头对齐代码无法正常工作

看这条线。睡眠呼叫。如果该值大于0,则标题行将不对齐,否则按预期工作

我已经没有主意了。尝试了根视图上的requestLayout()、invalidate()、forceLayout()以及我能想到的任何地方

有什么想法吗

布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:keepScreenOn="true">
<LinearLayout android:id="@+id/header"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
    <LinearLayout android:id="@+id/bodyheader"
                  android:orientation="horizontal"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content">
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="X"/>
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Y"/>
    </LinearLayout>
</LinearLayout>
<ScrollView android:id="@+id/body"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/header">
    <hu.dwim.test.TableView android:id="@+id/bodytable"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"/>
</ScrollView>
</RelativeLayout>

以下是TableView类的代码,该类试图保持其标题对齐:

package hu.dwim.test;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TableView extends TableLayout {
private LinearLayout header;

public TableView(Context context) {
    super(context);
}

public TableView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public LinearLayout getHeader() {
    return header;
}

public void setHeader(LinearLayout header) {
    this.header = header;
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    TableRow firstRow = getChildCount() > 0 ? (TableRow)getChildAt(0) : null;
    if (firstRow != null) {
        for (int i = 0; i < header.getChildCount(); i++) {
            TextView textView = (TextView)header.getChildAt(i);
            textView.setWidth(firstRow.getChildAt(i).getWidth());
        }
    }
}
}
包hu.dwim.test;
导入android.content.Context;
导入android.util.AttributeSet;
导入android.widget.LinearLayout;
导入android.widget.TableLayout;
导入android.widget.TableRow;
导入android.widget.TextView;
公共类TableView扩展了TableLayout{
专用线路布置头;
公共表视图(上下文){
超级(上下文);
}
公共表视图(上下文、属性集属性){
超级(上下文,attrs);
}
public LinearLayout getHeader(){
返回头;
}
公共无效集合标头(LinearLayout标头){
this.header=头;
}
@凌驾
仅受保护的void布局(布尔值已更改、int l、int t、int r、int b){
超级在线布局(更改,l,t,r,b);
TableRow firstRow=getChildCount()>0?(TableRow)getChildAt(0):空;
如果(第一行!=null){
对于(int i=0;i
下面是活动和任务的代码:

package hu.dwim.test;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TestActivity extends Activity {
private TableView tableView;

private class FillTask extends AsyncTask<Object, Object, Object> {
    @Override
    protected Object doInBackground(Object... params) {
        try {
            Thread.sleep(1000);
        }
        catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    protected void onPostExecute(Object result) {
        tableView.removeAllViews();
        for (int i = 0; i < 10; i++) {
            TableRow tableRow = new TableRow(TestActivity.this);
            TextView textView = new TextView(TestActivity.this);
            textView.setText("Hello world");
            tableRow.addView(textView);
            textView = new TextView(TestActivity.this);
            textView.setText("Shouldn't this work");
            tableRow.addView(textView);
            tableView.addView(tableRow);
        }
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tableView = (TableView)findViewById(R.id.bodytable);
    tableView.setHeader((LinearLayout)findViewById(R.id.bodyheader));
    tableView.removeAllViews();
    new FillTask().execute(null);
}
}
包hu.dwim.test;
导入android.app.Activity;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.widget.LinearLayout;
导入android.widget.TableRow;
导入android.widget.TextView;
公共类测试活动扩展了活动{
私有TableView TableView;
私有类FillTask扩展了AsyncTask{
@凌驾
受保护对象doInBackground(对象…参数){
试一试{
睡眠(1000);
}
捕捉(中断异常e){
抛出新的运行时异常(e);
}
返回null;
}
受保护的void onPostExecute(对象结果){
tableView.removeallview();
对于(int i=0;i<10;i++){
TableRow TableRow=新的TableRow(TestActivity.this);
TextView TextView=新的TextView(TestActivity.this);
setText(“你好世界”);
tableRow.addView(textView);
textView=新的textView(TestActivity.this);
setText(“这不应该工作吗”);
tableRow.addView(textView);
tableView.addView(tableRow);
}
}
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tableView=(tableView)findViewById(R.id.bodytable);
tableView.setHeader((LinearLayout)findViewById(R.id.bodyheader));
tableView.removeallview();
新建FillTask().execute(空);
}
}

似乎我在发布问题后终于找到了答案…:(

更新标头的代码应该从新的处理程序()运行。post(new Runnable()

不知道为什么