Android listView在错误的位置添加动态视图

时间:2013-11-19 13:58:23

标签: android android-listview android-custom-view

我为listView提供了自定义适配器扩展BaseAdapter。列表视图有一个LinearLayout,我试图根据特定条件动态添加视图。现在的问题是,它在错误的行中添加了视图,有时两个视图同时添加到一行。这是listview适配器布局的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="100dip"
        android:baselineAligned="false"
        android:orientation="horizontal" >

            <LinearLayout
                android:id="@+id/dynamic_layout"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.3"
                android:orientation="horizontal" >
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.7"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/groupName_tv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Group1"
                    android:textColor="@color/white"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/groupContacts_tv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="a, b , c"
                    android:textColor="@color/white"
                    android:textSize="16sp" />
            </LinearLayout>

    </LinearLayout>

在我的适配器中,我试图动态地向@id dynamic_layout添加视图。这是我的适配器的getView方法:

    @Override
    public View getView(final int position, View converView, ViewGroup arg2) {
        // TODO Auto-generated method stub
        View view;
        TextView group_name;
        LinearLayout dynamic_layout;
        final TextView contacts_names;
        ViewHolder viewHolder;


        // Log.e("size in adapter ", getCount() + " size");
        if (converView == null) {
            converView = this.inflater.inflate(resource, arg2, false);

            group_name = (TextView) converView.findViewById(R.id.groupName);
            dynamic_layout = (LinearLayout) converView.findViewById(R.id.dynamic_layout);
            contacts_names = (TextView) converView
                    .findViewById(R.id.groupContacts);
            viewHolder = new ViewHolder(group_name, dynamic_layout, contacts_names);
            converView.setTag(viewHolder);

        } else {
            //view = converView;

            viewHolder = (ViewHolder) converView.getTag();

            group_name = viewHolder.group_name;
            dynamic_layout = viewHolder.layout;
            contacts_names = viewHolder.contacts_names;

        }

        /*
         *  bind the data to the view object
         */
        group_name.setText(g_names.get(position));

        LayoutInflater vi = (LayoutInflater) c.getApplicationContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (group_map.get(g_names.get(position)).size() == 1) {
            View v = vi.inflate(R.layout.one_item_view, null);

            TextView textView = (TextView) v.findViewById(R.id.group_tv);
            textView.setText("one Item");

            ((LinearLayout) dynamic_layout).addView(v);


        }else if (group_map.get(g_names.get(position)).size() == 2) {
            View v = vi.inflate(R.layout.two_items_view, null);

            TextView textView = (TextView) v
                    .findViewById(R.id.two_group_tv1);
            textView.setText("left Item ");

            TextView textView2 = (TextView) v
                    .findViewById(R.id.two_group_tv2);
            textView2.setText("right Item ");

            ((LinearLayout) dynamic_layout).addView(v);


        }

        return converView;
    }

}  

1 个答案:

答案 0 :(得分:1)

listview中的行可见时,系统会调用getView。第一次converView将是null。您需要创建一个新视图并将其返回。执行此操作后,下次getView将使用您返回的上一个视图进行调用。由于您的行没有任何更改,因此可以安全地返回相同的视图。

在原始代码中,if (converView == null) {...}内没有任何内容,因此您不必每次都使用((LinearLayout) dynamic_layout).addView(v);创建新视图并添加它们。

@Override
public View getView(final int position, View converView, ViewGroup arg2) {

    if (converView == null) {
        converView = this.inflater.inflate(resource, arg2, false);

        TextView group_name = (TextView) converView.findViewById(R.id.groupName);
        LinearLayout dynamic_layout = (LinearLayout) converView.findViewById(R.id.dynamic_layout);
        TextView contacts_names = (TextView) converView.findViewById(R.id.groupContacts);
        ViewHolder viewHolder = new ViewHolder(group_name, dynamic_layout, contacts_names);
        converView.setTag(viewHolder);

        // bind the data to the view object
        group_name.setText(g_names.get(position));

        LayoutInflater vi = (LayoutInflater) c.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (group_map.get(g_names.get(position)).size() == 1) {
            View v = vi.inflate(R.layout.one_item_view, null);

            TextView textView = (TextView) v.findViewById(R.id.group_tv);
            textView.setText("one Item");

            ((LinearLayout) dynamic_layout).addView(v);

        } else if (group_map.get(g_names.get(position)).size() == 2) {
            View v = vi.inflate(R.layout.two_items_view, null);

            TextView textView = (TextView) v.findViewById(R.id.two_group_tv1);
            textView.setText("left Item ");

            TextView textView2 = (TextView) v.findViewById(R.id.two_group_tv2);
            textView2.setText("right Item ");

            ((LinearLayout) dynamic_layout).addView(v);
        }
    }
    return converView;
}