2016-02-25 100 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)給原來的答覆者。 –