1

it possible to add Button just in One child of a expandable parent?

Like Bellow image :

enter image description here

Saeid
  • 2,261
  • 4
  • 27
  • 59
  • 1
    Yes, if the child's index is 0, set the visibility of those buttons to **visible** (normally they are **gone**). That's it – Phantômaxx Jul 26 '14 at 14:14
  • Do you mean that set button(in gone mode) for all child but just set visible for first child? – Saeid Jul 26 '14 at 16:14
  • 1
    Exactly. In your child row layout xml, set the visibility to `GONE`. In code, set the visibility to `View.VISIBLE` only for the first child (which has index **0**). `GONE` takes **no space**, so it's like the buttons aren't inside the layout. Until you change them to Visible. – Phantômaxx Jul 26 '14 at 16:19

1 Answers1

2

EDIT:

Okay, this is how you can accomplish this through an adapter:

static class ViewHolder {
    TextView textView;
    Button button1, button2;
}

@Override
public int getChildTypeCount() {
    return 2;
}

@Override
public int getChildType(int groupPosition, int childPosition) {
    if (childPosition == 0)
        return 0;
    else
        return 1;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        if (childPosition == 0) {
            // 1st view specifics here
            convertView = mInflater.inflate(R.layout.exp_list_child_2, parent, false);
            holder.button1 = (Button) convertView.findViewById(R.id.button1);
            holder.button2 = (Button) convertView.findViewById(R.id.button2);
            holder.button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext, "Some action here", Toast.LENGTH_SHORT).show();
                }
            });

        } else {
            // Other views
            convertView = mInflater.inflate(R.layout.exp_list_child, parent, false);
        }
        holder.textView = (TextView) convertView.findViewById(R.id.childItem);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.textView.setText("Use your data here for other items");

    return convertView;
}

Few things to note here:

  • The list uses convertView to re-use the views, but now you have 2 different types of views (1st view and other views). That means 2 different types of convertViews.
  • To force the list to think there are 2 types of views we Override getChildTypeCount and getChildType
  • We re-use the default ViewHolder class and cram 2 optional button views. These are set only for the 1st child's view.

The solution that @FrankN.Stein offered is also eligible. I'm not sure which one is better performance-wise (you'll have to test it yourself). Still, keep in mind that even though GONE views are invisible and don't take up any space in the layout, they are still present in the view hierarchy and the memory.

EDIT2:

When the adapter re-uses the views, it has to know which view to get. So it calls getChildType with child's position and asks: "What type is this"?

If there was only a single type(that's by default), then the adapter couldn't distinguish between them and would re-use your 1st-child-layout in places where it shouldn't be.

This answer has a nice picture which explains how the view's are re-used.

Hope this somewhat clears it up :-) Happy coding !

Community
  • 1
  • 1
Simas
  • 43,548
  • 10
  • 88
  • 116
  • Can help me How inflate button to child?.i have problem with inflating. So Tanx – Saeid Jul 26 '14 at 16:17
  • @saeid are you using a custom adapter? – Simas Jul 26 '14 at 17:36
  • yes i use this sample : http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/ – Saeid Jul 26 '14 at 21:32
  • ok - i use this : if(childPosition == 0 && groupPosition==0) { LayoutInflater infalInflater = (LayoutInflater) this._context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.ex_list_item2, null); } ... but not inflate in right child(First child) – Saeid Jul 26 '14 at 22:29
  • Sooooooo nice Work! , it was helpful and work right, but a question!!? - i use this way before Exactly like you! - but without getChildTypeCount & getChildType , for this reason, I received error always; so , can u explain me getChildType simply for Perception that have it work!? - tanx – Saeid Jul 27 '14 at 09:03
  • @saeid Updated my answer with a broader explanation :) – Simas Jul 27 '14 at 09:35
  • Hello - i have new problem with this - buttons add all first child of parents - this is a screenshot of problem : http://www.uppic.com/uploads/14084761461.png - what i to do? – Saeid Aug 19 '14 at 19:23
  • @saeid If only the first group should have a special child then change to: `if (childPosition == 0 && groupPosition == 0) { ...` – Simas Aug 19 '14 at 19:30