2014-07-02 48 views
1

我有一个ListView,我需要将FastScroll的可见性始终启用。问题是,当列表项目只有少数(比如只有2或3),并且很容易在屏幕上显示时,显然它们不能滚动。但FastScroll仍然在屏幕上,即可见。当列表项目少于可滚动时,我该如何禁用或隐藏它。仅当列表数据足够滚动时才显示FastScroll

enter image description here

+0

在启用快速滚动的位置显示您的代码 –

+0

M在我的listview.xml中启用fast_scroll –

回答

1

您可以启用/通过setFastScrollEnabled(boolean)方法编程方式禁用快速滚动功能。

所以只需检查您的列表有多少条目,并启用/禁用相应的快速滚动。

+0

但是我怎么能知道,我有足够的项目显示在屏幕上,无法滚动。因为设备的尺寸会改变设备的设备。 –

+0

快速滚动只应启用,如果你真的有很多项目。对于正常数量的项目(比如20或30左右),列表视图的正常滚动行为应该足够了。 – Ridcully

1

不要听@Ridcully。默认行为很少是最佳的,这并不难。以下方法要求您知道物品高度。这也有你的活动实现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高度或数据是否会首先准备就绪。如何将数据添加到适配器将取决于您的适配器。

相关问题