赞
踩
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);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
Log.d(TAG, "params.height: "+params.height);
listView.setLayoutParams(params);
}
public abstract class BaseController
{
public View mRootView;
public Context mContext;
public BaseController(Context context){
this.mContext = context;
// 在构造中 就加载显示的view
mRootView = initView(context);
}
/**
* 初始化view的方法让子类去实现
* @return
*/
protected abstract View initView(Context context);
/**
* 加载数据的方法,子类可以实现,也可以不实现
*/
public void initData(){
}
/**
* 暴露出去的获得根view的方法
* @return
*/
public View getRootView()
{
return mRootView;
}
}
public class RightController extends BaseController {
private static final String TAG = "RightController";
private ListView mListView;
private List<String> mDatas;
public RightController(Context context) {
super(context);
}
@Override
protected View initView(Context context) {
// TextView readView = new TextView(context);
// readView.setText("right-页面");
View rootView = View.inflate(context, R.layout.controller_right, null);
mListView = (ListView) rootView.findViewById(R.id.lv);
prepareData();
RightAdapter rightAdapter = new RightAdapter(context, mDatas);
mListView.setAdapter(rightAdapter);
setListViewHeightBasedOnChildren(mListView);
return rootView;
}
private void prepareData() {
mDatas = new ArrayList<>();
for (int i = 0; i < 20; i++) {
mDatas.add("item_" + i);
}
}
/**
* 在scrollview中完整显示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++) {
for (int i = 0; i < 10; 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() * 5);// (listAdapter.getCount() - 1));
Log.d(TAG, "params.height: "+params.height);
listView.setLayoutParams(params);
}
}
@Override
public
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。