【问题标题】:Scroll to specific list item in ListView滚动到 ListView 中的特定列表项
【发布时间】:2013-06-18 05:17:14
【问题描述】:

我用 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

如何自动滚动显示孩子?

【问题讨论】:

  • @Raghunandan setSelection 使项目位于顶部。这不是我的愿望。我想要滚动可用的最小滚动。
  • 检查同一链接中的其他方法 smoothScrollToPosition 或 smoothScrollByOffset 取决于您的要求

标签: android android-listview expandablelistview


【解决方案1】:

我做到了。

如果只使用 smoothScrollToPosition() => 它只显示组视图而不是子视图。

所以我在 ListView 中添加了 OnLayoutChangeListener。像这样。

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;
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多