如何将 ScrollView 与一个 RelativeLayout 和两个 LinearLayout 一起使用?
How to use ScrollView with one RelativeLayout and two LinearLayout?
我已经实现了一个 ScrollView
,我在其中添加了一个 RelativeLayout
。在这个RelativeLayout
一个里加了两个LinearLayout
。每个都包含一个 TextView
和一个 ListView
。每个 ListView
包含超过 25 个项目。我想显示两个列表视图的所有项目,并且两者都有一个滚动条。现在,如果我在第一个 ListView
中仅添加 3 个项目,在第二个中添加 25 个项目,则第一个没有滚动,只有第二个有滚动。如果我在第一个 ListView
25 个项目中添加,在第二个中添加 3 个,第一个获得滚动条,第二个甚至不显示。我怎样才能做到这一点?这是我的代码:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linearLayout1">
<TextView
android:text="title1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/productsToBuyTitle" />
<com.example.alexm.shoppinglist.dlv.DragSortListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dragSortList1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linearLayout2"
android:layout_below="@id/linearLayout1">
<TextView
android:text="title2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/allProductsTitle" />
<com.example.alexm.shoppinglist.dlv.DragSortListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dragSortList2" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
提前致谢!
试试这个布局
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:id="@+id/linearLayout1">
<TextView
android:text="title1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/productsToBuyTitle" />
<com.example.alexm.shoppinglist.dlv.DragSortListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dragSortList1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:text="title2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/allProductsTitle" />
<com.example.alexm.shoppinglist.dlv.DragSortListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dragSortList2" />
</LinearLayout>
</LinearLayout>
</ScrollView>
制作固定高度的列表视图。假设 250dp
然后在你的 activity 中为每个列表视图添加此代码:
mListView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mScrollView.requestDisallowInterceptTouchEvent(true);
int action = event.getActionMasked();
switch (action) {
case MotionEvent.ACTION_UP:
mScrollView.requestDisallowInterceptTouchEvent(false);
break;
}
return false;
}
});
如果上面的代码不起作用,那么使用这个:
mListView.setOnTouchListener(new View.OnTouchListener() {
// Setting on Touch Listener for handling the touch inside ScrollView
@Override
public boolean onTouch(View v, MotionEvent event) {
// Disallow the touch request for parent scroll on touch of child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
这将避免scrollview
阻止列表视图上的触摸事件。
我已经将屏幕平均分为两部分..希望它现在可以滚动
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linearLayout1">
<TextView
android:text="title1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/productsToBuyTitle" />
<com.example.alexm.shoppinglist.dlv.DragSortListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dragSortList1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linearLayout2"
android:layout_below="@id/linearLayout1">
<TextView
android:text="title2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/allProductsTitle" />
<com.example.alexm.shoppinglist.dlv.DragSortListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dragSortList2" />
</LinearLayout>
</LinearLayout>
</ScrollView>
我认为,不仅在滚动方面,而且考虑到性能方面,更好的方法是拥有一个可以扩展所有这些布局的单一列表视图。你只需要处理一个卷轴,你的性能也会得到提升;带有滚动视图的嵌套列表视图不是一个好主意,滚动可能会变得棘手,这样,您的列表视图只会加载可见的单元格(是的,甚至更好的性能)。
因此,您将在该布局中定义一个 ListView,并使用适配器来扩充每个视图(TextView 和列表内容,即单元格)。
我相信这可以解决您的问题。如果您需要代码,请告诉我,我会更新答案
我已经实现了一个 ScrollView
,我在其中添加了一个 RelativeLayout
。在这个RelativeLayout
一个里加了两个LinearLayout
。每个都包含一个 TextView
和一个 ListView
。每个 ListView
包含超过 25 个项目。我想显示两个列表视图的所有项目,并且两者都有一个滚动条。现在,如果我在第一个 ListView
中仅添加 3 个项目,在第二个中添加 25 个项目,则第一个没有滚动,只有第二个有滚动。如果我在第一个 ListView
25 个项目中添加,在第二个中添加 3 个,第一个获得滚动条,第二个甚至不显示。我怎样才能做到这一点?这是我的代码:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linearLayout1">
<TextView
android:text="title1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/productsToBuyTitle" />
<com.example.alexm.shoppinglist.dlv.DragSortListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dragSortList1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linearLayout2"
android:layout_below="@id/linearLayout1">
<TextView
android:text="title2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/allProductsTitle" />
<com.example.alexm.shoppinglist.dlv.DragSortListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dragSortList2" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
提前致谢!
试试这个布局
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:id="@+id/linearLayout1">
<TextView
android:text="title1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/productsToBuyTitle" />
<com.example.alexm.shoppinglist.dlv.DragSortListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dragSortList1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:text="title2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/allProductsTitle" />
<com.example.alexm.shoppinglist.dlv.DragSortListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dragSortList2" />
</LinearLayout>
</LinearLayout>
</ScrollView>
制作固定高度的列表视图。假设 250dp
然后在你的 activity 中为每个列表视图添加此代码:
mListView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mScrollView.requestDisallowInterceptTouchEvent(true);
int action = event.getActionMasked();
switch (action) {
case MotionEvent.ACTION_UP:
mScrollView.requestDisallowInterceptTouchEvent(false);
break;
}
return false;
}
});
如果上面的代码不起作用,那么使用这个:
mListView.setOnTouchListener(new View.OnTouchListener() {
// Setting on Touch Listener for handling the touch inside ScrollView
@Override
public boolean onTouch(View v, MotionEvent event) {
// Disallow the touch request for parent scroll on touch of child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
这将避免scrollview
阻止列表视图上的触摸事件。
我已经将屏幕平均分为两部分..希望它现在可以滚动
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linearLayout1">
<TextView
android:text="title1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/productsToBuyTitle" />
<com.example.alexm.shoppinglist.dlv.DragSortListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dragSortList1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linearLayout2"
android:layout_below="@id/linearLayout1">
<TextView
android:text="title2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/allProductsTitle" />
<com.example.alexm.shoppinglist.dlv.DragSortListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dragSortList2" />
</LinearLayout>
</LinearLayout>
</ScrollView>
我认为,不仅在滚动方面,而且考虑到性能方面,更好的方法是拥有一个可以扩展所有这些布局的单一列表视图。你只需要处理一个卷轴,你的性能也会得到提升;带有滚动视图的嵌套列表视图不是一个好主意,滚动可能会变得棘手,这样,您的列表视图只会加载可见的单元格(是的,甚至更好的性能)。
因此,您将在该布局中定义一个 ListView,并使用适配器来扩充每个视图(TextView 和列表内容,即单元格)。
我相信这可以解决您的问题。如果您需要代码,请告诉我,我会更新答案