public void getData() { List<TravelPeopleEntity> dataList = new ArrayList<>(); for (int i = 1; i < 5; i++) { TravelPeopleEntity entity = new TravelPeopleEntity(); entity.setId(i); entity.setName("李娜娜"); entity.setCard("610402199206054213"); entity.setPhoneNumber("15319079845"); dataList.add(entity); } adapter = new TravelPeopleAdapter(this); adapter.setData(dataList); listView.setAdapter(adapter); ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = getHeight() + (listView.getDividerHeight() * (adapter.getCount() - 1)); ((ViewGroup.MarginLayoutParams) params).setMargins(0, 0, 0, 30); listView.setLayoutParams(params); }
public int getHeight() { int total = 0; for (int i = 0; i < adapter.getCount(); i++) { View listItem = adapter.getView(i, null, listView); listItem.measure(0, 0); total += listItem.getMeasuredHeight(); } return total; }