ScrollView 中不可滚动的 ListView

我试着做一个这样的布局:

问题是我不希望 ListViews是可滚动的。如果我把 ListView的高度设置为 wrap_content,那就不起作用了。唯一的方法是设置一个特定的高度-但我不知道有多少项目将在 ListView

我知道我不应该把 ListView放在 ScrollView 中-但是我不希望 ListView是可滚动的,只是为了显示所有的项目。

也许还有更好的办法?

44204 次浏览

嗯,有一个很酷的 Android 家伙叫做 Mark Murphy 又名 CommonsWare,他构建了一个非常有用的组件 合并适配器,它可以让你做你需要做的事情。您需要动态地添加视图,而不是从 XML 添加视图; 但是考虑到您的简单 UI,这应该不成问题。我用它来处理这种案子我不知道该滚动什么样的数据。

如果你不想使用它(可能是出于许可的原因) ,你可以在那里有一个单独的列表视图(而不是那两个连续的列表视图) ,并覆盖 getItemViewsCountgetItemViewTypegetView,以提供上述布局。如果你在谷歌上搜索 android listview different rows,你会发现有用的信息。例如 这个链接这个

不过,我还是会选择合并适配器。

根本不需要使用 listview

如果您认真考虑一下,在您的情况下,listview可以替换为 LinearLayoutAdapter也是一样的,但是不需要用 listview连接适配器,只需要在 for 循环中调用适配器的 getView函数,并将检索到的视图添加到垂直 linearlayout中。

但是,只有当列表中的项数量较少且这些项的显示表示形式不需要占用大量内存时,才建议使用这种方法。

创建不可滚动的自定义列表视图

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) ;

我有一个简单的解决方案,如果可以计算你的项目高度/宽度... 你只需要创建一个父 ListView 线性布局,然后从 Java 固定父高度。

enter image description here

假设您需要设计这个视图... 现在父布局大小是从 java 定义的..。

final LinearLayout parent=(LinearLayout)findViewById(R.id.parent);
final ListView  listview=(ListView)findViewById(R.id.listview);
parent.setLayoutParams(new LinearLayout.LayoutParams(parent.getLayoutParams().width, dpToPx( number_of_item_in_list * per_item_size_in_dp )));
//number_of_item_in_list is list side ~ listArray.size();
//per_item_size_in_dp = calculate item size in dp. Ex: 80dp


public static int dpToPx(int dp)
{
return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}

仅此而已:)

做到这一点的最佳方法是使用 RecyclerViewNestedScrollView

不需要黑客,它的工作方式就像你想要的。