2017-02-22 36 views
-2

我有一個佈局,我必須在列表視圖頂部和底部添加一些東西,所以我不想只滾動我只想滾動列表視圖我想滾動整個屏幕所以我添加了這個代碼。Listview在Android中從底部打開

//刪除從列表視圖中滾動,使其滾動整個屏幕

public 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, ActionBar.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); 
    } 

這裏是我的xml

<?xml version="1.0" encoding="utf-8"?> 

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:id="@+id/header" 
      android:orientation="horizontal" 
      android:layout_alignLeft="@+id/body" 
      android:layout_marginBottom="80dp" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 






     </LinearLayout> 
     <LinearLayout 
      android:id="@+id/body" 
      android:orientation="vertical" 
      android:background="#ffffff" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_alignTop="@+id/header" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentEnd="true" 
      android:paddingTop="10dp" 


      > 

      <ScrollView 
       android:layout_width="match_parent" 
       android:layout_marginTop="@dimen/activity_vertical_margin" 
       android:layout_height="wrap_content"> 

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

        <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_marginLeft="@dimen/activity_horizontal_margin" 
         android:layout_marginRight="@dimen/activity_horizontal_margin" 
         android:layout_marginBottom="@dimen/activity_horizontal_margin" 
         android:weightSum="2" 
         android:layout_height="wrap_content"> 

         <TextView 
          android:layout_width="0dp" 
          android:text="RSHO Doses" 
          android:layout_weight="1" 
          android:textColor="#636363" 
          android:textSize="@dimen/sub_Heading_Size" 
          android:drawablePadding="10dp" 
          android:drawableLeft="@drawable/rsho_drawable" 
          android:layout_height="wrap_content" /> 

         <TextView 
          android:layout_width="0dp" 
          android:layout_weight="1" 
          android:textSize="@dimen/sub_Heading_Size" 
          android:text="Seizures" 
          android:drawablePadding="10dp" 
          android:drawableLeft="@drawable/seizures_drawable" 

          android:textColor="#636363" 
          android:layout_height="wrap_content" /> 

        </LinearLayout> 

        <LinearLayout 
         android:layout_width="match_parent" 

         android:orientation="vertical" 
         android:layout_height="wrap_content"> 

         <TextView 
          android:layout_width="match_parent" 
          android:text="Events" 
          android:padding="@dimen/activity_horizontal_margin" 
          android:background="@color/colorHeadingBG" 
          android:textSize="@dimen/sub_Heading_Size" 
          android:textColor="@color/colorHeading" 
          android:layout_height="wrap_content" /> 

         <ListView 
          android:layout_width="match_parent" 
          android:dividerHeight="2dp" 
          android:divider="#e5e5e5" 
          android:layout_alignParentTop="true" 
          android:paddingLeft="@dimen/activity_horizontal_margin" 
          android:paddingBottom="@dimen/activity_horizontal_margin" 
          android:id="@+id/listView" 
          android:layout_height="wrap_content"></ListView> 

        </LinearLayout> 
       </LinearLayout> 

      </ScrollView> 
     </LinearLayout> 





    </LinearLayout> 






</android.support.design.widget.CoordinatorLayout> 

所以當我打開這個應用程序刪除它從列表視圖中滾動,並添加全在頁面中列出視圖高度,但始終從底部打開。任何解決方案

+0

你想要的是一種混亂,嘗試把一些逗號或解釋更好,我會分析現在。 –

+0

我想滾動整個頁面不僅listview和我能夠做到這一點使用上面的代碼,但是當我打開應用程序它從屏幕底部打開。獲取我的觀點? –

回答

0

嘗試使用此設置。

listView.setNestedScrollingEnabled(false); 
listView.setHasFixedSize(false); 

這樣,ListView的滾動將被禁用。

+0

它說無法解決setHadFixedSize(boolen) –

+0

OPS,這是爲了回收者視圖。 –

+0

正如我告訴過你的,我可以停止滾動,但是當我打開應用程序時,它從列表視圖的底部打開 –