1

我的列表视图中的项目意外地重新组织起来。这意味着当我向下滚动时会显示相同的列表项。这是非常奇怪的行为。有没有人遇到过这个?

    @Override
public View getView(int position, View view, ViewGroup parent) {

    View row = view;
    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

       ......
       //this view is on xml file (container)
       LinearLayout ll = (LinearLayout) row.findViewById(R.id.listViewControllers);


       //adding my own views to a linear layout container dynamically...
       .......
       }



    }
   return row;

我决定使用充气器在我的 getView 中充气另一个列表项视图。都在一个 for 循环中。但是,我在为我的 textview 设置文本时遇到问题。第一个之后的行不想正确设置文本。

这是getView中的代码片段

for(Controller aController : controllers) {   

          inflater.inflate(R.layout.list_view_item, ll, true);
          TextView tv2 = (TextView) row2.findViewWithTag("controller_name");
          tv2.setText(aController.getName());
       }

这是 list_view_item:

    <?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="wrap_content"
    android:background="@drawable/bg_controller_cell"
    android:orientation="vertical" >

    <TableRow
        android:id="@+id/tableRowBatchController"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="15dp"
            android:paddingTop="15dp"
            android:tag="controller_name"
android:text="name"
            android:textColor="#ffffff" />
    </TableRow>

</LinearLayout>

提前致谢。仍在寻找为什么列表项没有在“controller_name”文本视图上正确设置文本

4

1 回答 1

0

这是意料之中的,ListView重复使用离开屏幕的列表项以显示可见项。添加 'else' 分支,它不会膨胀 new View,而是用项目数据填充现有分支。

于 2013-08-21T02:33:21.687 回答