[英]How can i have a ListView inside a NestedScrollView
我創建了一個帶有頁面的應用程序,需要從Web服務動態加載內容。 我希望listview能夠與NestedScrollView中的線性布局一起滾動。 但是當內容加載到列表視圖時,它不會延伸到其完整高度。
這是我的代碼。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.myquestionth.myquestionth10.Profile2Activity"
tools:showIn="@layout/activity_profile2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#BBBBBB" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Media heading"
android:id="@+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis."
android:id="@+id/textView8" />
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#70bcf5" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
我有一些關於scrollview的搜索無法嵌套。 根據我在Google Play評論頁面設計的布局,這是我想要的一個例子。 他們使用的方法是什么? 建議我,如果我做錯了什么。 非常感謝。
這就是我想要的。
在棒棒糖上你可以使用
yourtListView.setNestedScrollingEnabled(true);
如果您需要向后兼容您必須使用RecyclerView的舊版操作系統,則啟用或禁用此視圖的嵌套滾動。
我建議將所有內容放在ListView中,而不是在線性布局下面和ScrollView中添加ListView。
是的你可以。
在適配器上實現(覆蓋)以下方法:
public class MyAdapter extends BaseAdapter {
// One view to Header
// One view to filter options ("most helpful first" and "Options")
// One view to comments
private final static int VIEW_HEADER = 0;
private final static int VIEW_OPTIONS = 1;
private final static int VIEW_COMMENTS = 2;
private final static int VIEW_TYPE_MAX = 3;
@Override
public int getViewTypeCount () {
// It will return 3 since I have 3 different types of VIEW
return VIEW_TYPE_MAX;
}
@Override
public int getItemViewType(int position) {
if (position == 0)
return VIEW_HEADER;
else if (position == 1)
return VIEW_OPTIONS;
else
return VIEW_COMMENTS;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
if(getItemViewType(position) == VIEW_HEADER)
// Inflate HEADER Layout
else if (getItemViewType(position) == VIEW_OPTIONS)
// Inflate Options Layout
else
// Inflate comments Layout
}
// Fill the view contents according to its type
....
return convertView;
}
}
Android會重新使用這些視圖。 但是Android總是會重復使用相同類型的視圖。
如果您想在nestedscrollview中展開listview,只需使用此代碼即可。 將listview引用傳遞給創建listview末尾的函數。
private static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.