主要方法(此方法是用来计算item高度) 在listview 设置完适配器后 调用此方法 参数就是listview
/*** * 动态设置listview的高度 * @param listView */ public 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); //在还没有构建View 之前无法取得View的度宽。在此之前我们必须选 measure 一下. totalHeight += listItem.getMeasuredHeight(); //Log.i("xxxxxxxx listItem xxxxxxxxx", i + " height : " + totalHeight); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); // listView.getDividerHeight()获取子项间分隔符占用的高度 listView.setLayoutParams(params); }
示例代码
布局文件
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.rk_six.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content"></ListView> </LinearLayout> </ScrollView>
item布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginLeft="15dp" android:layout_marginStart="15dp" android:layout_marginTop="23dp" android:text="TextView"/> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView" android:layout_marginLeft="52dp" android:layout_marginStart="52dp" android:layout_toEndOf="@+id/textView" android:layout_toRightOf="@+id/textView" android:text="TextView"/> </RelativeLayout>
Main方法
package com.example.rk_six; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ListView mListView; private List<Bean>list = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.listview); for (int i = 0;i<100;i++){ Bean bean = new Bean(); bean.setAge("年龄"+i); bean.setName("路人甲"+i+"号"); list.add(bean); } mListView.setAdapter(new MyAdapter()); setListViewHeightBasedOnChildren(mListView); } /*** * 动态设置listview的高度 * @param listView */ public 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); //在还没有构建View 之前无法取得View的度宽。在此之前我们必须选 measure 一下. totalHeight += listItem.getMeasuredHeight(); //Log.i("xxxxxxxx listItem xxxxxxxxx", i + " height : " + totalHeight); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); // listView.getDividerHeight()获取子项间分隔符占用的高度 listView.setLayoutParams(params); } private class MyAdapter extends BaseAdapter { @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView==null){ convertView =View.inflate(MainActivity.this,R.layout.item,null); holder = new ViewHolder(); holder.t1 = (TextView) convertView.findViewById(R.id.textView); holder.t2 = (TextView) convertView.findViewById(R.id.textView2); convertView.setTag(holder); }else { holder = (ViewHolder) convertView.getTag(); } holder.t1.setText(list.get(position).getName()); holder.t2.setText(list.get(position).getAge()); return convertView; } } class ViewHolder{ TextView t1,t2; } }
Bean 类
package com.example.rk_six; /** * Created by 1 on 2017/6/13. */ public class Bean { private String name; private String age; public Bean() { } public Bean(String name, String age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }