簡體   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