BaseExpandableListAdapter getChildView检查布尔条件

时间:2014-08-01 12:50:50

标签: android

我正在制作一个购物清单,并使用ExpandableListview实现它并且它有一些带有子项的组项目,我创建了删除组和子项的功能,并且在每个组中都有一个最后一个子项即“添加项目”

现在问题出现在UI中,在添加项目中我隐藏了删除按钮,因为它没有意义。

隐藏是通过将一个字段传递给最终填充我的列表视图的arraylist来完成的,它通过在条件中检查并相应地分配状态工作正常但是当我滚动时隐藏的删除按钮变得可见,反之亦然。

我尝试过很多修复但没有运气,比如使用Viewholder实现。

请有人帮助我,我真的需要一个快速的解决方案。

如果需要任何其他信息,请问我,请给我答案。

感谢

编辑:抱歉,但我不能分享部分代码,其余的就是这个

public class MyListAdapter extends BaseExpandableListAdapter {

private Context context;
private ArrayList<HeaderInfo> deptList;
ShoppingListFragment ShoppingListFrag;
ArrayList<HashMap<View, Boolean>> alValues;

DatabaseHelper db;

public MyListAdapter(Context context, ArrayList<HeaderInfo> deptList,
        ShoppingListFragment ownerFragment) {
    this.context = context;
    this.deptList = deptList;
    db = new DatabaseHelper(context);
    this.ShoppingListFrag = ownerFragment;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    ArrayList<DetailInfo> productList = deptList.get(groupPosition)
            .getProductList();
    return productList.get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View view, ViewGroup parent) {
    // View vi = view;
    ViewHolderChild holder = null;
    DetailInfo detailInfo = (DetailInfo) getChild(groupPosition,
            childPosition);

    Boolean bFlag = ((DetailInfo) getChild(groupPosition, childPosition))
            .getLastItem();

    if (view == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = infalInflater.inflate(R.layout.child_row, null);
        holder = new ViewHolderChild();

        holder.childItem = (TextView) view.findViewById(R.id.childItem);
        holder.tvRemover = (TextView) view.findViewById(R.id.tv_removeItem);


        view.setTag(holder);
        view.setTag(R.id.childItem, detailInfo.getLastItem());
        view.setTag(R.id.heading, detailInfo.getTagId());


    } else {

        holder = (ViewHolderChild) view.getTag();

        view.getTag(R.id.childItem);
        view.getTag(R.id.heading);

    }

    holder.childItem.setText(detailInfo.getName().trim());

    Log.e("MyListAdapter, GetChildView",
            "Group Pos: "
                    + groupPosition
                    + ", Child Pos: "
                    + childPosition
                    + ", Last Item Pos: "
                    + ((DetailInfo) getChild(groupPosition, childPosition))
                            .getLastItem() + ", bflag: " + bFlag
                    + ", view: " + view);


    if (detailInfo.getLastItem()) {
        holder.tvRemover.setVisibility(View.GONE);
    }


    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            // Checking if it is last item
            if (Boolean.parseBoolean(v.getTag(R.id.childItem).toString())) {
                //adding child to the corresponding group
            } else {
                Toast.makeText(context, " item", Toast.LENGTH_SHORT).show();
            }

        }
    });

    holder.tvRemover.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //Removing child from the group
        }
    });

    return view;
}

@Override
public int getChildrenCount(int groupPosition) {

    ArrayList<DetailInfo> productList = deptList.get(groupPosition)
            .getProductList();
    return productList.size();

}

@Override
public Object getGroup(int groupPosition) {
    return deptList.get(groupPosition);
}

@Override
public int getGroupCount() {
    return deptList.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isLastChild, View view,
        ViewGroup parent) {
    ViewHolder holder = null;
    HeaderInfo headerInfo = (HeaderInfo) getGroup(groupPosition);
    if (view == null) {
        LayoutInflater inf = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inf.inflate(R.layout.group_heading, null);
        holder = new ViewHolder();

        holder.heading = (TextView) view.findViewById(R.id.heading);
        holder.imgdelete = (ImageView) view.findViewById(R.id.img_delete);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
    LibFunctions.setTypeFace(context, holder.heading,
            "AvantGardeMedium.OTF");
    holder.heading.setText(headerInfo.getName().trim());

    holder.imgdelete.setTag(headerInfo);
    Log.e("MyListActivity", "groupPosition: " + groupPosition);

    holder.imgdelete.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //Deleting group here

        }
    });

    return view;
}

static class ViewHolder {
    TextView heading;
    ImageView imgdelete;

}

static class ViewHolderChild {
    TextView tvRemover, childItem;
}

@Override
public boolean hasStableIds() {
    return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

}

请帮忙

2 个答案:

答案 0 :(得分:0)

由于您有两种不同的视图类型,因此您不应仅使用一个视图并对其进行修改。

相反,您应该使用两个不同的视图,并实现getViewTypeCount()getItemViewType(int position)来显示相应的视图。

This Thread应该帮助你

答案 1 :(得分:0)

我用现有代码解决了它,只是改变了条件检查,这是一个愚蠢的错误。

但我仍然有点困惑,为什么其他位置的视图被视为被视为可见或不可见的视图。

无论如何,这是我实施的代码的变化:

final Boolean bFlag = ((DetailInfo) getChild(groupPosition,
                    childPosition)).getLastItem();
.
.
.

if (bFlag) {
            holder.tvRemover.setVisibility(View.GONE);
        } else {
            holder.tvRemover.setVisibility(View.VISIBLE);
        }

所以,错误是在查看条件后视图已经消失但是没有将其恢复到可见的状态。

谢谢大家。