我有一个可扩展的ListView,并希望更新放置在父组中的按钮内放置在子项内的视图(ImageView或ToggleButton)可见性。 我已经成功地更新了孩子们内部其他视图的内容,但无法获得可见性。经过几个小时的挣扎,我来到这里。
这是我在Adapter类中的getChildView方法,它确实扩展了BaseExpandableListAdapter
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ViewHolderItem viewHolder;
final NavDrawerItem children = (NavDrawerItem) getChild(groupPosition,
childPosition);
if (convertView == null) {
convertView = inflater.inflate(R.layout.drawer_list_item, null);
viewHolder = new ViewHolderItem();
viewHolder.txtTitle = (TextView) convertView.findViewById(R.id.title);
viewHolder.txtCount = (TextView) convertView.findViewById(R.id.counter);
viewHolder.circleIcon = (CircleView) convertView.findViewById(R.id.icon);
viewHolder.btnDelete = (ToggleButton) convertView.findViewById(R.id.btn_nav_delete);
if (children.isDeleteBtnVisible())
viewHolder.btnDelete.setVisibility(View.VISIBLE);
else
viewHolder.btnDelete.setVisibility(View.GONE);
//viewHolder.headerImgIcon = (ImageView) parent.findViewById(R.id.iconHeader);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolderItem) convertView.getTag();
}
viewHolder.txtTitle.setText(children.getTitle());
// Set custom font
Typeface type = Typeface.createFromAsset(activity.getAssets(),TypeFaceUtility.getNavDrawerFont());
viewHolder.txtTitle.setTypeface(type);
viewHolder.circleIcon.circleFillColor = children.getIconColor();
if (children.getCount() > 0){
viewHolder.txtCount.setVisibility(View.VISIBLE);
viewHolder.txtCount.setText("" + children.getCount());
}else
viewHolder.txtCount.setVisibility(View.GONE);
return convertView;
}
GetGroupView方法
@Override
public View getGroupView(final int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ViewHolderHeader viewHolder;
final NavGroup group = (NavGroup) getGroup(groupPosition);
final NavDrawerItem groupItem = group.navDrawerItem;
if (convertView == null) {
convertView = inflater.inflate(R.layout.drawer_list_header, null);
viewHolder = new ViewHolderHeader();
viewHolder.headerImgIcon = (ImageView) convertView.findViewById(R.id.iconHeader);
viewHolder.txtTitle = (TextView) convertView
.findViewById(R.id.title);
viewHolder.txtCount = (TextView) convertView.findViewById(R.id.counter);
viewHolder.iconExpand = (ImageView) convertView
.findViewById(R.id.icon_expand);
viewHolder.iconCollapse = (ImageView) convertView
.findViewById(R.id.icon_collapse);
viewHolder.btnNavSetting = (ToggleButton) convertView.findViewById(R.id.btn_nav_setting);
viewHolder.btnNavSetting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("NavAdapter",groupItem.getTitle());
enableDelete(groupPosition, true);
}
});
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolderHeader) convertView.getTag();
}
从组视图中调用的enableDelete方法
private void enableDelete(int groupPosition, boolean enabled) {
NavGroup group = (NavGroup) getGroup(groupPosition);
for (int i = 0; i < group.children.size(); i++) {
group.children.get(i).setDeleteBtnVisible(enabled);
}
notifyDataSetChanged();
}
儿童布局XML
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="@id/icon"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="@drawable/nav_drawer_listitem_textcolor_selector"
android:gravity="center_vertical"
android:paddingRight="40dp"/>
<ToggleButton
android:id="@+id/btn_nav_delete"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/title"
android:contentDescription="@string/desc_list_item_icon"
android:background="@drawable/ic_action_delete"
android:textOff=""
android:textOn=""
android:visibility="visible"/>
<TextView
android:id="@+id/counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:textColor="@drawable/nav_drawer_listitem_textcolor_selector" />
当用户触摸组中的btn_nav_setting时,我想要做什么,我想让子视图中的btn_nav_delete可见,并在组折叠时隐藏btn_nav_delete。 任何帮助将受到高度赞赏。