我有自定义适配器扩展 ListView 的 BaseAdapter。 ListView 有一个 LinearLayout,我正在尝试根据特定条件动态添加 View 。现在的问题是,它在错误的行中添加了 View ,有时两个 View 同时添加到一行中。这是 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>
在我的适配器中,我试图将 View 动态添加到@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;
}
}
最佳答案
当 listview
中的行变得可见时,系统将调用 getView
。第一次 converView
将为 null
。您需要创建一个新 View 并将其返回。执行此操作后,下一次 getView
将使用您返回的上一个 View 进行调用。因为你的行没有变化,所以返回相同的 View 是安全的。
在您的原始代码中,if (converView == null) {...}
中没有所有内容,因此您不必要地创建新 View 并使用 ( (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;
}
关于Android listView 在错误的地方添加动态 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20073497/