java 我如何在 NestedScrollView 中有一个 ListView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35634023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i have a ListView inside a NestedScrollView
提问by Marcus
I have created an app with a page that need to load content dynamically from web service. I want to have listview that can scroll together with a linear layout inside NestedScrollView. But when the contents is loaded to the listview, it doesn't stretch to its full height.
我创建了一个应用程序,其页面需要从 Web 服务动态加载内容。我想要一个可以在 NestedScrollView 中与线性布局一起滚动的列表视图。但是当内容加载到列表视图时,它不会拉伸到它的全高。
Here is my code.
这是我的代码。
<?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>
I have some searched about scrollview cannot be nested. This is an example i want according to my layout designing from Google play review page. What the method they use? Suggest me if i do something wrong. Many thanks.
我有一些关于滚动视图不能嵌套的搜索。这是我想要的一个例子,根据我从 Google Play 评论页面设计的布局。他们使用什么方法?如果我做错了什么,建议我。非常感谢。
Here is what i want.
这是我想要的。
采纳答案by Red Hot Chili Coder
Well, I would suggest you 2 ways to solve that problem:
好吧,我会建议你 2 种方法来解决这个问题:
1) Try to make LinearLayout a header of your ListView. Note that header should be inflated as it is written here.
1) 尝试使 LinearLayout 成为您的 ListView 的标题。请注意,header 应该像这里写的那样膨胀。
2) You mentioned that you use NestedScrollView, so maybe you should also try to replace ListView inside NestedScrollView with LinearLayout, as wise people suggested here, adding row views in loop similar to how your adapter works.
2)你提到你使用了 NestedScrollView,所以也许你也应该尝试用 LinearLayout替换NestedScrollView 中的ListView ,正如明智的人在这里建议的那样,在循环中添加行视图类似于你的适配器的工作方式。
Good luck!
祝你好运!
回答by HymanA
on Lollipop onwards you can use
在棒棒糖上你可以使用
yourtListView.setNestedScrollingEnabled(true);
This enable or disable nested scrolling for this view if you need backwards compatibility with older version of the OS you'll have to use the RecyclerView.
如果您需要向后兼容旧版本的操作系统,这将启用或禁用此视图的嵌套滚动,您将不得不使用 RecyclerView。
回答by Nitin Vats
Just use this code, if you want to make listview expand in a nestedscrollview. Pass the listview reference to the function at the end of creating 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);
}
回答by W0rmH0le
Instead of add a ListView below a Linear Layout and inside a ScrollView, I would suggest to put everything inside the ListView.
我建议将所有内容都放在 ListView 内,而不是在线性布局下方和 ScrollView 内添加 ListView。
Yes you can.
是的你可以。
Implement (override) following method on your adapter:
在您的适配器上实现(覆盖)以下方法:
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 will re-use the views. However Android will re-use views of the same type always.
Android 将重新使用这些视图。然而,Android 将始终重复使用相同类型的视图。