Button in a row in ExpandableListView

I have an ExpandableListView with three levels. In the adapter for the first level, I have a getGroupView that displays 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 go to the next level .. just bloating XML and instead of TextView, Button too; however, it does not work then, since it does not expand when I click on a line. This is an adapted method and, of course, in the constructor, I initialize the inflation:

 @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 anyone talk about why it does not work, and how to fix it? This is really strange because it is just a change to the TextView to view.

Many thanks!

0
source share
1 answer

I do it as follows:

In the activity class:

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

In the extends BaseExpandableListAdapter class:

 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; } 

Thus, you can expand the group element, and the button in the group element can do something else. There must be a better way to do this. But I don’t have time to learn from this.

0
source

All Articles