scrollView嵌套listview内容显示不全的解决方法
自定义LinearLayoutForListView 继承LinearLayout
import urun.news.adapter.AdapterForLinearLayout;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
public class LinearLayoutForListView extends LinearLayout {
private AdapterForLinearLayout adapter;
private OnClickListener onClickListener = null;
/**
* 绑定布局
*/
public void bindLinearLayout() {
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
View v = adapter.getView(i, null, null);
v.setOnClickListener(this.onClickListener);
// if (i == count - 1) {
// LinearLayout ly = (LinearLayout) v;
// ly.removeViewAt(2);
// }
addView(v, i);
}
Log.v("countTAG", "" + count);
}
public LinearLayoutForListView(Context context) {
super(context);
}
public LinearLayoutForListView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
/**
* 获取Adapter
*
* @return adapter
*/
public AdapterForLinearLayout getAdpater() {
return adapter;
}
/**
* 设置数据
*
* @param adpater
*/
public void setAdapter(AdapterForLinearLayout adpater) {
this.adapter = adpater;
bindLinearLayout();
}
/**
* 获取点击事件
*
* @return
*/
public OnClickListener getOnclickListner() {
return onClickListener;
}
/**
* 设置点击事件
*
* @param onClickListener
*/
public void setOnclickLinstener(OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
}
自定义Adapter
import java.util.List;
import java.util.Map;
import urun.news.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class AdapterForLinearLayout extends BaseAdapter {
private LayoutInflater mInflater;
private String[] from;
public AdapterForLinearLayout(Context context, String[] from
) {
this.from = from;
this.mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return from.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return from[position];
}
@SuppressWarnings("unchecked")
public String get(int position, Object key) {
return from[position].toString();
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
convertView = mInflater.inflate(R.layout.newsdetails_item_revision, null);
TextView view=(TextView) convertView.findViewById(R.id.particulars_te_content);
bindView(view, from[position].toString());
convertView.setTag(position);
return convertView;
}
/**
* 绑定视图
* @param view
* @param item
* @param from
*/
private void bindView(View view, String from) {
if (view instanceof TextView) {
((TextView) view).setText(from == null ? "" : from.toString());
}
}
}
动态设置listview的高
public static void setListViewHeight(ListView lv) {
ListAdapter la = lv.getAdapter();
if (null == la) {
return;
}
// calculate height of all items.
int h = 0;
final int cnt = la.getCount();
for (int i = 0; i < cnt; i++) {
View item = la.getView(i, null, lv);
item.measure(0, 0);
h += item.getMeasuredHeight();
}
// reset ListView height
ViewGroup.LayoutParams lp = lv.getLayoutParams();
lp.height = h + (lv.getDividerHeight() * (cnt) - 1);
lv.setLayoutParams(lp);
}