问题描述
我有一个ListView延伸BaseAdapter的自定义适配器。在ListView具有的LinearLayout到我试图动态地根据一定的条件下添加视图。现在的问题是,它增加了在错误的行视图和有时两个视图同时添加到一个单列。下面是列表视图适配器布局XML:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =100dip
机器人:baselineAligned =假
机器人:方向=横向> <的LinearLayout
机器人:ID =@ + ID / dynamic_layout
机器人:layout_width =0dp
机器人:layout_height =match_parent
机器人:layout_weight =0.3
机器人:方向=横向>
< / LinearLayout中> <的LinearLayout
机器人:layout_width =0dp
机器人:layout_height =match_parent
机器人:layout_weight =0.7
机器人:方向=垂直> <的TextView
机器人:ID =@ + ID / groupName_tv
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=组1
机器人:文字颜色=@色/白
机器人:TEXTSIZE =18sp/> <的TextView
机器人:ID =@ + ID / groupContacts_tv
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=A,B,C
机器人:文字颜色=@色/白
机器人:TEXTSIZE =16SP/>
< / LinearLayout中> < / LinearLayout中>
在我的适配器我试图动态地添加欣赏到@id dynamic_layout。这里是我的适配器的getView方法:
@覆盖
公共查看getView(最终诠释的立场,观点converView,ViewGroup中ARG2){
// TODO自动生成方法存根
查看视图。
TextView的组名;
的LinearLayout dynamic_layout;
最终的TextView contacts_names;
ViewHolder viewHolder;
// Log.e(大小适配器,getCount将()+大小);
如果(converView == NULL){
converView = this.inflater.inflate(资源,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 =新ViewHolder(组名,dynamic_layout,contacts_names);
converView.setTag(viewHolder); }其他{
//视图= converView; viewHolder =(ViewHolder)converView.getTag(); GROUP_NAME = viewHolder.group_name;
dynamic_layout = viewHolder.layout;
contacts_names = viewHolder.contacts_names; } / *
*将数据绑定到视图对象
* /
group_name.setText(g_names.get(位置)); LayoutInflater VI =(LayoutInflater)c.getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 如果(group_map.get(g_names.get(位置))。大小()== 1){
视图V = vi.inflate(R.layout.one_item_view,NULL); TextView中的TextView =(TextView中)v.findViewById(R.id.group_tv);
textView.setText(一项目); ((的LinearLayout)dynamic_layout).addView(V);
}否则如果(group_map.get(g_names.get(位置))。大小()== 2){
视图V = vi.inflate(R.layout.two_items_view,NULL); TextView中的TextView =(TextView的)V
.findViewById(R.id.two_group_tv1);
textView.setText(左项目); TextView的textView2 =(TextView的)V
.findViewById(R.id.two_group_tv2);
textView2.setText(右边的); ((的LinearLayout)dynamic_layout).addView(V);
} 返回converView;
}}
从您的的ListView行
变得可见,系统会调用 getView
。首次 converView
将空
。您需要创建一个新的视图,然后返回。一旦你这样做,下一次 getView
将与你返回previous视图调用。因为你有你的行没有改变,它是安全返回相同的看法了。
在原来的code,你没有在一切,如果(converView == NULL){...}
,所以你unnecessarly创建新视图并使用它们添加((LinearLayout中)dynamic_layout).addView(v);
每次
@覆盖
公共查看getView(最终诠释的立场,观点converView,ViewGroup中ARG2){ 如果(converView == NULL){
converView = this.inflater.inflate(资源,ARG2,FALSE); TextView的组名=(的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 =新ViewHolder(组名,dynamic_layout,contacts_names);
converView.setTag(viewHolder); //将数据绑定到视图对象
group_name.setText(g_names.get(位置)); LayoutInflater VI =(LayoutInflater)c.getApplicationContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。 如果(group_map.get(g_names.get(位置))。大小()== 1){
视图V = vi.inflate(R.layout.one_item_view,NULL); TextView中的TextView =(TextView中)v.findViewById(R.id.group_tv);
textView.setText(一项目); ((的LinearLayout)dynamic_layout).addView(V); }否则如果(group_map.get(g_names.get(位置))。大小()== 2){
视图V = vi.inflate(R.layout.two_items_view,NULL); TextView中的TextView =(TextView中)v.findViewById(R.id.two_group_tv1);
textView.setText(左项目); TextView的textView2 =(TextView中)v.findViewById(R.id.two_group_tv2);
textView2.setText(右边的); ((的LinearLayout)dynamic_layout).addView(V);
}
}
返回converView;
}
I have custom adapter extending BaseAdapter for a listView. The listview has a LinearLayout to which i am trying to add views dynamically based on a certain condition. Now problem is that, it adds views in wrong rows and some times two views are added at the same time to a single row. Here is the xml for listview adapter layout:
<?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>
In my adapter i am trying to add views dynamically to the @id dynamic_layout. Here is my adapter's getView method:
@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;
}
}
As rows from your listview
become visible, system will call getView
. First time converView
will be null
. You need to create a new view and return it back. Once you do that, next time getView
will be called with the previous view you returned. Because you have no changes in your rows, it is safe to return same view back.
In your original code, you don't have everything inside if (converView == null) {...}
, so you were unnecessarly creating new views and adding them using ((LinearLayout) dynamic_layout).addView(v);
each time.
@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中增加了在错误的地方动态视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!