问题
I have an ExpandableListView with three levels. In the adapter for the first level, I have the getGroupView which renders a TextView, and it works fine.
@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;
}
But now I wanted to take it to the next level.. just inflating an XML and have instead of just a TextView, a Button too; nevertheless, it doesn't work then, since it doesn't expand when I click on a row. This is the adapted method, and of course, in the constructor, I initialize the 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);
Can anybody please throw some light on why it doesn't work, and how to fix it? It's really strange because it's just changing a TextView for a View..
Thanks a lot in advance!
回答1:
I do it in this way:
In the activity class:
public void expandGroup(int groupPosition)
{
if(expandableListView.isGroupExpanded(groupPosition))
expandableListView.collapseGroup(groupPosition);
else
expandableListView.expandGroup(groupPosition);
}
In the class extends 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;
}
By this way you can expand the group item while has a button on the group item to do something else. There must be a better way to do this. But I don't have time to study on it.
来源:https://stackoverflow.com/questions/11205052/button-in-a-row-in-expandablelistview