android.widget.ListView.getAdapter()方法的使用及代码示例

x33g5p2x  于2022-01-23 转载在 其他  
字(11.6k)|赞(0)|评价(0)|浏览(230)

本文整理了Java中android.widget.ListView.getAdapter()方法的一些代码示例,展示了ListView.getAdapter()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ListView.getAdapter()方法的具体详情如下:
包路径:android.widget.ListView
类名称:ListView
方法名:getAdapter

ListView.getAdapter介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

/**** Method for Setting the Height of the ListView dynamically.
 **** Hack to fix the issue of not showing all the items of the ListView
 **** when placed inside a ScrollView  ****/
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);
}

代码示例来源:origin: stackoverflow.com

private static final int UNBOUNDED = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

// To calculate the total height of all items in ListView call with items = adapter.getCount()
public static int getItemHeightofListView(ListView listView, int items) {
  ListAdapter adapter = listView.getAdapter();

  int grossElementHeight = 0;
  for (int i = 0; i < items; i++) {
    View childView = adapter.getView(i, null, listView);
    childView.measure(UNBOUNDED, UNBOUNDED);
    grossElementHeight += childView.getMeasuredHeight();
  }
  return grossElementHeight;
}

代码示例来源:origin: jingle1267/android-utils

/**
 * get ListView height according to every children
 *
 * @param view
 * @return
 */
public static int getListViewHeightBasedOnChildren(ListView view) {
  int height = getAbsListViewHeightBasedOnChildren(view);
  ListAdapter adapter;
  int adapterCount;
  if (view != null && (adapter = view.getAdapter()) != null
      && (adapterCount = adapter.getCount()) > 0) {
    height += view.getDividerHeight() * (adapterCount - 1);
  }
  return height;
}

代码示例来源:origin: stackoverflow.com

ListView listView = (ListView) findViewById(R.id.listView);
 String[] names = new String[] {"hi", "my", "name", "is", "slim", "shady"};
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, names);
 listView.setAdapter(adapter);
 View row = listView.getAdapter().getView(0, null, listView);
 row.measure(0, 0);
 int rowHeight = row.getMeasuredHeight();
 listView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, rowHeight*3));

代码示例来源:origin: stackoverflow.com

private int calculateHeight(ListView list) {

  int height = 0;

  for (int i = 0; i < list.getCount(); i++) {
    View childView = list.getAdapter().getView(i, null, list);
    childView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    height+= childView.getMeasuredHeight();
  }

  //dividers height
  height += list.getDividerHeight() * list.getCount();

  return height;
}

代码示例来源:origin: stackoverflow.com

public void onShow(DialogInterface alert) {
  ListView listView = ((AlertDialog)alert).getListView();
  final ListAdapter originalAdapter = listView.getAdapter();
      return originalAdapter.getCount();
      View view = originalAdapter.getView(position, convertView, parent);
      TextView textView = (TextView)view;
      textView.setTypeface(MyFontUtil.getTypeface(MyActivity,MY_DEFAULT_FONT));

代码示例来源:origin: stackoverflow.com

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

    int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();

    for (int i = 0; i < listAdapter.getCount(); i++) {
      View listItem = listAdapter.getView(i, null, listView);
      if (listItem instanceof ViewGroup) {
        listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
       }

       listItem.measure(0, 0);
       totalHeight += listItem.getMeasuredHeight();
    }

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

代码示例来源:origin: stackoverflow.com

ListAdapter adapter  = listview.getAdapter(); 
int itemscount       = adapter.getCount();
int allitemsheight   = 0;
List<Bitmap> bmps    = new ArrayList<Bitmap>();
  View childView      = adapter.getView(i, null, listview);
  childView.measure(MeasureSpec.makeMeasureSpec(listview.getWidth(), MeasureSpec.EXACTLY), 
      MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
  childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
  childView.setDrawingCacheEnabled(true);
  childView.buildDrawingCache();
  bmps.add(childView.getDrawingCache());
  allitemsheight+=childView.getMeasuredHeight();

代码示例来源:origin: stackoverflow.com

ListView lv_marca; 
 lv_marca.setAdapter(adapter_marca);
 int list_height = getListViewHeight(lv_marca);
 private int getListViewHeight(ListView list) {
    ListAdapter adapter = list.getAdapter();
    int listviewHeight = 0;
    list.measure(MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED), 
           MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    listviewHeight = list.getMeasuredHeight() * adapter.getCount() + (adapter.getCount() * list.getDividerHeight());
    return listviewHeight;
 }

代码示例来源:origin: stackoverflow.com

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

代码示例来源:origin: stackoverflow.com

ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
           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);
    firstHeight = listItem.getMeasuredHeight();
         (LinearLayout.LayoutParams)llMain.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() *
                    (listAdapter.getCount() - 1));
llMain.setLayoutParams(params);
anotherView.requestLayout();

代码示例来源:origin: stackoverflow.com

public static void getTotalHeightofListView(ListView listView) {

  ListAdapter mAdapter = listView.getAdapter();

  int totalHeight = 0;

  for (int i = 0; i < mAdapter.getCount(); i++) {
    View mView = mAdapter.getView(i, null, listView);

    mView.measure(
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),

        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

    totalHeight += mView.getMeasuredHeight();
    Log.w("HEIGHT" + i, String.valueOf(totalHeight));

  }

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

}

代码示例来源:origin: stackoverflow.com

public boolean setListViewHeight(ListView listView) {
  ListAdapter listAdapter = listView.getAdapter();
  if (listAdapter != null) {

    int numberOfItems = listAdapter.getCount();

    View item = listAdapter.getView(0, null, listView);
    item.measure(0, 0);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, numberOfItems * item.getMeasuredHeight());
    listView.setLayoutParams(params);

    return true;

  } else {
    return false;
  }
}

代码示例来源:origin: JackyAndroid/AndroidTVLauncher

/**
 * @param listView
 * @author sunglasses
 * @category 计算listview高度
 */
public static void setListViewHeightBasedOnChildren(ListView listView) {
  ListAdapter listAdapter = listView.getAdapter();
  if (listAdapter == null) {
    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);
}

代码示例来源:origin: stackoverflow.com

public static void setListViewHeightBasedOnChildren(ListView listView) {
  ListAdapter listAdapter = listView.getAdapter(); 
  int totalHeight = 0;
  int listWidth = listView.getMeasuredWidth();
  for (int i = 0; i < listAdapter.getCount(); i++) {
    View listItem = listAdapter.getView(i, null, listView);
    listItem.measure(
      MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
      MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    totalHeight += listItem.getMeasuredHeight();
  }
  // ...update list height
}

代码示例来源:origin: stackoverflow.com

public static void justifyListViewHeightBasedOnChildren (ListView listView) {

  ListAdapter adapter = listView.getAdapter();

  if (adapter == null) {
    return;
  }
  ViewGroup vg = listView;
  int totalHeight = 0;
  for (int i = 0; i < adapter.getCount(); i++) {
    View listItem = adapter.getView(i, null, vg);
    listItem.measure(0, 0);
    totalHeight += listItem.getMeasuredHeight();
  }

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

代码示例来源:origin: stackoverflow.com

private  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();
 }

代码示例来源:origin: stackoverflow.com

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

代码示例来源:origin: stackoverflow.com

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

代码示例来源:origin: stackoverflow.com

public static void setListViewHeightBasedOnChildren(ListView listView) {
  ListAdapter listAdapter = listView.getAdapter();
  if (listAdapter == null) {
    return;
  }
  int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
  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);
  listView.requestLayout();
}

相关文章

ListView类方法