简体   繁体   English

列表视图的高度

[英]Height of a listview

I have been trying to make a layout for my app, where I set the android:layout_height="wrap_content" . 我一直在尝试为我的应用设置布局,在其中设置android:layout_height="wrap_content"

This is my code :: 这是我的代码::

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.hp.money.DashBoard">

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

        <android.support.v7.widget.CardView
            android:id="@+id/cv_amount_display"
            android:layout_width="match_parent"
            android:layout_height="200dp">
           .....
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            card_view:cardCornerRadius="10dp">

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

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/last_10_transaction" />

                <ListView
                    android:id="@+id/last_transactions_lv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" />
            </LinearLayout>
        </android.support.v7.widget.CardView>
    </LinearLayout>
</ScrollView>

I am using a ListView inside a ScrollView, which is a bad idea, I know! 我在ScrollView中使用ListView,这是一个坏主意,我知道! It leads to some scrolling issues, I believe. 我认为,这会导致一些滚动问题。 But I have to dynamically update data on my screen during runtime, and I think only the listview can handle it. 但是我必须在运行时动态更新屏幕上的数据,而且我认为只有listview可以处理它。 If there's any other View to do it, please suggest. 如果还有其他View可以使用,请提出建议。

Now, the problem is that even if the Data source of the ListView has 10 items, the height of the ListView always remains equal to the size of one listView element, but the height of the listview is set to wrap_content , so probably it is supposed to resize accordingly. 现在的问题是,即使ListView的数据源有10个项目,ListView的高度始终保持等于一个listView元素的大小,但是listview的高度设置为wrap_content ,所以很可能应该相应地调整大小。 But it doesn't! 但事实并非如此! How to fix this? 如何解决这个问题?

This is how it looks, even though the Listview has 10 items! 即使Listview有10个项目,外观也是这样! The ListView is the one which has a heading as LAST 10 TRANSACTIONS ListView是标题为“最后10个交易”的那个

ListView是标题为“最后10个交易”的那个

Why I used a ScrollView?? 为什么我使用ScrollView?

I put a scrollview, because, the number of enteries in the listview can be many, so I want that when the user scrolls to see the entries not currently visible int the listview, the whole page gets scrolled, and not only the listview. 我放置了一个scrollview,因为listview中的enteries可以很多,所以我希望当用户滚动以查看listview中当前不可见的条目时,整个页面都会滚动,而不仅仅是listview。

What do you mean by " dynamically insert data into the Listview during runtime". 您的意思是“在运行时将数据动态地插入到Listview中”。 If you are modifying list data dynamically then ListView can handle it internally. 如果要动态修改列表数据,则ListView可以在内部处理它。 You no need to do anything in that case. 在这种情况下,您无需执行任何操作。 So you can remove the ScrollView so it works fine. 因此,您可以删除ScrollView ,使其工作正常。

You can not add a ListView inside ScrollView . 您不能在ScrollView内添加ListView。 For considering your needs you can set ListView height dynamically to fulfill your needs ... After setting up your list adapter simply call this method... 为了考虑您的需求,您可以动态设置ListView的高度以满足您的需求。设置列表适配器后,只需调用此方法即可。

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);
    }

As others pointed, you can't put a ListView inside a ScrollView (anyways it doesn't make sense, ListView has its own scroll) 正如其他人指出的那样,您不能将ListView放在ScrollView内(无论如何,ListView有自己的滚动条)

But I have to dynamically insert data into the Listview during runtime, if there's any other way to do it, please suggest. 但是我必须在运行时动态地将数据插入到Listview中,如果还有其他方法可以这样做,请提出建议。

I can't see why you think you can't, and want to know why you think a ScrollView will do it. 我看不到为什么您认为自己做不到,并且想知道为什么您认为ScrollView可以做到。

If you have created your own adapter to fill that ListView you should have something like this: 如果您创建了自己的适配器来填充该ListView,则应该具有以下内容:

class listViewAdapter extends BaseAdapter
{
     public View getView(int position, View convertView, ViewGroup parent) 
     {
     }
     public int getCount()
     {
     }
     public Object getItem(int position) {
     }
     public long getItemId(int position){
     }
}

Then you should create a data field and a setter 然后,您应该创建一个数据字段和一个setter

private Arraylist<DataType> data;

public void setData(Arraylist<DataType> data)
{
    data = data;
}

Then you can use it like this: 然后,您可以像这样使用它:

ListView lv = findViewById...
ListViewAdapter adapter = new ListViewAdapter();
adapter.setData(data);
lv.setAdapter(new ListViewAdapter());

When you have added or deleted data you can reload it by calling 添加或删除数据后,可以通过调用重新加载数据

adapter.notifySetDataChanged();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM