3

私のXMLファイルの形式は次のとおりです。

 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

ここに私の BaseAdapter クラスがあります

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;
}
}

アダプターを設定するメインクラスは次のとおりです。

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

そして最後に、リストビューに設定した個々の行があります

<?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>

しかし、上記のコードを試してみると、最初の製品が表示され、2 番目の製品の上部しか表示されず、それ以降は何も表示されません。ListViewでは、新しい製品を追加するときに動的に高さを変更するにはどうすればよいですか? それらのすべてを表示するために。

4

8 に答える 8

17

リストビューの高さを動的に変更する場合は、次を使用できます。

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

この答えを試して、

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

以下のメソッドを使用して、リストビューの高さをプログラムで設定できます。

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

リストビューの高さを設定するために使用されるメソッド:

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;
        }

    }

次に、以下のコードのように設定します。

 MenuAdapter menuAdapter = new MenuAdapter(context, R.layout.menu_item);
            lvMenu.setAdapter(menuAdapter);
            setListViewHeightBasedOnItems(lvMenu);
于 2016-02-22T11:53:56.343 に答える
0

このクラスは、実行時にリストビューまたはグリッドビューの高さを拡張するために使用できます。パッケージに入れ、リストビューを渡して使用してください

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 に答える