我有一个ListView,我需要打开始终启用的FastScroll的可见性。问题是,当列表项目只有几个(例如只有2或3个)并且很容易安装在屏幕上时,显然不能滚动。但是FastScroll仍然在屏幕上,即可见的。当列表项小于可滚动项时,如何禁用它或隐藏它。

发布于 2014-07-02 06:15:10
您可以通过setFastScrollEnabled(boolean)方法以编程方式启用/禁用快速滚动功能。
因此,只需检查列表中有多少条目,并相应地启用/禁用快速滚动即可。
发布于 2014-10-16 20:47:11
不要听“里奇科利”。默认行为很少是最优的,这并不难做到。下面的方法确实要求您知道项的高度。这也使您的活动实现了OnPreDrawListener。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView = (ListView) findViewById(R.id.list);
itemViewHeight = getResources().getDimensionPixelSize(R.dimen.item_height);
adapter = new YourAdapterClass();
listview.setAdapter(adapter);
ViewTreeObserver vto = list.getViewTreeObserver();
if (vto != null && list.getMeasuredHeight() == 0) {
vto.addOnPreDrawListener(this);
} else if (list.getMeasuredHeight() != 0) {
listViewHeight = list.getMeasuredHeight();
}
}
public void setData(Object data) {
// Set your adapter data how ever you do.
adapter.setData(data);
handleFastScrollVisibility();
}
private void handleFastScrollVisibility() {
if (listViewHeight == 0 || list == null) return;
int itemCount = adapter.getCount();
int totalItemHeight = itemCount * itemViewHeight;
list.setFastScrollAlwaysVisible(totalItemHeight > listViewHeight);
}
@Override
public boolean onPreDraw() {
ViewTreeObserver vto = list.getViewTreeObserver();
if (vto != null) vto.removeOnPreDrawListener(this);
listViewHeight = list.getMeasuredHeight();
handleFastScrollVisibility();
return true;
}简单地说,你不知道ListView的高度什么时候会准备好。这就是为什么您添加了一个预绘监听器,当它准备好时,它会通知您。我不知道您是如何获得数据的,但是这个方法假设您不知道您的ListView高度或数据将首先准备好。如何向适配器添加数据将取决于适配器。
https://stackoverflow.com/questions/24524037
复制相似问题