在ExpandableListView中连续按钮

时间:2012-06-26 10:10:25

标签: android android-listview expandablelistview expandablelistadapter

我有一个包含三个级别的ExpandableListView。 在第一级的适配器中,我有getGroupView渲染TextView,它工作正常。

@Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
             TextView tv = new TextView(context);
             tv.setText(mCountries.get(groupPosition).getName());
             tv.setBackgroundColor(Color.BLUE);
             tv.setPadding(10, 7, 7, 7);
             return tv;
        }

但是现在我想把它提升到一个新的水平..只是膨胀一个XML而不仅仅是一个TextView,一个Button;尽管如此,它不起作用,因为当我点击一行时它不会扩展。 这是改编的方法,当然,在构造函数中,我初始化了inflater:

@Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View v = mInflater.inflate(R.layout.countries_row, null, false);
        mCountryName = (TextView) v.findViewById(R.id.CountryTextView);
        mCountryName.setText(mCountries.get(groupPosition).getName());
        mCountryName.setBackgroundColor(Color.BLUE);
        mCountryName.setPadding(10, 7, 7, 7);
        mCountryExpandButton = (Button) v.findViewById(R.id.CountryExpandButton);
        return v;
    }

mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

有人可以说一下为什么它不起作用,以及如何解决它? 这真的很奇怪,因为它只是改变了View的TextView ..

提前多多感谢!

1 个答案:

答案 0 :(得分:0)

我是这样做的:

在活动类中:

public void expandGroup(int groupPosition)
{
    if(expandableListView.isGroupExpanded(groupPosition))
        expandableListView.collapseGroup(groupPosition);
    else
        expandableListView.expandGroup(groupPosition);
}

在类扩展BaseExpandableListAdapter:

public View getGroupView(final int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {

    LayoutInflater inflater=LayoutInflater.from(acti);
    View rtnview=inflater.inflate(R.layout.XXX, null);
    TextView text=(TextView)rtnview.findViewById(R.id.tvinfo1);
    text.setText(groupArray.get(groupPosition));
    text.setTextColor(acti.getResources().getColor(R.color.blackcolor));
    text.setTextSize(EDEnv.EEfontSize+3);
    text.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            acti.expandGroup(groupPosition);
        }
    });

    Button btn1=(Button)rtnview.findViewById(R.id.button1);
    btn1.setId(groupPosition);
    btn1.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v)
        {
            acti.showDialog(1);
        }
    });

    if (convertView == null) {
        convertView = rtnview;
    }

    return convertView;
}

通过这种方式,您可以展开组项目,同时在组项目上有一个按钮可以执行其他操作。必须有更好的方法来做到这一点。但我没有时间研究它。