Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android onLayout()和AsyncTask()无法一起使用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要一个带有固定标题的可滚动表,所以我遵循 this great blog并且一切都很好.

这个想法是使用一个表作为标题,一个表用于在scrollview中添加内容,它们都在自定义的LinearLayout中.在自定义的LinearLayout中,我们将覆盖onLayout()以获取每行的最大宽度,并为header和content表的每一行设置宽度.

这是活动及其布局:

package com.stylingandroid.ScrollingTable;

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

public class ScrollingTable extends LinearLayout
{
    public ScrollingTable( Context context )
    {
        super( context );
    }
public ScrollingTable( Context context,AttributeSet attrs )
{
    super( context,attrs );
}

@Override
protected void onLayout( Boolean changed,int l,int t,int r,int b )
{
    super.onLayout( changed,l,t,r,b );

    TableLayout header = (TableLayout) findViewById( R.id.HeaderTable );
    TableLayout body = (TableLayout) findViewById( R.id.bodyTable );

    if (body.getChildCount() > 0 ) {
        TableRow bodyRow = (TableRow) body.getChildAt(0);
        TableRow headerRow = (TableRow) header.getChildAt(0);

        for ( int cellnum = 0; cellnum < bodyRow.getChildCount(); cellnum++ ){
            View bodyCell = bodyRow.getChildAt(cellnum);
            View headerCell = headerRow.getChildAt(cellnum);
            int bodyWidth = bodyCell.getWidth();
            int headerWidth = headerCell.getWidth();
            int max = Math.max(bodyWidth,headerWidth);
            TableRow.LayoutParams bodyParams = (TableRow.LayoutParams)bodyCell.getLayoutParams();
            bodyParams.width = max;
            TableRow.LayoutParams headerParams = (TableRow.LayoutParams)headerCell.getLayoutParams();
            headerParams.width = max;
        }       
    }
}
}@H_772_12@ 
 @H_269_5@main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://scheR_586_11845@as.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.stylingandroid.ScrollingTable.ScrollingTable
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">

        <TableLayout 
            android:layout_height="wrap_content"
            android:layout_width="match_parent" 
            android:id="@+id/HeaderTable">
        </TableLayout>

        <ScrollView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TableLayout 
                android:layout_height="wrap_content"
                android:layout_width="match_parent" 
                android:id="@+id/BodyTable">
            </TableLayout>

        </ScrollView>

    </com.stylingandroid.ScrollingTable.ScrollingTable>

</LinearLayout>@H_772_12@ 
 

主要活动

package com.stylingandroid.ScrollingTable;

import android.app.Activity;
import android.app.progressDialog;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.bundle;
import android.widget.TableLayout;
import android.widget.TableRow;

    import android.widget.TextView;

    public class ScrollingTableActivity extends Activity
    {
        private String[][] tableData = {
                {"header11111111111","header2","header3","header4"},{"column1","column1","column1"},"column1"}
        };
        /** Called when the activity is first created. */
        @Override
        public void onCreate( Bundle savedInstanceState )
        {
            super.onCreate( savedInstanceState );
            setContentView( R.layout.main );
            TableLayout tableHeader = (TableLayout)findViewById(R.id.HeaderTablE);
            TableLayout tableBody = (TableLayout)findViewById(R.id.bodyTablE);

        appendRows(tableHeader,tableBody,tableData);
}

private void appendRows(TableLayout tableHeader,TableLayout tableContent,String[][] amortization) {
    int rowSize=amortization.length;
    int colSize=(amortization.length > 0)?amortization[0].length:0;
    for(int i=0; i<rowSize; i++) {
        TableRow row1 = new TableRow(this);

        for(int j=0; j<colSize; j++) {
            TextView c = new TextView(this);
            c.setText(amortization[i][j]);
            c.setPadding(3,3,3);
            if (i == 0) {
                c.setTextColor(Color.bLACK);
            }
            row1.addView(c);
        }

        if (i == 0) { 
            row1.setBACkgroundColor(Color.LTGRAY);
            tableHeader.addView(row1,new TableLayout.LayoutParams());
        } else {
            tableContent.addView(row1,new TableLayout.LayoutParams());
        }
    }
}@H_772_12@ 
 

上面的代码完美地工作(),但是,当我使用AnysnTask从服务器获取数据并稍后将数据添加到表时,我的自定义视图中的onLayout()不再起作用.我通过注销一些数字模拟获取数据:

public void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.main );

        new myTask().execute();
    }

    private class myTask extends AsyncTask<Void,Void,Void> {

        private ProgressDialog progressDialog;
        protected void onPreExecute() {
                progressDialog = ProgressDialog.show(ScrollingTableActivity.this,"","Loading. Please wait...",truE);
        }
        @Override
        protected Void doInBACkground(Void... reportTypes) {
            for (int i = 0; i < 500; i++) { 
                System.out.println(i);
            } 
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            progressDialog.dismiss();
            TableLayout tableHeader = (TableLayout)findViewById(R.id.HeaderTablE);
            TableLayout tableBody = (TableLayout)findViewById(R.id.bodyTablE);

            appendRows(tableHeader,tableData);
        }

    }@H_772_12@ 
 

所以onLayout()只有在我通过将它放在onCreate()方法中从主UI线程调用appendRows()时才有效.如果我从另一个UI线程调用(在AsyncTask的onPostExecute()中),则调用onLayout()(我通过创建一些日志来检查它),但它不会影响GUI.我尝试使用invalidate(),forceLayout(),requestLayout()但不改变任何东西.

我想我们需要调用一个方法来进行GUI刷新,但不知道它是什么,我已经在2天内搜索并尝试了很多方法但没有得到任何结果,所以如果你能给出任何想法我将非常感激对这个.非常感谢.

解决方法

你可能想看看这个答案:
Android Set textview layout width dynamically

但是,基本上,尝试将每个TextView的宽度设置为与标题相同.

可能需要你做两次,因为你可能需要让系统进行布局,所以使用View.INVISIBLE,然后你需要退出AsyncTask,调用一个,这样就可以进行布局工作.

然后在第二个中,您可以获取不可见的表,循环查找该列中的最大宽度,然后将该列中的所有TextView设置为最大.

这不是最好的解决方案,但应该有效.

我认为您在AsyncTask中的主要问题是需要完成布局,然后您可以进行修复.

大佬总结

以上是大佬教程为你收集整理的Android onLayout()和AsyncTask()无法一起使用全部内容,希望文章能够帮你解决Android onLayout()和AsyncTask()无法一起使用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。