我通过添加一些额外的视图修改了这个tutorial,我现在要做的是将图像视图的可见性设置为仅在最后一个组中不可见,
组布局是这个
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp" >
<TextView
android:id="@+id/laptop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:paddingLeft="25dp" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
android:text=" " />
<ImageView
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/checkBox1"
android:layout_toLeftOf="@+id/checkBox1"
android:contentDescription="@string/app_name"
android:src="@android:drawable/ic_delete" />
</RelativeLayout>
和我试图隐藏视图的代码是这个
public View getGroupView(final int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String laptopName = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_item, null);
}
TextView item = (TextView) convertView.findViewById(R.id.laptop);
item.setTypeface(null, Typeface.BOLD);
item.setText(laptopName);
ImageView delete = (ImageView) convertView.findViewById(R.id.delete);
if (groupPosition == getGroupCount()-1 ) {
delete.setVisibility(View.INVISIBLE);
}
delete.setOnClickListener(new OnClickListener() {
int gpos = groupPosition;
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(
context);
builder.setMessage("Do you want to remove?");
builder.setCancelable(false);
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
return convertView;
}
只有在滚动列表后才会出现问题,它看起来与回收视图有关,我不知道应该如何处理它,你能帮我解决这个问题吗?
答案 0 :(得分:1)
您是正确的,并且您的观点已被回收,这就是为什么每次返回视图时都必须将视图显式设置为VISIBLE
。尝试这样的事情。
if (groupPosition == getGroupCount()-1 ) {
delete.setVisibility(View.GONE);
}else {
delete.setVisibility(View.VISIBLE);
}