android - CustomScrollView 中的 ListView 崩溃

标签 android listview

你好我是 Android 开发的新手,我读过各种线程,它们讨论了 ListView 不应该在 Scrollview 内并且需要包装的事实线性或相对布局。

但是我有这样的布局

<com.itmind.spac.spacapp.custom_extends.CustomScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollViewreceipt"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

    <include
    android:id="@+id/include1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    layout="@layout/toolbar" />

    <Spinner
        android:id="@+id/customers_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:theme="@android:style/Theme.Holo.Light.DarkActionBar"
        />
    <Spinner
        android:id="@+id/venues_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:theme="@android:style/Theme.Holo.Light.DarkActionBar"
        />
    <Spinner
        android:id="@+id/workgroup_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:theme="@android:style/Theme.Holo.Light.DarkActionBar"
        />

        <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical" android:layout_width="wrap_content"
         android:layout_height="wrap_content"
            android:minHeight="150dp">
    <ListView
        android:id="@+id/activityList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="15dp"
        android:minHeight="150dp"
        />
    </LinearLayout >

    <android.gesture.GestureOverlayView
        android:id="@+id/signaturePad"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_weight="5"
        android:background="#d3d3d3"
        android:eventsInterceptionEnabled="true"
        android:fadeEnabled="false"
        android:gestureColor="#333"
        android:gestureStrokeLengthThreshold="0.1"
        android:gestureStrokeType="multiple"
        android:fadeOffset="5000"
        android:orientation="vertical" >
    </android.gesture.GestureOverlayView>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="testImage"
        android:text="testami"/>
</LinearLayout>

这是我的 custoScrollView

public class CustomScrollView extends ScrollView {

private boolean enableScrolling = true;

public boolean isEnableScrolling() {
    return enableScrolling;
}

public void setEnableScrolling(boolean enableScrolling) {
    this.enableScrolling = enableScrolling;
}

public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public CustomScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomScrollView(Context context) {
    super(context);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

    if (isEnableScrolling()) {
        return super.onInterceptTouchEvent(ev);
    } else {
        return false;
    }
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (isEnableScrolling()) {
        return super.onTouchEvent(ev);
    } else {
        return false;
    }
}
}

我需要自定义类 ScrollView ,因为我可以在触摸时设置启用滚动

所以在我实现 GestureOverlayView.OnGestureListener 的 Activity 中我有:

 @Override
public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
    myScrollView.setEnableScrolling(false);
}
@Override
public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
    myScrollView.setEnableScrolling(true);
}

元素太多,我需要滚动查看所有元素。 在我的 scrollView 中,我有一个 listView,我用从 db 检索的数据填充它。

我的问题是我的 Listview 崩溃了,它显示一行,我看到并滚动到一行。

我尝试使用 min-height 但不起作用,如果我在 Listview 中放置布局高度,我将无法滚动此列表。

我该怎么做? 我会在一个长页面中列出一个 View ,所以我必须使用 ScrollView 或者是否有解决方案?

最佳答案

下面给定的自定义 ListView 类将根据 ScrollView 中的内容生成 Listview 的高度。在您的 xml 布局文件中使用它代替 ListView,就像您使用 CustomScrollView 一样。

public class CustomListView extends ListView {

//  private boolean expanded;

    public CustomListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomListView(Context context) {
        super(context);
    }

    public CustomListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

希望对您有所帮助。

注意:-根据this Android 开发者网站的链接,我们不应该将 ScrollView 与 ListView 一起使用,因为 ListView 会处理自己的垂直滚动。

关于android - CustomScrollView 中的 ListView 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38632634/

相关文章:

c# - 如何知道 ListView 中哪个 Xamarin Forms 条目已被修改?

java - Web 服务器在 Android 中不工作

android - 是否可以在Android中以编程方式收听扬声器的输出?

android - Android中单个设备的多个APP ID的Under Urban Airship

java - Android ListView onItemClickListener 被 ProgressBar 阻止

android - 如何将链接放在 TextView 上?

Android 应用程序持久身份验证

android - 添加到后台堆栈时如何维护 fragment 状态?

java - 如何让listView说一件事,但传递不同的数据

ios - react native ListView 问题