3

The format of my XML file is as following:

 LinearLayout 

     ScrollView

         RelativeLayout

             LinearLayout

             <ListView
               android:id="@android:id/list"
               android:layout_width="match_parent"
               android:layout_height="fill_parent"
               android:layout_margin="5dip"
               android:layout_weight="30" >
             </ListView>

             LinearLayout

Here is my BaseAdapter class

public class APIQuickCheckoutProductProvider extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;

public APIQuickCheckoutProductProvider(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data = d;
    inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.product_item, null);

    TextView brand = (TextView) vi.findViewById(R.id.brands); 
    TextView quantity_name = (TextView) vi.findViewById(R.id.quantity_name); 

    TextView price = (TextView) vi.findViewById(R.id.price); 

    HashMap<String, String> product = new HashMap<String, String>();
    product = data.get(position);

    // Setting all values in listview
    brand.setText(product.get(APIQuickCheckout.KEY_BRAND));
    quantity_name.setText(product.get(APIQuickCheckout.KEY_QUANTITY_NAME));
    price.setText(product.get(APIQuickCheckout.KEY_PRICE));

    return vi;
}
}

Here is the main class where I set the adapter:

adapter = new APIQuickCheckoutProductProvider(APIQuickCheckout.this, productList);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();

And finally here is the each individual row which i set to the listview

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dip"
    android:layout_weight="7"
    android:weightSum="6" >

    <TextView
        android:id="@+id/brands"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="3" />

    <TextView
        android:id="@+id/quantity_name"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="3" />
  </LinearLayout>

  <LinearLayout
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dip"
    android:layout_weight="2"
    android:weightSum="6" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="3" />

    <TextView
        android:id="@+id/price"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="3" />
   </LinearLayout>

 </LinearLayout>

However, when I am trying the above mentioned code it displays the first product, and I can see only the top of the second product.And I can see nothing from then and on. So, how can I change the height of the ListView dynamically when I add a new product? In order to display all of them.

4

8 回答 8

17

If you want to change the height of list view dynamically, you could use,

list.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, theSizeIWant));
于 2012-12-24T12:01:24.623 回答
3
import android.view.ViewGroup.LayoutParams;

 ListView mListView = (ListView)   findViewById(R.id.listviewid);
 LayoutParams list = (LayoutParams) mListView.getLayoutParams();
   list.height = set the height acc to you;//like int  200
   mListView.setLayoutParams(list);
于 2013-07-24T08:02:39.493 回答
2

Try this answer,

public static void setListViewHeightBasedOnChildren(final ListView listView) {
    listView.post(new Runnable() {
        @Override
        public void run() {
            ListAdapter listAdapter = listView.getAdapter();
            if (listAdapter == null) {
                return;
            }
            int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
            int listWidth = listView.getMeasuredWidth();
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(
                        View.MeasureSpec.makeMeasureSpec(listWidth, View.MeasureSpec.EXACTLY),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));


                totalHeight += listItem.getMeasuredHeight();
                Log.d("listItemHeight" + listItem.getMeasuredHeight(), "___________");
            }
            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = (int) ((totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1))));
            listView.setLayoutParams(params);
            listView.requestLayout();

        }
    });
   }
于 2015-12-10T12:14:34.320 回答
1

Its not recommended to use ListView inside of a ScrollView

于 2012-12-24T11:42:12.913 回答
1

You can use below method to set the height of listview programmatically:

<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);
于 2016-02-22T11:53:56.343 回答
0

this class can be use to expand the height of listview or gridview at runtime ,please put it in your package and use by passing listview

public class CustomView {

        public static void getListViewSize(ListView myListView) {
            ListAdapter myListAdapter = myListView.getAdapter();
            if (myListAdapter == null) {
                //do nothing return null
                return;
            }
            //set listAdapter in loop for getting final size
            int totalHeight = 0;
            for (int size = 0; size < myListAdapter.getCount(); size++) {
                View listItem = myListAdapter.getView(size, null, myListView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
            //setting listview item in adapter
            ViewGroup.LayoutParams params = myListView.getLayoutParams();
            params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
            myListView.setLayoutParams(params);
            // print height of adapter on log
            Log.i("height of listItem:", String.valueOf(totalHeight));
        }



        public static void getGridViewSize(GridView myGridView) {
            ListAdapter myListAdapter = myGridView.getAdapter();
            if (myListAdapter == null) {
                //do nothing return null
                return;
            }
            //set listAdapter in loop for getting final size
            int totalHeight = 0;
            int maxHeight = 0;//implement new 
            for (int size = 0; size < myListAdapter.getCount(); size++) {
                View listItem = myListAdapter.getView(size, null, myGridView);

                listItem.measure(0, 0);

                totalHeight += listItem.getMeasuredHeight()+8;

                if(maxHeight < listItem.getMeasuredHeight()){//implement new 

                    maxHeight = listItem.getMeasuredHeight()+8;
                    System.out.println(" max height of gridview item<11>  "+size+"  ==  "+maxHeight);
                }

                System.out.println(myListAdapter.getCount()+"  height of gridview item<22>  "+size+"  ==  "+listItem.getMeasuredHeight());
            }

            //setting listview item in adapter
             ViewGroup.LayoutParams params = myGridView.getLayoutParams();

            if(myListAdapter.getCount() > 1)
                params.height = maxHeight * ((myListAdapter.getCount()+1)/2);//implement new
            else
                params.height = maxHeight;//implement new

             myGridView.setLayoutParams(params);

        }


    }
于 2016-03-16T15:13:31.750 回答
0
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();
    }
}
于 2016-05-25T14:16:19.217 回答
0

 <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <ListView
                android:id="@+id/lv_premimum_upgrade"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/custom_margin"
                android:minHeight="48dp">
            </ListView>
        </LinearLayout> 

</ScrollView>

int list_cell_size=188;    // cell_size is varible 
ViewGroup.LayoutParams list;
list = (ViewGroup.LayoutParams) listView.getLayoutParams();
list.height = list_cell_size*response.size();  // response is list items size
listView.setLayoutParams(list);

于 2017-04-12T09:51:45.730 回答