2016-02-25 99 views
5

我创建了一个应用程序,其中包含需要从Web服务动态加载内容的页面。我想要有一个可以在NestedScrollView内与线性布局一起滚动的listview。但是当内容加载到列表视图时,它不会被划伤到其全部高度。我该如何在NestedScrollView中使用ListView

这是我的代码。

<?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 review页面进行的布局设计。他们使用什么方法?建议我如果我做错了什么。非常感谢。

enter image description here

这里是我想要的。

enter image description here

+1

修复listview的高度。或者最好是使用coordinatorlayout和recyclerview.Refer [this](http://inthecheesefactory.com/blog/android-design-support-library-codelab/en) –

回答

4

那么,我建议你2种方法来解决这个问题:

1)尽量使你的LinearLayout的ListView的头。请注意,标题应该填充为here

2)你提到,你用NestedScrollView,所以也许你也应该尝试与LinearLayout中更换的ListView里面嵌套滚动型,因为明智的人建议here,并以类似于如何适配器工作循环增加行的意见。

祝你好运!

1

相反的下面添加一个线性布局和滚动型内一个ListView,我建议把一切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会始终重复使用相同类型的视图。

+0

感谢Guiherme我会试试这个。 – Marcus

+0

不客气..尝试亚历克斯胡椒的答案以及...似乎更简单 – W0rmH0le

3

的棒棒糖起,可以使用

yourtListView.setNestedScrollingEnabled(true); 

此启用或对这一观点 禁用嵌套的滚动,如果你需要与旧版本你将不得不使用RecyclerView操作系统的向后兼容性。

+0

[点评](https://stackoverflow.com/a/35198484/4015799)给原来的答复者。 –