Android list dynamic height

I have an ExpandableListview inside a ScrollView , and I know that this is not good, but I also had the only solution to show the entire list by setting its height using code using layoutParams

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams (LayoutParams.MATCH_PARENT, ListViewData.length ());

this solution is good, but I can’t determine the correct height that I have to specify in Params , SO is a way to find out the actual size from the size of the array

Edit: I came up with a solution that every time I expand the list group, I'm going to change the height to fit the new geometry

+4
source share
4 answers

try this, use child based listview. setListViewHeightBasedOnChildren() this will set the height of your listview child

  public class Utils { 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); listView.requestLayout(); } } 
+13
source

In this case, your ListView is not actually needed. . You can also iterate over adapter elements and simply add them to the vertical LinearLayout inside your ScrollView.

If you do not want to change a lot of code:

Replace ListView.setAdapter with

 LinearLayout ll; //this should be the vertical LinearLayout that you substituted the listview with for(int i=0;i<adapter.getCount();i++) { View v = adapter.getView(position, null, null); ll.addView(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); } 

If you already use the OnItemClickListener add-on after View v = adapter.getView(position, null, null); next

 final int position = i; v.setOnClickListener(new OnClickListener() { public onClick(View v) { yourOnItemClickListener.onItemClick(null, v, position, 0); } }); 

In this case, you do not need to worry about any errors in height.

+10
source

Try it, it works for my own problem.

 public static boolean setListViewHeightBasedOnItems(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter != null) { int numberOfItems = listAdapter.getCount(); // Get total height of all items. int totalItemsHeight = 0; for (int itemPos = 0; itemPos < numberOfItems; itemPos++) { View item = listAdapter.getView(itemPos, null, listView); item.measure(0, 0); totalItemsHeight += item.getMeasuredHeight(); } // Get total height of all item dividers. int totalDividersHeight = listView.getDividerHeight() * (numberOfItems - 1); // Set list height. ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalItemsHeight + totalDividersHeight; listView.setLayoutParams(params); listView.requestLayout(); return true; } else { return false; }} 

The requestLayout () method is called in the view because something has changed and its layout has been disabled - it redraws the forces.

+3
source

you can set a variable in the size file for different screen sizes, and then multiply it by the number of list items you want to display.

0
source

Source: https://habr.com/ru/post/1241386/


All Articles