简体   繁体   中英

Height of a listview

I have been trying to make a layout for my app, where I set the 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! 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. If there's any other View to do it, please suggest.

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. But it doesn't! How to fix this?

This is how it looks, even though the Listview has 10 items! The ListView is the one which has a heading as LAST 10 TRANSACTIONS

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

Why I used a 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.

What do you mean by " dynamically insert data into the Listview during runtime". If you are modifying list data dynamically then ListView can handle it internally. You no need to do anything in that case. So you can remove the ScrollView so it works fine.

You can not add a ListView inside ScrollView . For considering your needs you can set ListView height dynamically to fulfill your needs ... After setting up your list adapter simply call this method...

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)

But I have to dynamically insert data into the Listview during runtime, if there's any other way to do it, please suggest.

I can't see why you think you can't, and want to know why you think a ScrollView will do it.

If you have created your own adapter to fill that ListView you should have something like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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