简体   繁体   中英

expandable listview with custom Child

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

Like Bellow image :

在此处输入图片说明

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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM