You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android ExpandListView技术求助:仅在末尾显示EditText

Solution: Show EditText Only at the End of ExpandableListView

Got it, let's fix this so your EditText only shows up at the very end of your ExpandableListView. Here's a step-by-step solution that works perfectly:

First, tweak your child layout to hide the EditText by default—this way we only show it when we need to:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/myTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/toggle"/>

    <ToggleButton
        android:id="@+id/toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="Yes"
        android:textOff="No"
        android:layout_alignParentRight="true"/>

    <EditText
        android:id="@+id/myEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"/> <!-- Hide by default -->
</RelativeLayout>

Next, you'll need a custom ExpandableListAdapter where we control the visibility of the EditText based on the item's position. The key is checking if the current child item is the last one in the last group of your list:

public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private List<String> mGroupItems;
    private Map<String, List<String>> mChildItems;

    public CustomExpandableListAdapter(Context context, List<String> groupItems, Map<String, List<String>> childItems) {
        this.mContext = context;
        this.mGroupItems = groupItems;
        this.mChildItems = childItems;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        // Reuse views with ViewHolder pattern for efficiency
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.your_child_layout_name, parent, false);
            
            viewHolder = new ViewHolder();
            viewHolder.textView = convertView.findViewById(R.id.myTextView);
            viewHolder.toggleBtn = convertView.findViewById(R.id.toggle);
            viewHolder.editText = convertView.findViewById(R.id.myEditText);
            
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        // Populate your text and toggle button as usual
        String childText = mChildItems.get(mGroupItems.get(groupPosition)).get(childPosition);
        viewHolder.textView.setText(childText);

        // Check if this is the LAST child in the LAST group of the entire list
        boolean isLastGroup = (groupPosition == mGroupItems.size() - 1);
        boolean isFinalChild = isLastGroup && (childPosition == getChildrenCount(groupPosition) - 1);

        // Show EditText only if it's the final child, hide otherwise
        viewHolder.editText.setVisibility(isFinalChild ? View.VISIBLE : View.GONE);

        return convertView;
    }

    // --- Required overrides for ExpandableListAdapter ---
    @Override
    public int getGroupCount() {
        return mGroupItems.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mChildItems.get(mGroupItems.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mGroupItems.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mChildItems.get(mGroupItems.get(groupPosition)).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        // Implement your group view layout here (e.g., a simple TextView)
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.your_group_layout_name, parent, false);
        }
        TextView groupTitle = convertView.findViewById(R.id.group_title);
        groupTitle.setText(mGroupItems.get(groupPosition));
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    // ViewHolder to optimize view recycling
    private static class ViewHolder {
        TextView textView;
        ToggleButton toggleBtn;
        EditText editText;
    }
}

Quick Note:

If you meant you want the EditText to show at the end of each group (instead of the entire list), just replace the visibility check with:

viewHolder.editText.setVisibility(isLastChild ? View.VISIBLE : View.GONE);

That will make the EditText appear only as the last child of every expanded group.

内容的提问来源于stack exchange,提问作者CodePlorer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:26:21