2015-10-15 321 views
3

我有一個佈局,它有兩個ListViews,它們都在滾動視圖內,因爲內容可以超過設備的高度。這是佈局:ScrollView中的列表視圖不顯示所有項目

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.galleri5.android.activities.ImageActivity"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:elevation="4dp" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="#FAC80A"> 

     <RelativeLayout 
      android:id="@+id/relativeLayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Image" 
       android:textColor="#000000" 
       android:textSize="17sp" 
       android:layout_centerVertical="true" 
       android:id="@+id/textView80" /> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/imageView134" 
       android:src="@drawable/gallery_page_ic_notifications" 
       android:layout_alignParentEnd="true" 
       android:layout_marginRight="30dp" 
       android:layout_centerVertical="true" /> 

     </RelativeLayout> 

    </android.support.v7.widget.Toolbar> 

    <ScrollView 
     android:id="@+id/scrollView2" 
     android:layout_below="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="true"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/user_profile_bg" 
      android:orientation="vertical"> 

      <ImageView 
       android:id="@+id/imageView102" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" 
       android:layout_marginBottom="25dp" 
       android:layout_marginTop="10dp" 
       android:adjustViewBounds="true" /> 

      <ImageView 
       android:id="@+id/imageView103" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" 
       android:layout_marginBottom="25dp" 
       android:src="@drawable/user_profile_activity_1_shape_thin_line_separator" /> 

      <TextView 
       android:id="@+id/textView38" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="10dp" 
       android:layout_marginLeft="30dp" 
       android:layout_marginRight="30dp" 
       android:alpha="0.8" 
       android:inputType="textMultiLine" 
       android:textColor="#FFFFFF" /> 

      <TextView 
       android:id="@+id/textView39" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="25dp" 
       android:layout_marginLeft="30dp" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:textColor="#707070" /> 

      <ImageView 
       android:id="@+id/imageView104" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" 
       android:layout_marginBottom="25dp" 
       android:src="@drawable/user_profile_activity_1_shape_thin_line_separator" /> 

      <LinearLayout 
       android:id="@+id/linearLayout3" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="20dp" 
       android:layout_marginLeft="30dp" 
       android:layout_marginRight="30dp" 
       android:orientation="horizontal"> 

       <RelativeLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 

        <ImageView 
         android:id="@+id/imageView107" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_centerVertical="true" 
         android:paddingRight="5dp" /> 

        <LinearLayout 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_centerVertical="true" 
         android:layout_toRightOf="@+id/imageView107" 
         android:orientation="vertical" 
         android:paddingLeft="5dp"> 

         <TextView 
          android:id="@+id/textView45" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:alpha="0.8" 
          android:paddingBottom="2dp" 
          android:textColor="#FFFFFF" /> 

         <TextView 
          android:id="@+id/textView46" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:paddingTop="2dp" 
          android:textAppearance="?android:attr/textAppearanceSmall" 
          android:textColor="#707070" 
          android:textSize="12sp" /> 
        </LinearLayout> 

        <ImageView 
         android:id="@+id/imageView108" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentEnd="true" 
         android:layout_centerVertical="true" 
         android:src="@drawable/image_page_button_bookmark" /> 
       </RelativeLayout> 
      </LinearLayout> 

      <ImageView 
       android:id="@+id/imageView109" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" 
       android:layout_marginBottom="10dp" 
       android:src="@drawable/user_profile_activity_1_shape_thin_line_separator" /> 

      <android.support.v7.widget.CardView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="35dp" 
       android:layout_marginLeft="30dp" 
       android:layout_marginRight="30dp"> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="#343031" 
        android:orientation="vertical"> 

        <TextView 
         android:id="@+id/textView47" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginLeft="25dp" 
         android:layout_marginTop="15dp" 
         android:text="This Image is Part Of..." 
         android:textColor="#707070" /> 

        <ImageView 
         android:id="@+id/imageView110" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center_horizontal" 
         android:layout_marginBottom="20dp" 
         android:layout_marginTop="10dp" 
         android:src="@drawable/user_profile_activity_1_shape_thin_line_separator" /> 

        <ListView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:id="@+id/listView2" /> 

        <TextView 
         android:id="@+id/textView57" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginBottom="20dp" 
         android:layout_marginLeft="30dp" 
         android:text="See All Galleries" 
         android:textColor="#707070" /> 

       </LinearLayout> 


      </android.support.v7.widget.CardView> 

      <ImageView 
       android:id="@+id/imageView125" 
       android:layout_width="305dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="right" 
       android:layout_marginBottom="20dp" 
       android:src="@drawable/image_page_belongs_to_shape_line_separator_1" /> 

      <ListView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/listView6" /> 

      <EditText 
       android:id="@+id/editText6" 
       android:layout_width="300dp" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="40dp" 
       android:alpha="0.8" 
       android:inputType="text" 
       android:elegantTextHeight="true" 
       android:enabled="true" 
       android:hint="Comment" 
       android:textCursorDrawable="@null" 
       android:textColor="#FFFFFF" 
       android:textColorHint="#FFFFFF" 
       android:layout_marginTop="10dp" 
       android:backgroundTint="#fac80a" /> 

     </LinearLayout> 
    </ScrollView> 

</RelativeLayout> 

這兩個listviews有id listView2和listView6。我已經正確設置了適配器的列表視圖。我從後端獲取數據。對於這兩個listviews,我從後端得到兩個項目,但實際上它只在每個listView中顯示一個項目。我也嘗試使用notifyDataSetChanged()將項目添加到第二個listView,但這不起作用。我只得到一個項目。可能的原因是什麼以及如何解決?

+0

列表視圖可以滾動本身,當您在滾動視圖使用它,它會導致問題 – Pirisok

+0

一個選擇是使用自動列表視圖中展開其子元素的高度。所以你可以使用scrollview,列表視圖高度將等於它的子高度,所以不需要滾動。這裏是鏈接第2個答案由DougW [鏈接](http://stackoverflow.com/questions/3495890/how-can-i-put-a-listview-into-a-scrollview-without-it-collapsing)...其他選項是使用NestedScroll和Recyclerview。例如,我將不得不查找或編寫它並讓你知道。 –

+0

我的建議是使用具有多種視圖類型的[RecyclerView](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html)。看看下面的[gist](https://gist.github.com/hister/d56c00fb5fd2dfaf279b)。 –

回答

1

這裏是我做的,萬一別人是有類似的問題:

注:該解決方案是非常低效的。我使用它,因爲我有我的listView中最多2個項目。我不會推薦給其他人,如果你有一個列表視圖有一些大量的項目。

public static void setListViewHeightBasedOnChildren(ListView listView) { 
       ListAdapter listAdapter = listView.getAdapter(); 
      if (listAdapter == null) { 
      // pre-condition 
        return; 
      } 

      int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom(); 
      for (int i = 0; i < listAdapter.getCount(); i++) { 
       View listItem = listAdapter.getView(i, null, listView); 
       if (listItem instanceof ViewGroup) { 
        listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
       } 
       listItem.measure(0, 0); 
       totalHeight += listItem.getMeasuredHeight(); 
      } 

      ViewGroup.LayoutParams params = listView.getLayoutParams(); 
      params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); 
         listView.setLayoutParams(params); 
     } 

設置適配器後,我在兩個listView上調用了這個函數。 希望它有幫助。

+0

你不知道你的解決方案是多麼的錯誤是.. 它很容易引起有關CPU,內存等這麼多的問題.. 如果你有在列表視圖1000名的項目?你加載列表加載1000個項目?那是錯誤的。 – JozeRi

+0

我知道這種方法效率不高。但是我在列表中僅顯示最多2件商品。所以我繼續這個方法。 –

+0

也許你親自使用2項在最大..但是你自己寫的 - 「這是我做的,萬一別人是有類似的問題」 ..別人可能會使用此代碼,並依靠它。而其他人可能有2個以上的項目(可能還有2個以上),他們可能認爲這個代碼是一個很好的解決方案,但我不認爲它是。請至少編輯您的答案,以免其他編碼者不會感到困惑。 ty :) – JozeRi

0

不建議在滾動視圖中使用listview。雖然列表顯示了所有項目,但將列表滾動體驗並不會很好。您應該重構佈局以避免這種情況。

希望它可以幫助你!

+0

現在,我的列表視圖內容佔據屏幕底部高度的30%左右。如果我不放置滾動視圖,我將只能在屏幕的底部30%進行滾動。如果內容的組合高度大於設備的高度,我希望列表視圖上方的內容也滾動。 –

+0

我明白你,你能改變你的屏幕邏輯嗎?例如,在按下「顯示列表」按鈕時,在對話框或新屏幕中顯示列表... –

+0

我不能。它是由設計師製作的,我必須實現它。我也試過嵌套滾動視圖,但那不起作用。 –

1

正如我在評論中所說,而不是在scrollview內使用listview使用recyclerview。我剛剛創建了一個demo project,讓您瞭解具有多種佈局的回收站。導入項目,並看看它。

同樣,您可以將當前佈局裁剪爲多個佈局,並根據您的需求膨脹它們並在recyclerview中實施它。

1

另一種可能性是使用一個的ListView2- ..不同適配器許多不同視圖。而對於ListView,則有https://github.com/commonsguy/cwac-merge。在一個大適配器中添加許多適配器和視圖,您可以添加到1個ListView。從頁面

文字:

只需創建一個MergeAdapter並調用addAdapter(),addView(),或addViews()(後者接受列表),那麼你的適配器連接到ListView。

搖籃:

repositories { 
    maven { 
     url "https://s3.amazonaws.com/repo.commonsware.com" 
    } 
} 

dependencies { 
    compile 'com.commonsware.cwac:merge:1.1.+' 
} 
+0

你能舉一些例子嗎? –

相關問題