1

我用 ListView(Adapter) 制作了 Fake ExandableListView(Adapter)。(因为我需要更多的定制)

一个列表项包含 GroupView 和 ChildView(默认可见性消失)

然后我改变子视图的可见性。

public void itemClicked(int position) {
    Group group = groupData.get(position);
    Child child = childData.get(group);
    View childView = child.getChildView();
    if (group.toggleExpansion()) {
        childView.setVisibility(View.VISIBLE);
    } else {
        childView.setVisibility(View.GONE);
    }
}

它工作正常,但我需要在展开列表项时自动滚动,比如真正的 ExpandableListView

如何自动滚动以显示孩子?

4

1 回答 1

1

I did it.

If just use smoothScrollToPosition() => It show only group view not child view.

So I add OnLayoutChangeListener in ListView. Like this.

private int expandedPosition;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private boolean addOnLayoutChangeListener() {

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        return false;
    }

    listView.addOnLayoutChangeListener(new OnLayoutChangeListener() {

        @Override
        public void onLayoutChange(View v, int left, int top, int right,
                int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            LogUtils.pi("onLayoutChange");
            if (expandedPosition != ListView.INVALID_POSITION) {
                listView.smoothScrollToPosition(expandedPosition);
            }
        }
    });
    return true;
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

        boolean expanded = adapter.itemClicked(position);
        if (expanded) {
            expandedPosition = position;
        } else {
            expandedPosition = ListView.INVALID_POSITION;
        }
}
于 2013-06-18T06:15:31.307 回答