一个可滚动的视图,里面有两个ListView

时间:2015-09-26 10:37:58

标签: android listview android-listview cursor

我有这个视图,其中包含一般信息的标题(不是services标题),下面应该是两个ListView s,由定制的ListView填充。现在我不能将所有内容放在CursorAdapter内,因为你不能在其中放置ScrollView(它会破坏内容并且不会滚动)。

有什么想法吗?布局是这样的:

ListView

1 个答案:

答案 0 :(得分:0)

使用ScrollView的自定义类

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.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:
                    Log.i("VerticalScrollview", "onInterceptTouchEvent: DOWN super false" );
                    super.onTouchEvent(ev);
                    break;

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

            case MotionEvent.ACTION_CANCEL:
                    Log.i("VerticalScrollview", "onInterceptTouchEvent: CANCEL super false" );
                    super.onTouchEvent(ev);
                    break;

            case MotionEvent.ACTION_UP:
                    Log.i("VerticalScrollview", "onInterceptTouchEvent: UP super false" );
                    return false;

            default: Log.i("VerticalScrollview", "onInterceptTouchEvent: " + action ); break;
        }

        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        super.onTouchEvent(ev);
        Log.i("VerticalScrollview", "onTouchEvent. action: " + ev.getAction() );
         return true;
    }
}

并在xml中使用它 如: -

<com.yourPackage.VerticalScrollView 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:orientation="vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    tools:context="...">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
  >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="8dp"
        android:paddingTop="8dp">
    <!-- A lot of things here (the header I was talking about) -->
    </RelativeLayout>

    <!-- Just a horizontal line (separator) -->
    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/gray_dark" />

    <!-- Two ListViews here -->
    <ListView
        android:id="@+id/first_listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ListView
        android:id="@+id/second_listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
</com.yourPackage.VerticalScrollView>

现在你的两个列表视图都应该是可滚动的。