如何使用ArrayAdapter获取list_item中包含的每个子节点的宽度

时间:2013-09-17 07:03:38

标签: android android-layout listview android-arrayadapter

我正在创建一个ListView,它有一些静态标题,如下图所示:

enter image description here

我希望标题列(日期,状态,纬度和经度)根据其相应列的宽度调整其宽度。

我想要的是获取与适配器关联的列表项的布局,以便我可以在ListActivity中找到其子项的宽度(但似乎是不可能的)。

然后我尝试在我的自定义ArrayAdapter的getView方法中检索与列表项关联的每个TextView的宽度,如下面的代码所示:(但它返回null,我认为在重新编译textView之前它不能返回宽度。 )

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    DispatchHolder holder = null;
    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new DispatchHolder();
        holder.tvcurrentDateTime = (TextView) row
                .findViewById(R.id.tvCurrentDateTimeLog);
        holder.tvAction = (TextView) row.findViewById(R.id.tvActionLog);
        holder.tvLatitude = (TextView) row.findViewById(R.id.tvlatitudeLog);
        holder.tvLongitude = (TextView) row
                .findViewById(R.id.tvlogitudeLog);

        row.setTag(holder);
    } else {
        holder = (DispatchHolder) row.getTag();
    }
    holder.tvcurrentDateTime.setText(data.get(position).getTime());
    int i = holder.tvcurrentDateTime.getWidth()    <------- Here it is return 0
    holder.tvAction.setText(data.get(position).getAction());
    holder.tvLatitude.setText(data.get(position).getLatitude());
    holder.tvLongitude.setText(data.get(position).getLogitude());

    return row;
}

我搜索过但我找不到办法。

以下是相关代码:

我的ListActivity:

public class Activity_Log extends ListActivity {

PickupStatusDao puDao;
LogAdapter adapter;
List<LogModel> listOfDispatch;
private Object mItem;
private View childView;

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

    puDao = new PickupStatusDao(this);
    List<LogModel> lstResult = puDao.readAllLogData();
    adapter = new LogAdapter(this, R.layout.log_item_row, lstResult);

    setListAdapter(adapter);

}

}

我的list_item(R.layout.log_item_row):

<?xml version="1.0" encoding="utf-8"?>

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

    <TableRow
        android:id="@+id/tableRow2"
        style="@style/BodyRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/tvCurrentDateTimeLog"
            style="@style/BodyText"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="this time"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/tvActionLog"
            style="@style/BodyText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="this is action"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/tvlogitudeLog"
            style="@style/BodyText"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="logitude"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/tvlatitudeLog"
            style="@style/BodyText"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="latuide"
            android:textSize="20sp" />
    </TableRow>
</TableLayout>

列出活动布局(R.layout.activity_log_xml)

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

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

        <TableRow style="@style/HeaderRow" >

            <TextView
                style="@style/HeaderText"
                android:layout_width="239dp"
                android:text="Date" />

            <TextView
                style="@style/HeaderText"
                android:layout_width="156dp"
                android:text="Status" />

            <TextView
                style="@style/HeaderText"
                android:layout_width="121dp"
                android:text="Latitude" />

            <TextView
                style="@style/HeaderText"
                android:layout_width="121dp"
                android:text="Longitude" />
        </TableRow>
    </TableLayout>

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

不幸的是,这不是您可以使用AdapterView高效完成的事情。由于它们将任意长的数据源连接在一起,但在任何时候只显示一小部分数据,因此没有有效的方法来确定数据的最大宽度,而无需测量每个数据的视图。

我建议在初始化数据源以查找将要显示的文本的平均长度时,可能会做一些工作,并将每列设置为对数据有意义的某个值。或者,只为每列使用相同的值(即layout_weights或某些硬编码值的某种组合)。

答案 1 :(得分:1)

我已经通过XML android:layout_weight属性实现了这一点,它适用于所有具有不同密度的Android设备,这是更新的UI图片:

enter image description here

ListActivity的更新XML( activity_log_xml.xml )和list_item( log_item_row.xml )的更新分别如下:

<?xml version="1.0" encoding="utf-8"?>

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

    <TableRow
        style="@style/HeaderRow"
        android:weightSum="10" >

        <TextView
            android:id="@+id/dateHeaderTV"
            style="@style/HeaderText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="Date" />

        <TextView
            android:id="@+id/statusHeaderTV"
            style="@style/HeaderText"
            android:layout_width="0dp"
            android:layout_weight="4"
            android:text="Status" />

        <TextView
            android:id="@+id/latitudeHeaderTV"
            style="@style/HeaderText"
            android:layout_width="0dp"
            android:layout_weight="1.5"
            android:text="Latitude" />

        <TextView
            android:id="@+id/longitudeHeaderTV"
            style="@style/HeaderText"
            android:layout_width="0dp"
            android:layout_weight="1.5"
            android:text="Longitude" />
    </TableRow>
</TableLayout>

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >
</ListView>


<?xml version="1.0" encoding="utf-8"?>

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

    <TableRow
        android:id="@+id/tableRow2"
        style="@style/BodyRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="10" >

        <TextView
            android:id="@+id/tvCurrentDateTimeLog"
            style="@style/BodyText"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:text="this time" />

        <TextView
            android:id="@+id/tvActionLog"
            style="@style/BodyText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:text="this is action" />

        <TextView
            android:id="@+id/tvlogitudeLog"
            style="@style/BodyText"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1.5"
            android:text="logitude" />

        <TextView
            android:id="@+id/tvlatitudeLog"
            style="@style/BodyText"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1.5"
            android:text="latuide" />
    </TableRow>
</TableLayout>