如何创建不在ScrollView中的ListView,或禁用ScrollView?

syn*_*nic 2 android listview scrollview

我想要ListView中的一些好东西,比如能够使用ListAdapter和项目选择等,但我不想要它的ScrollView部分.我想以不同的方式自己实现该部分(为什么或如何做到这一点并不是这个问题的重点,所以请不要问"为什么").

有没有办法让ListView不在ScrollView中或者滚动禁用?

Nei*_*aft 6

另一种方法是将ListView您已经制作的任何自定义滚动解决方案包装起来.如果您只是将高度设置ListView为与所有行完全一样高(使用列表视图LayoutParams),那么它将像普通视图一样无法滚动; 因此,您可以将其添加到另一个滚动视图.这种方法的难点在于您必须提前知道列表的高度,因此您必须知道每行的高度.此外,这将同时创建所有行,因此您将无法利用视图回收功能.

如果你没有提前计算高度的简单方法,你可以ListView通过覆盖onMeasure并给出你自己的身高规格来欺骗你做这件事:

// Calculate height of the entire list by providing a very large 
// height hint.  But do not use the highest 2 bits of this integer; 
//  those are reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
        MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
Run Code Online (Sandbox Code Playgroud)