android根据屏幕高度改变item占ListView高度
首先要知道LayoutParams是什么。
利用LayoutParams可以更改每个item在listview中的样子。
// 调整每个Item占屏幕的百分比
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, mPullToRefreshListView.getHeight() / 5);
convertView.setLayoutParams(lp);
mPullToRefreshListView为我的listview控件。先获取到该listview在布局中所占的高度。然后除以我想每个页面显示item的个数,就得到了每个item的高度。在用convertView.setLayoutParams(lp);改变当前item在view中的样子。
注意:setLayoutParams必须用父控件的LayoutParams。例如该item的父控件为listview所以使用的是AbsListView。
下面贴上我的适配器的所有代码:
package com.adapter;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android_demo.R;
import com.bean.MaoyanBean;
import java.util.List;
import adaptation.AbViewUtil;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.bean.MaoyanBean.DataBean.MoviesBean;
import com.common.SystemApplication;
import com.pullToRefresh.PullToRefreshListView;
/**
* xlistview适配器
* XlistViewAdapter
*
* @author tianshi
* @time 2016/11/16 9:18
*/
public class XlistViewAdapter extends BaseAdapter {
private Activity mActivity;
private LayoutInflater mInflater;
private MaoyanBean mData;
private List list;
private PullToRefreshListView mPullToRefreshListView;
public XlistViewAdapter(Activity mActivity, PullToRefreshListView mPullToRefreshListView, MaoyanBean mData) {
this.mActivity = mActivity;
mInflater = LayoutInflater.from(mActivity);
this.mData = mData;
list = mData.getData().getMovies();
this.mPullToRefreshListView = mPullToRefreshListView;
}
public int getCount() {
return list.size();
}
public Object getItem(int position) {
return list.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_main02, null);
viewHolder = new ViewHolder(convertView);
// 调整每个Item占屏幕的百分比
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, mPullToRefreshListView.getHeight() / 5);
convertView.setLayoutParams(lp);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
//图片
SystemApplication.showImage(mActivity, list.get(position).getImg(), viewHolder.iv01);
//影片名
viewHolder.tv01.setText(list.get(position).getNm());
viewHolder.tv02.setText(list.get(position).getScm());
viewHolder.tv03.setText(list.get(position).getShowInfo());
return convertView;
}
/**
* 优化机制
*/
static class ViewHolder {
@BindView(R.id.iv_01)
ImageView iv01;
@BindView(R.id.tv_01)
TextView tv01;
@BindView(R.id.tv_02)
TextView tv02;
@BindView(R.id.tv_03)
TextView tv03;
ViewHolder(View view) {
ButterKnife.bind(this, view);
AbViewUtil.scaleContentView((LinearLayout) view.findViewById(R.id.rootLayout));
}
}
}