Android ExpandableListView Parent with Button

I am trying to achieve something similar. An expandable list consists of the names of certain categories and when you click the parent displays a list of all the children of this category. Now suppose I want to dynamically add a child to any category? How can I do it? Do I save a button with each parent in the list to which a new child will be added under it?

But, looking around on different forums, I realized that it’s really not easy to install a button handler inside each parent. But if this is the only way, can someone give me some sample code?

I found this thread, but could not implement it in my code. Android Row becomes non-sticky with a button

+6
source share
1 answer

Adding a button to group mode doesn't have to be that complicated.

I believe the following should work (although I don't have a project using an array supported by ExpandableListView for testing).

I do not know your group line layout, so I will do it here for reference purposes.

group_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/test" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@android:id/text1" android:layout_width="wrap_content" android:layout_height="35dp" android:focusable="false" android:focusableInTouchMode="false" android:gravity="center_vertical" android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/addbutton" android:layout_width="wrap_content" android:layout_height="35dp" android:focusable="false" android:focusableInTouchMode="false" android:text="Add" android:textSize="12dp" /> </LinearLayout> 

Then in your getGroupView method from your adapter:

 public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { if (convertView == null) { View convertView = View.inflate(getApplicationContext(), R.layout.group_layout, null); Button addButton = (Button)convertView.findViewById(R.id.addButton); addButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { // your code to add to the child list } }); } TextView textView = (TextView)convertView.findViewById(R.id.text1); textView.setText(getGroup(groupPosition).toString()); return convertView; } 
+6
source

Source: https://habr.com/ru/post/917969/


All Articles