[英]disable listview scrolling inside scrollview
我面臨在滾動視圖中實現兩個listview的問題。 我有可進行滾動瀏覽的活動。 這是我想要的圖像
我要制作包含兩個列表視圖的發票,一個用於項目,一個用於跟蹤數據。 我能夠動態地使listview高度,也可以禁用其click事件。 但是現在在列表視圖中,我無法單擊或滾動屏幕。 所有組件都在滾動視圖中。 但是當我觸摸列表視圖時,我無法滾動滾動視圖。
這是我在管理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並使用此屬性
note_recyclerview.setNestedScrollingEnabled(false);
但我沒有得到我想要的。
我該如何實現?
不要在ScrollView
使用ListView
。
當您使用多個ListView
,因此應使用android.support.v4.widget.NestedScrollView
而不是ScrollView
來獲得正確的滾動行為。
NestedScrollView
就像ScrollView
一樣,但是在新舊版本的Android上,它都同時充當nested
滾動parent
和child
。 默認情況下啟用嵌套滾動。
請參閱文檔 。
這是一個例子:
<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>
希望這會有所幫助〜
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.