在scrollview中完全显示列表视图

时间:2015-02-17 09:26:49

标签: 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"
            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>

问题是适配器列表未完全显示。

我知道将ListView放在ScrollView中是不正确的,我已经找到了另一个建议使用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();

}

但使用此方法时,listView高度未按我的意愿设置。

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

你有什么想法吗?

此致

1 个答案:

答案 0 :(得分:0)

关注此link

列表视图

myList = (ListView) findViewById(R.id.listView);
              myList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listview_array));
              Helper.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));
    }
}