[android]代码库
package com.example.day_16_demo;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
List<String> list=new ArrayList<String>();
for (int i = 0; i < 10000; i++) {
list.add("测试数据"+i);
}
//listView.setDividerHeight(Integer.MAX_VALUE);
Adapter adapter=new Adapter(list, MainActivity.this);
listView.setAdapter(adapter);
listView();
}
public void listView(){
//获取适配器
BaseAdapter adapter=(BaseAdapter) listView.getAdapter();
//判空
if(adapter==null){
return;
}
int num=0;
//获取适配器中条目总数
int count=adapter.getCount();
for (int i = 0; i <count; i++) {
//获取每个条目listItem
View listItem=adapter.getView(i, null, listView);
//计算每个条目listItem高度
listItem.measure(0, 0);
//计算所有条目的总高度
num=num+listItem.getMeasuredHeight();
}
//listView.getDividerHeight()获取条目间分隔符高度
//params.height获取整个listView总高度
ViewGroup.LayoutParams params=listView.getLayoutParams();
params.height=num+(listView.getDividerHeight()*(adapter.getCount()-1));
//设置高度
listView.setLayoutParams(params);
}
}
by: 发表于:2017-10-18 09:18:14 顶(0) | 踩(0) 回复
??
回复评论