用于隐藏expandablelistview子元素的按钮

时间:2016-12-18 07:05:49

标签: java android expandablelistview visibility

我的buttons孩子中有expandablelistiview,第一个按钮View.VISIBLE,另一个View.GONE。我尝试使第一个按钮更改第二个按钮可见性变得可见,但它不起作用。

我的 getchildview 方法:

@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView,
    ViewGroup parent) {

    final String childText = (String) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_item, null);
    }

    buttonOff = (Button) convertView.findViewById(R.id.button_off);

    buttonOff.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            buttonOff.setVisibility(View.GONE);
            buttonTest.setVisibility(View.VISIBLE);
        }
    });

    buttonTest = (Button) convertView.findViewById(R.id.button_test);
    buttonTest.setVisibility(View.GONE);

    buttonTest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            buttonTest.setVisibility(View.GONE);
            buttonOff.setVisibility(View.VISIBLE);
        }
    });

    TextView listChildText = (TextView) convertView.findViewById(R.id.list_item);

    listChildText.setText(childText);

    return convertView;
}

1 个答案:

答案 0 :(得分:0)

您的代码无效,因为您没有维护按钮的状态。初始化所有行的按钮状态。然后每次调用getView()方法时,首先检查状态,然后根据您可以设置哪个按钮可见。单击按钮时,您需要更新该行的状态。下面的代码可以指导您:

@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView,
                         ViewGroup parent) {

    final String childText = (String) getChild(groupPosition, childPosition);

    // get visibility status of the first button
    final boolean buttonOffVisible = getFirstButtonVisibility(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_item, null);
    }

    buttonOff = (Button) convertView.findViewById(R.id.button_off);

    buttonOff.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // set the state of the first button
            setFirstButtonVisibility(groupPosition, childPosition, false); // false denoting as GONE

            buttonOff.setVisibility(View.GONE);
            buttonTest.setVisibility(View.VISIBLE);
        }
    });

    buttonTest = (Button) convertView.findViewById(R.id.button_test);
    // buttonTest.setVisibility(View.GONE);    <-- remove this line

    buttonTest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // set the state of the first button
            setFirstButtonVisibility(groupPosition, childPosition, true); // true denoting as VISIBLE

            buttonTest.setVisibility(View.GONE);
            buttonOff.setVisibility(View.VISIBLE);
        }
    });

    // depending upon the visibility status of first button, make one of the two visible
    if(buttonOffVisible) {
        buttonOff.setVisibility(View.VISIBLE);
        buttonTest.setVisibility(View.GONE);
    }
    else {
        buttonOff.setVisibility(View.GONE);
        buttonTest.setVisibility(View.VISIBLE);
    }

    TextView listChildText = (TextView) convertView.findViewById(R.id.list_item);

    listChildText.setText(childText);

    return convertView;
}