我试图弄清楚为什么此LinearLayout
没有滚动:
<ScrollView
android:layout_width="match_parent"
android:fillViewport="true"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearLayout"
android:orientation="vertical"
android:weightSum="2"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:text="TITLE 1"
android:textSize="30sp" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp" />
</LinearLayout>
</ScrollView>
我正在以编程方式将textView
和listView
添加到linearLayout
,并且页面被切成两半。滚动的唯一内容是listViews
。
尝试此代码
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:fillViewport="true"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearLayout"
android:orientation="vertical"
android:weightSum="2"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:text="TITLE 1"
android:textSize="30sp" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
列表视图不会滚动,因为它是嵌套的。如果在Scrollview中使用listview,它们可能会互相干扰。 (两个滚动)您应该自定义Scrollview,这样当您触摸Listview区域时,scrollview将停止滚动。
1)创建新类:CustomScrollView
并从ScrollView
扩展它>
2)将xml <ScrollView>
更改为自定义滚动视图:<com.your_package_name.CustomScrollView>
CustomScrollView.java:
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;
public class CustomScrollView extends ScrollView {
public CustomScrollView(Context context) {
super(context);
}
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomScrollView(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("CustomScrollView", "onInterceptTouchEvent: DOWN super false" );
super.onTouchEvent(ev);
break;
case MotionEvent.ACTION_MOVE:
return false; // redirect MotionEvents to ourself
case MotionEvent.ACTION_CANCEL:
// Log.i("CustomScrollView", "onInterceptTouchEvent: CANCEL super false" );
super.onTouchEvent(ev);
break;
case MotionEvent.ACTION_UP:
//Log.i("CustomScrollView", "onInterceptTouchEvent: UP super false" );
return false;
default:
//Log.i("CustomScrollView", "onInterceptTouchEvent: " + action );
break;
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
super.onTouchEvent(ev);
//Log.i("CustomScrollView", "onTouchEvent. action: " + ev.getAction() );
return true;
}
}