繁体   English   中英

在ScrollView中滚动ListView

[英]scrolling ListView within 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上方和下方的视图。 同样,它是一个ViewPager:除了有问题的ListView之外,还有其他页面。

您不应在ScrollView中具有列表视图。

但是您可以拥有一个自定义类,并使用该类在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>

我有一个ScrollView

摆脱它。

它的子级之一是ListView

使用addHeaderView() / addFooterView()我的MergeAdapter或沿这些行的其他内容,将所有内容放入ListView

你不能滚动listview相同的方向scrollview 但是,如果你有一个水平scrollview可以滚动垂直,一个listview

您不能在滚动视图中添加ListView,因为列表视图也会出现scoll,并且listview滚动和滚动视图scoll之间会出现同步化问题。 您可以创建一个CustomList视图并将此方法添加到其中。

@Override public boolean onInterceptTouchEvent(MotionEvent ev)
{
    /*
     * Prevent parent controls from stealing our events once we've gotten a touch down
     */
    if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
        ViewParent p = getParent();
        if (p != null) {
            p.requestDisallowInterceptTouchEvent(true);
        }
    }
    return false;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM