内部有两个 ListView 的可滚动视图
A scrollable view with two ListViews inside
我有这个视图,其中包含 header(不是 ListView
header)和一般信息,下面应该是两个 ListView
,已填充由 custom-made CursorAdapter
秒。现在我不能把所有东西都放在 ScrollView
里面,因为你不能把 ListView
放在里面(它会破坏东西并且不会滚动)。
有什么想法吗?布局是这样的:
<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:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
tools:context="...">
<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>
为 ScrollView
使用自定义 class
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>
现在您的两个列表视图都应该可以滚动了。
我有这个视图,其中包含 header(不是 ListView
header)和一般信息,下面应该是两个 ListView
,已填充由 custom-made CursorAdapter
秒。现在我不能把所有东西都放在 ScrollView
里面,因为你不能把 ListView
放在里面(它会破坏东西并且不会滚动)。
有什么想法吗?布局是这样的:
<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:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
tools:context="...">
<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>
为 ScrollView
使用自定义 classimport 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>
现在您的两个列表视图都应该可以滚动了。