禁用列表视图在scrollview中滚动 [英] disable listview scrolling inside scrollview
问题描述
我在滚动视图中实现两个listview时遇到了问题.我有可以进行滚动浏览的活动. 这是我想要的图像
I am facing problem to implement two listview in scrollview. I have activity in which I have scrollview. here is image what I want
我要制作包含两个列表视图的发票,一个用于项目,一个用于跟踪数据.我能够动态地使listview高度,也可以禁用其click事件.但是现在在列表视图中,我无法单击或滚动屏幕. 所有组件都在滚动视图中.但是当我触摸列表视图时,我无法滚动滚动视图.
I want to make invoice which contain two listviews one for items and one for tracking data. I am able to make listview height dynamically and also disable its click event. but now on listview I am not able to click or scroll screen. all components are in scrollview. but I am not able to scroll scrollview when I touch on listviews.
这是我管理listview
public static boolean setListViewHeightBasedOnItems(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter != null) {
int numberOfItems = listAdapter.getCount();
// Get total height of all items.
int totalItemsHeight = 0;
for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
View item = listAdapter.getView(itemPos, null, listView);
float px = 500 * (listView.getResources().getDisplayMetrics().density);
item.measure(View.MeasureSpec.makeMeasureSpec((int)px, View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalItemsHeight += item.getMeasuredHeight();
}
// Get total height of all item dividers.
int totalDividersHeight = listView.getDividerHeight() *
(numberOfItems - 1);
// Get padding
int totalPadding = listView.getPaddingTop() + listView.getPaddingBottom();
// Set list height.
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalItemsHeight + totalDividersHeight + totalPadding;
listView.setLayoutParams(params);
listView.requestLayout();
return true;
} else {
return false;
}
}`
我尝试了带有此属性的recyclerview
I tried recyclerview and with this property
note_recyclerview.setNestedScrollingEnabled(false);
但是我没有得到想要的东西.
but I didn't get what I want.
我该如何实现?
推荐答案
请勿在ScrollView
内使用ListView
.
Do not use ListView
inside ScrollView
.
由于您使用多个ListView
,因此应使用android.support.v4.widget.NestedScrollView
而不是ScrollView
来获得正确的滚动行为.
As you are using multiple ListView
, so you should use android.support.v4.widget.NestedScrollView
instead of ScrollView
to get proper scrolling behavior.
NestedScrollView
就像ScrollView
一样,但是它支持充当 在新版本和旧版本上都同时滚动nested
和parent
Android.默认情况下,嵌套滚动处于启用状态.
NestedScrollView
is just likeScrollView
, but it supports acting as both anested
scrollingparent
andchild
on both new and old versions of Android. Nested scrolling is enabled by default.
请参见文档.
以下是示例:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<ListView
android:id="@+id/listview2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
希望这会有所帮助〜
这篇关于禁用列表视图在scrollview中滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!