2016-10-15 7 views
0

Привет У меня есть попытаться вставить ListView в Scrollview, но у меня есть эта проблема: enter image description hereAndroid ListView в Scrollview

пространство, Scrollview резерв ListView мало, я хочу, чтобы Scrollview является чем экран, я хочу Scrollview является видимо только тогда, когда listview становится больше, чем на экране. Как я это делаю?

это код:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <LinearLayout android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:orientation="vertical" 
     android:id="@+id/activity_lista__eventi" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="14dp" 
     android:paddingRight="14dp" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     tools:context="com.example.fra87.eudroid.activity_class.Lista_Eventi"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="50dp" 
      android:orientation="vertical"> 
      <SearchView 
       android:layout_width="match_parent" 
       android:layout_height="50dp" 
       android:queryHint="Evento" 
       android:id="@+id/cercaEvento"/> 

     </LinearLayout> 


     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:id="@+id/linearLayoutEventi"> 

      <ListView 
       android:id="@+id/listViewEventi" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/cercaEvento" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="15dp" /> 

     </LinearLayout> 

    </LinearLayout> 
</ScrollView> 

Как я могу это сделать?

+0

Try nestedscrollview вместо Scrollview – AbhayBohra

+0

сделать вашу линейную высоту макета как wrape_content и посетить этот http://stackoverflow.com/questions/18367522/android -list-вид-в-а-свитка вид –

ответ

0

Другие решения можно найти здесь: Android list view inside a scroll view

Это общая проблема, что многие лица разработчиков. Проблема в том, что вы укладываете прокручиваемое представление в другое прокручиваемое представление. Одно решение для удаления списка из прокрутки.

Другой следующий код:

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

int totalHeight = 0; 
for (int i = 0; i < listAdapter.getCount(); i++) { 
    View listItem = listAdapter.getView(i, null, listView); 
    listItem.measure(0, 0); 
    totalHeight += listItem.getMeasuredHeight(); 
} 

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

}