How to change ListView height dynamically in Android?

前端 未结 7 853
小鲜肉
小鲜肉 2020-12-03 02:19

I need to change the height of a ListView dynamically in my app.

Is there any way to do that?

相关标签:
7条回答
  • 2020-12-03 02:37

    for change height and width dynamically so, try this

    RelativeLayout.LayoutParams mParam = new RelativeLayout.LayoutParams((int)(width),(int)(height);
            listView.setLayoutParams(mParam);
    

    you can also use LayoutParams.FILL_PARENT or LayoutParams.WRAP_CONTENT instead of height & width

    0 讨论(0)
  • This piece of code helped me to achieve dynamic listview height.

    import android.view.View;
    import android.view.ViewGroup;
    import android.view.View.MeasureSpec;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    
    public class Utility {
        public static void setListViewHeightBasedOnChildren(ListView listView) {
            ListAdapter listAdapter = listView.getAdapter();
            if (listAdapter == null) {
                // pre-condition
                return;
            }
    
            int totalHeight = 0;
            int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
                totalHeight += listItem.getMeasuredHeight();
            }
    
            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
            listView.setLayoutParams(params);
            listView.requestLayout();
        }
    }
    

    In the main Activity use this Utility class to change the listview height.

     phraseListView=(ListView)findViewById(R.id.phrase_listview);
     phraseAdapter=new PhraseListAdapter(this);
     phraseListView.setAdapter(phraseAdapter);
     Utility.setListViewHeightBasedOnChildren(phraseListView);
    

    Taken from here

    0 讨论(0)
  • 2020-12-03 02:41

    You can use below method to set the height of listview programmatically as per your items:

    <ListView
            android:id="@+id/lvMenu"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffff"
            />
    

    Method used for setting height of listview:

    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);
                    float px = 500 * (listView.getResources().getDisplayMetrics().density);
                    item.measure(View.MeasureSpec.makeMeasureSpec((int)px, View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                    totalItemsHeight += item.getMeasuredHeight();
                }
    
                // Get total height of all item dividers.
                int totalDividersHeight = listView.getDividerHeight() *
                        (numberOfItems - 1);
                // Get padding
                int totalPadding = listView.getPaddingTop() + listView.getPaddingBottom();
    
                // Set list height.
                ViewGroup.LayoutParams params = listView.getLayoutParams();
                params.height = totalItemsHeight + totalDividersHeight + totalPadding;
                listView.setLayoutParams(params);
                listView.requestLayout();
                return true;
    
            } else {
                return false;
            }
    
        }
    

    Then set it like below code:

    MenuAdapter menuAdapter = new MenuAdapter(context, R.layout.menu_item);
                lvMenu.setAdapter(menuAdapter);
                setListViewHeightBasedOnItems(lvMenu);
    

    Hope it will help you.

    0 讨论(0)
  • 2020-12-03 02:49

    Personaly I spent so much time trying to find a solution, turns out all I had to do is to set my listview's height to wrap_content.

    0 讨论(0)
  • 2020-12-03 02:50

    use customized listview and give height as wrap_content.

    0 讨论(0)
  • 2020-12-03 02:51

    First thing to keep in mind is that while getting height of listview using mylistview.getmeasureheight, it always gives height according to the textview/View inside the list which was set during initializing of adapter. So we need to declare the adapter as follows:

    myadapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myarraylist){
            @NonNull
            @Override
            public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                TextView textView = ((TextView) view.findViewById(android.R.id.text1));
                textView.setMinHeight(0); // Min Height
                textView.setMinimumHeight(0); // Min Height
                textView.setHeight(100); // Height
                return view;
            }
        };
    

    Now height is fixed to 100. Note that it's the height of text and not the view itself. So keep it in mind when adding padding. Now set the Adapter:

    mylist.setAdapter(myadapter);
    

    Now after every time you add an item to your list call a function as follows:

    myarraylist.add(ds.getKey());
    myadapter.notifyDataSetChanged();
    mylist.setAdapter(myadapter);
    
    setListViewHeightBasedOnItems(mylist);
    

    define the function in same class as:

    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);
                float px = 500 * (listView.getResources().getDisplayMetrics().density);
                item.measure(View.MeasureSpec.makeMeasureSpec((int)px, View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                totalItemsHeight += item.getMeasuredHeight();
            }
    
            // Get total height of all item dividers.
            int totalDividersHeight = listView.getDividerHeight() *
                    (numberOfItems - 1);
            // Get padding
            int totalPadding = listView.getPaddingTop() + listView.getPaddingBottom();
    
            // Set list height.
            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalItemsHeight + totalDividersHeight + totalPadding;
            listView.setLayoutParams(params);
            listView.requestLayout();
            return true;
    
        } else {
            return false;
        }
    
    }
    

    You can also check if height is set or not correctly by returning true/false. This solution worked for me. Hope this Helps!!!!!

    0 讨论(0)
提交回复
热议问题