在scrollview中完全显示listview

在scrollview中完全显示listview,listview,android-listview,android-linearlayout,adapter,Listview,Android Listview,Android Linearlayout,Adapter,我的布局如下: <ScrollView android:id="@+id/scrollview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="@dimen/activity_vertical_margin" androi

我的布局如下:

<ScrollView
            android:id="@+id/scrollview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/activity_vertical_margin"
            android:overScrollMode="never" >  
    <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
        <Button
                    android:id="@+id/btnStart"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/default_button_height"
                    android:layout_marginLeft="@dimen/activity_horizontal_margin"
                    android:layout_marginRight="@dimen/activity_horizontal_margin"
                    android:background="@drawable/rounded_alpha_button"
                    android:text="@string/no_ora_inizio"
                    android:textColor="@color/labels"
                    android:textSize="@dimen/button_font_size" />
        <ListView
                    android:id="@+id/listTargets"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="@dimen/activity_vertical_margin" />
    </LinearLayout>
</ScrollView>

我无法修改布局,在listview中,我必须添加一个适配器列表,定义为:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/alpha_white" >
    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_toLeftOf="@+id/counterContainer"
        android:ellipsize="end"
        android:gravity="center_vertical"
        android:textColor="@color/labels"
        android:textSize="@dimen/label_font_size" />
    <LinearLayout
        android:id="@+id/counterContainer"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_marginRight="8dp"
        android:gravity="center_vertical"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/btnSub"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:background="@drawable/circular_alpha_button"
            android:gravity="center"
            android:lineSpacingExtra="6dp"
            android:text="-"
            android:textColor="@color/labels"
            android:textSize="@dimen/label_big_font_size" />
        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:gravity="center"
            android:text="0"
            android:textColor="@color/labels"
            android:textSize="@dimen/label_font_size" />
        <Button
            android:id="@+id/btnAdd"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:background="@drawable/circular_alpha_button"
            android:gravity="center"
            android:lineSpacingExtra="7dp"
            android:text="+"
            android:textColor="@color/labels"
            android:textSize="@dimen/label_big_font_size" />
    </LinearLayout>
</RelativeLayout>

问题是适配器列表没有完全显示

我知道在ScrollView中放置ListView是不正确的,我已经找到了另一个建议使用LinearLayout而不是ListView的线程。我尝试了,但使用LinearLayout,ui显示正确,但我无法更新适配器,notifyDataSetChanged()不会更新链接到按钮btnSub和btnAdd的文本id/text2

现在我正试图找到另一种解决方案,使用ListView或其他类型的视图

我发现以下方法可以设置listview高度:

public void setListViewHeightBasedOnChildren(ListView listView) {
    if (mAdapter == null)
        return;

    int totalHeight = 0;//it is the ListView Height
    for (int i = 0, len = mAdapter.getCount(); i < len; i++) {
        View listItem = mAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        int list_child_item_height = listItem.getMeasuredHeight()+listView.getDividerHeight();//item height
        totalHeight += list_child_item_height; //
    }

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

}
public void setListViewHeightBasedOnChildren(ListView ListView){
if(mAdapter==null)
返回;
int totalHeight=0;//它是ListView的高度
for(int i=0,len=mAdapter.getCount();i
但是使用这种方法,listView的高度并不是我想要的

我需要有适配器列表(每个适配器的高度可能每次都不同)完全显示,没有任何额外的滚动条

你知道吗

问候

请遵循以下步骤

列表视图

myList = (ListView) findViewById(R.id.listView);
              myList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listview_array));
              Helper.getListViewSize(myList);
myList=(ListView)findViewById(R.id.ListView);
setAdapter(新的ArrayAdapter(这个,android.R.layout.simple_list_item_1,listview_数组));
getListViewSize(myList);
助手类

public class Helper {
    public static void getListViewSize(ListView myListView) {
        ListAdapter myListAdapter = myListView.getAdapter();
        if (myListAdapter == null) {
            //do nothing return null
            return;
        }
        //set listAdapter in loop for getting final size
        int totalHeight = 0;
        for (int size = 0; size < myListAdapter.getCount(); size++) {
            View listItem = myListAdapter.getView(size, null, myListView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
      //setting listview item in adapter
        ViewGroup.LayoutParams params = myListView.getLayoutParams();
        params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
        myListView.setLayoutParams(params);
        // print height of adapter on log
        Log.i("height of listItem:", String.valueOf(totalHeight));
    }
}
公共类助手{
公共静态无效getListViewSize(ListView myListView){
ListAdapter myListAdapter=myListView.getAdapter();
if(myListAdapter==null){
//不执行任何操作返回空值
返回;
}
//在循环中设置listAdapter以获取最终大小
int totalHeight=0;
对于(int size=0;size