我正在尝试制作这样的布局:
问题是我不希望 ListViews
可滚动。 我希望它们尽可能高,并使整个屏幕可滚动。如果我将 ListView
的高度设置为 wrap_content
,这是行不通的。让它工作的唯一方法是设置一个特定的高度 - 但我不知道 ListView
中有多少项目。
我知道我不应该将 ListView
放入 ScrollView
- 但我不想要 ListView
可滚动,只是为了显示所有项目。
也许有更好的方法让它发挥作用?
最佳答案
您创建不可滚动的自定义 ListView
public class NonScrollListView extends ListView {
public NonScrollListView(Context context) {
super(context);
}
public NonScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
在您的布局资源文件中
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadingEdgeLength="0dp"
android:fillViewport="true"
android:overScrollMode="never"
android:scrollbars="none" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- com.Example Changed with your Package name -->
<com.Example.NonScrollListView
android:id="@+id/lv_nonscroll_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.Example.NonScrollListView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/lv_nonscroll_list" >
<!-- Your another layout in scroll view -->
</RelativeLayout>
</RelativeLayout>
</ScrollView>
在 Java 文件中
创建你的 customListview 对象而不是 ListView
喜欢:
NonScrollListView non_scroll_list = (NonScrollListView) findViewById(R.id.lv_nonscroll_list);
关于android - ScrollView 内不可滚动的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18813296/