Listview inside Listview (ANDROID)

Asked

Viewed 98 times

-1

I am developing an application where this one has an Activity where it generates a Listview of Orders, and within the Adapter of this list of orders, I have another Listview that are the items, ie I have a Listview within another Listview.

The problem is that Android does not let put a Listview inside a View that has Scrollview, and by default, a Listview already has Scroll.

To solve this, I found on internet forums a code that would go through the items of my daughter list, take the size of each line and so, adding a total size, but it didn’t work very well in my project, would anyone have another solution for this? thank you.

Code I found on the Internet:

public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
    return;

int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
    view = listAdapter.getView(i, view, listView);
    if (i == 0)
        view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));

    view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
    totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}

What’s going on:

inserir a descrição da imagem aqui

  • What is going on with this code that you used? Could you send a print of what is happening?

  • listView.isNestedScrollingEnabled = false

  • @BonecoSinforoso http://uploaddeimagens.com.br/images/002/142/423/full/print_erro.png?1560552933

  • @Eduardodornel did not resolve

1 answer

0

I managed to solve the problem with the code below!

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

    int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST);
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);

        if(listItem != null){
            // This next line is needed before you call measure or else you won't get measured height at all. The listitem needs to be drawn first to know the height.
            listItem.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
            listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
            totalHeight += listItem.getMeasuredHeight();

        }
    }

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.