gpt4 book ai didi

android - 滚动到 ListView 中的特定列表项

转载 作者:行者123 更新时间:2023-11-30 03:32:50 24 4
gpt4 key购买 nike

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

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

然后我更改 subview 的可见性。

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

如何自动滚动以显示 child ?

最佳答案

我做到了。

如果只使用 smoothScrollToPosition() => 它只显示组 View 而不显示 subview 。

所以我在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;
}
}

关于android - 滚动到 ListView 中的特定列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17161033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com