可扩展列表视图使用ViewHolder时隐藏项目

时间:2014-12-21 06:41:43

标签: android expandablelistview show-hide android-viewholder

我正在编写一个应用程序,我需要隐藏可扩展列表视图的元素。

这就是我要做的事情。

public View getGroupView(int groupPosition, boolean isLastChild, View view,
            ViewGroup parent) {

        CardHeaderInfo headerInfo = (CardHeaderInfo) getGroup(groupPosition);
        if (view == null) {
            LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inf.inflate(R.layout.card_group_view, null);
        }

        if(groupPosition < validChild) {
            TextView heading = (TextView) view.findViewById(R.id.heading);
            heading.setText(headerInfo.getName().trim());
        } else {
            view.setVisibility(View.INVISIBLE);
        }

        return view;
    }

为了优化这里的工作,我使用了ViewHolder概念,但如果开始滚动列表,那么元素就会变得混乱甚至一些变量被hide视图替换。

使用ViewHolder时有什么办法摆脱这个问题吗?

1 个答案:

答案 0 :(得分:0)

我通过向可见已隐藏元素添加另一行来获得此结果:

if(groupPosition < validChild) {
            view.setVisibility(View.VISIBLE); ****
            TextView heading = (TextView) view.findViewById(R.id.heading);
            heading.setText(headerInfo.getName().trim());
        } else {
            view.setVisibility(View.INVISIBLE);
        }