滚动型内滚动的ListViewListView

2023-09-06 23:01:05 作者:-有种距离变得让人沉重

我有一个滚动型。它的一个孩子就是一个ListView。两个滚动在相同的方向。我该如何让他们都来响应滚动事件?然后,当达到ListView中的最后事件转到父滚动型所以滚动型可以滚动?

I have a ScrollView. One of its children is a ListView. Both scrolling in the same direction. How do I get both of them to respond to scroll events? And then when the end of the ListView is reached for the event to go to the parent ScrollView so the ScrollView may scroll?

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控件

My ListView actually goes inside the ViewPager. The design is such that about three items in the ListView is visible, and user is able to scroll to see other items. In the meantime, the views above and below the ViewPager are visible. Again, it's a ViewPager: it has other pages than the ListView in question.

推荐答案

你不应该有一个滚动型中列表视图。

You Should not have a listview within a ScrollView.

但你可以有一个自定义类,并用它来完成一个ListView与在滚动型。

But you can have a custom class and use that to accomplish A Listview with in a ScrollView.

请尝试以下code

先制作一个自定义的滚动型类。

first make a custom ScrollView Class.

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

然后使用这个类为您布局

Then use this class for you layout

<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>
 
精彩推荐
图片推荐