我写了一个应用程序,它的布局中包含一个支持快速滚动的ListView (即,拖动滚动条滑块可以让你快速向上或向下滚动列表)。这在所有版本的Jelly Bean (4.1-4.3)中都能完美地工作。然而,在我更新到Kit Kat后,快速滚动不再起作用,我的滚动条只是一个普通的滚动条。但是,如果我切换出我的应用程序并返回,则会出现快速滚动。如何让快速滚动在Kit Kat中持续工作?我已经复制并粘贴了下面的列表视图适配器代码:
// Adds section popup for fast scroll
class NameListAdapter extends SimpleAdapter implements SectionIndexer {
HashMap<String, Integer> alphaIndexer;
private String[] sections;
private ArrayList<String> sectionList;
private List<HashMap<String, String>> data = null;
public NameListAdapter (Context context, List<HashMap<String, String>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
alphaIndexer = new HashMap<String, Integer>();
sectionList = new ArrayList<String>();
this.data = data;
int size = data.size();
for (int i = 0; i < size; i++) {
HashMap<String, String> myData = (HashMap <String, String>) data.get(i);
// Get first character
String ch = myData.get("name").substring(0, 1);
// Convert character to upper case
ch = ch.toUpperCase();
// Put first char/index into our HashMap
if (!alphaIndexer.containsKey(ch)) {
alphaIndexer.put(ch, i);
sectionList.add(ch);
}
}
sections = new String[sectionList.size()];
sectionList.toArray(sections);
}
@Override
public int getPositionForSection(int section) {
if (section >= sections.length) {
return getCount() - 1;
}
return alphaIndexer.get(sections[section]);
}
@Override
public int getSectionForPosition(int pos) {
if (pos > data.size()) {
return 0;
}
HashMap<String, String> myData = (HashMap <String, String>) data.get(pos);
String ch = myData.get("name").substring(0, 1);
// Convert character to upper case
ch = ch.toUpperCase();
for (int i = 0; i < sectionList.size(); i++) {
if (sectionList.get(i).equals(ch)) {
return i;
}
}
return 0;
}
@Override
public Object[] getSections() {
return sections;
}
}
发布于 2013-12-11 17:36:52
在这里有相同的问题,这不是一个好的解决方案,但现在你可以这样做:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
view.setFastScrollAlwaysVisible(true);
}
这会让你的快速滚动条一直开着。
发布于 2014-01-29 19:30:01
这是一个略微改进的变通方法,类似于mikejonesguy的答案
基本上,当列表滚动不空闲时,您可以设置快速滚动始终可见。当列表停止滚动时,您启动一个计时器,并在一段时间后关闭快速滚动。这复制了它在旧版android上的工作方式。
希望能有所帮助
//fix for kitkat bug in fast scroll
Runnable delayedFastScrollFixRunnable = new Runnable()
{
@Override
public void run()
{
if (listView != null && fastScrollAlwaysVisible)
{
listView.setFastScrollAlwaysVisible(false);
fastScrollAlwaysVisible = false;
}
}
};
Handler delayedFastScrollFixHandler = new Handler();
boolean fastScrollAlwaysVisible = false;
mList.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState)
{
//fix an issue with 4.4 not showing fast scroll
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
if (scrollState == SCROLL_STATE_IDLE)
{
if (fastScrollAlwaysVisible)
{
delayedFastScrollFixHandler.postDelayed(delayedFastScrollFixRunnable, 750);
}
}
else
{
if (!fastScrollAlwaysVisible)
{
delayedFastScrollFixHandler.removeCallbacks(delayedFastScrollFixRunnable);
listView.setFastScrollAlwaysVisible(true);
fastScrollAlwaysVisible = true;
}
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
}
});
https://stackoverflow.com/questions/20448371
复制相似问题