去掉expandablelistview最后一个item中的divider view

Remove the divider view in the expandablelistview last item

我目前在可扩展列表视图中为我的每个项目设置了一个分隔线

<View android:id="@+id/divider"
        android:layout_height="1dp"
        android:layout_width="match_parent"
        android:background="@drawable/your_divider"
        android:layout_alignBottom="@+id/parent_layout_id"/>

我想要做的是删除组中每个最后一项的分隔线。 所以第 1 组中的最后一项不应该有分隔线, 第 2 组中的最后一项不应也有分隔符。

目前我的代码是这样的:

public View getChildView(int posParent, int posChild, boolean isLastChild, View view, ViewGroup viewGroup) {

        final AccountBalanceItem expandedListText = (AccountBalanceItem) getChild(posParent, posChild);
        if (view == null) {
            LayoutInflater layoutInflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.card_account, null);
                    View divider = (View) view
                .findViewById(R.id.divider);
        if (isLastChild) {
            divider.setVisibility(View.GONE);
        }
     }

我目前的代码只隐藏了组 2 中的最后一项,我组 1 中的最后一项仍然有一个错误的分隔符。 第 1 组和第 2 组的最后一项都不应该有分隔符。

尝试:在 if (view == null) { } 之外找到 divider 并始终设置 Visibility

public View getChildView(int posParent, int posChild, boolean isLastChild, View view, ViewGroup viewGroup) {

    final AccountBalanceItem expandedListText = (AccountBalanceItem) getChild(posParent, posChild);
    if (view == null) {
        LayoutInflater layoutInflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.card_account, null);
    }
    View divider = (View) view.findViewById(R.id.divider);
    if (isLastChild) {
        divider.setVisibility(View.GONE);
    } else {
        divider.setVisibility(View.VISIBLE);
    }
    return view;
 }

希望对您有所帮助!