Java 如何在NestedScrollView中拥有ListView
我创建了一个应用程序,其中包含一个需要从web服务动态加载内容的页面。我想有一个listview,它可以在NestedScrollView中与线性布局一起滚动。但是,当内容加载到listview时,它不会拉伸到其全部高度 这是我的密码Java 如何在NestedScrollView中拥有ListView,java,android,listview,layout,android-nestedscrollview,Java,Android,Listview,Layout,Android Nestedscrollview,我创建了一个应用程序,其中包含一个需要从web服务动态加载内容的页面。我想有一个listview,它可以在NestedScrollView中与线性布局一起滚动。但是,当内容加载到listview时,它不会拉伸到其全部高度 这是我的密码 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.co
<?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>
这是我想要的
我建议将所有内容都放在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将始终重复使用相同类型的视图。好吧,我建议您两种方法来解决这个问题:
1) 尝试使LinearLayout成为ListView的标题。请注意,标题在写入时应充气
2) 您提到您使用了NestedScrollView,所以也许您还应该尝试用LinearLayout替换NestedScrollView中的ListView,正如智者所建议的,在循环中添加行视图,类似于适配器的工作方式
祝你好运 在棒棒糖上,你可以使用
yourtListView.setNestedScrollingEnabled(true);
这将启用或禁用此视图的嵌套滚动
如果需要向后兼容较旧版本的操作系统,则必须使用RecyclerView。如果要使listview在nestedscrollview中展开,只需使用此代码即可。
在创建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);
}
private static void setListViewHeightBasedOnChildren(ListView ListView){
ListAdapter ListAdapter=listView.getAdapter();
如果(listAdapter==null)
返回;
int desiredWidth=View.MeasureSpec.makeMeasureSpec(listView.getWidth(),View.MeasureSpec.UNSPECIFIED);
int totalHeight=0;
视图=空;
对于(int i=0;i
固定listview的高度。或者最好的方法是使用coordinatorlayout和recyclerview。请参考。谢谢Guiherme,我将尝试此方法。不客气。。请尝试Alex Pepper的答案。。。这似乎更简单