android - 在 ScrollView 中滚动 ListView

标签 android android-layout android-listview android-scrollview

我有一个 ScrollView 。它的子项之一是 ListView。两者都向同一方向滚动。我如何让他们都响应滚动事件?然后,当到达 ListView 的末尾时,事件将转到父 ScrollView,以便 ScrollView 可以滚动?

xml布局:

<?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"
    >

    <LinearLayout
        ...
        android:orientation="vertical"
        >



        <LinearLayout
            android:id="@+id/..."
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"

            android:orientation="horizontal">

            ...
        </LinearLayout>

        <android.support.v4.view.ViewPager
            .../>


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="@dimen/pad_half"
            >
        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            >
        </LinearLayout>



    </LinearLayout>
</ScrollView>

我的 ListView 实际上在 ViewPager 里面。该设计使得 ListView 中的大约三个项目是可见的,并且用户能够滚动以查看其他项目。同时,ViewPager 上方和下方的 View 是可见的。同样,它是一个 ViewPager:除了所讨论的 ListView,它还有其他页面。

最佳答案

你不应该在 ScrollView 中有一个 ListView 。

但是您可以有一个自定义类,并使用它来在 ScrollView 中完成一个 Listview。

试试下面的代码

首先制作一个自定义的 ScrollView 类。

public class VerticalScrollview extends ScrollView{

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

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

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

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    final int action = ev.getAction();
    switch (action)
    {
        case MotionEvent.ACTION_DOWN:
                super.onTouchEvent(ev);
                break;

        case MotionEvent.ACTION_MOVE:
                return false; // redirect MotionEvents to ourself

        case MotionEvent.ACTION_CANCEL:
                super.onTouchEvent(ev);
                break;

        case MotionEvent.ACTION_UP:
                return false;

        default: break;
    }

    return false;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    super.onTouchEvent(ev);
     return true;
}
}

然后使用这个类进行布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >

<com.gui.today.VerticalScrollview
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
     <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <ProgressBar
                android:id="@+id/progressBar3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal" />

            <TextView
                android:id="@+id/empty_calender"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:text="@string/empty_calender"
                android:textColor="#ffffff"
                android:visibility="gone" />

            <ListView
                android:id="@+id/listView2"
                android:layout_width="fill_parent"
                android:layout_height="150dp" >
            </ListView>
        </FrameLayout>
    </LinearLayout>
  </com.gui.today.VerticalScrollview>

关于android - 在 ScrollView 中滚动 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20227068/

相关文章:

android - 在我的应用程序中处理我的网站 URL

php - 无法向数据库发送和检索数据(PHP/MySQL/Android)

android - LinearLayout 中元素间距的最佳实践

android - 优点和缺点,保持对字体的静态引用以便在布局中重用?

android - 从按钮中的文本中删除下划线 (Android)

android - 考虑 TextView 是可选择的

android - AppBarLayout.onCreateDrawableState 空指针异常 : Attempt to get length of null array

android - 获取自定义Listview Android中的总行数

android - 在滑动菜单 fragment 中为 ListView 添加 onitemclick 监听器

java - 如何使用getResources方法正确显示图片?