ScrollView套嵌ListView

这是我最近遇到的问题,想实现全屏滚动大家都知道在最外层嵌套一个scrollview,但是如果里面有listview就操蛋了,listview只能显示大约两行的大小。我找了找资料,解决的办法一种是:不要在scrollview里嵌套listview(我操,你这不废话么,老子的设计就是这样的,能怎么办?这种回答纯属欠抽),第二种办法是重写listview的onMeasure重新计算显示行数,还有一种办法是给listview加上头和尾,即调用丫的addHeaderView和addFooterView,这种办法也是不错滴,不过如果你的listview想实现某种背景效果的话这种办法就做不到了。哪哪,现在我就推出我的终极解决办法:也就是第一种欠抽的办法,既然listview不能用,那就不用listview了,用一个linearlayout,然后调用addView的方法来解决之。

我们可以重写一个adapter,然后重写一个linearlayout,就像listview调用adapter一样一样的。用adapter还是主要用它里面的getView和bindview。然后在自定义的linearlayout里自己写个方法循环的把子项添加到当前的linearlayout就搞定了。下面,贴代码:

首先是:XML也就是我们要用到的linearlayout。

<com.bengxin.utils.LinearLayoutForListView

android:id=”@+id/list_myself” android:orientation=”vertical”

android:layout_height=”wrap_content” android:layout_width=”fill_parent”

android:layout_gravity=”center_horizontal” android:background=”@drawable/list_bg”

android:paddingTop=”3dp” android:paddingBottom=”3dp” />

<com.bengxin.utils.LinearLayoutForListView   这个东西就是自己写的linearlayout,注意一下路径名称就行了。接下来我们看一下自定义的这个linearlayoutforlistview是怎么写的。

public class LinearLayoutForListView extends LinearLayout {//当然为了省事我们直接继承LinearLayout就行了

private AdapterForLinearLayout adapter;

private OnClickListener onClickListener = null;

//注意这两个构造函数一定要继承,否则后果自负

public LinearLayoutForListView(Context context) {

super(context);

}

public LinearLayoutForListView(Context context, AttributeSet attrs) {

super(context, attrs);

}

//我们把所有的子项加到当前这个linearlayout中去,注意一下,这里面要为每个子项添加上触发事件,我这里只是简单的实现功能,异常处理什么的统统没加

public void bindLinearLayout() {

for (int i = 0; i < adapter.getCount(); i++) {

View v = adapter.getView(i, null, null);

v.setOnClickListener(this.onClickListener);

this.addView(v, i);

}

}

//下面的这四个就不用解释了,get,set方法

public AdapterForLinearLayout getAdapter() {

return adapter;

}

public void setAdapter(AdapterForLinearLayout adapter) {

this.adapter = adapter;

bindLinearLayout();

}

public OnClickListener getOnClickListener() {

return onClickListener;

}

public void setOnClickListener(OnClickListener onClickListener) {

this.onClickListener = onClickListener;

}

好了,我们的linearlayout就准备好了,接下来我们就该准备适配器adapter了。前面说了,我们主要是用到adapter的getView和bindview,那么好,我们就重写一下这两个方法就是了。看代码:

public class AdapterForLinearLayout extends BaseAdapter {//二话不说,继承先

private LayoutInflater mInflater;

private int resource;

private List<? extends Map<String, ?>> data;

private String[] from;

private int[] to;

//这个我是仿照simpleAdapter搞的

public AdapterForLinearLayout(Context context,

List<? extends Map<String, ?>> data,int resource, String[] from,

int[] to) {

this.data = data;

this.resource = resource;

this.from = from;

this.to = to;

this.mInflater = LayoutInflater.from(context);

}

public int getCount() {

return data.size();

}

public Object getItem(int position) {

return data.get(position);

}

public long getItemId(int position) {

return position;

}

//亮点在这里,getView方法呈现

@SuppressWarnings(“unchecked”)

public View getView(int position, View convertView, ViewGroup parent) {

convertView = mInflater.inflate(this.resource, null);

Map item = data.get(position);

int count = to.length;

for (int i = 0; i < count; i++) {

View v = convertView.findViewById(to);

bindView(v, item, from);

}

convertView.setTag(position);//一定要注意这个东西,这个可是我们判断哪一个子项被点击的重要依据,你可以往里面set任何对象的

return convertView;

}

//接下来是bindview,我在这里面实现了imageview的功能,simpleAdapter没有的东西哟

@SuppressWarnings(“unchecked”)

private void bindView(View v, Map item, String from) {

Object data = item.get(from);

if (v instanceof Checkable) {

if (data instanceof Boolean) {

((Checkable) v).setChecked((Boolean) data);

} else {

throw new IllegalStateException(v.getClass().getName()

+ ” should be bound to a Boolean, not a “

+ data.getClass());

}

} else if (v instanceof TextView) {

((TextView) v).setText(data == null ? “” : data.toString());

} else if (v instanceof ImageView) {

if (data != null && !data.equals(“”)) {

if (data instanceof Integer) {

((ImageView) v).setImageResource((Integer) data);

} else {

Bitmap bm = ImageCache.getInstance().get(data.toString(),//这方法是自己写的

(ImageView) v);

((ImageView) v).setImageBitmap(bm);

((ImageView) v).setPadding(2, 2, 2, 2);

}

}

} else {

throw new IllegalStateException(v.getClass().getName()

+ ” is not a “

+ ” view that can be bounds by this SimpleAdapter”);

}

}
}

好了,一切准备就绪,我们就把他们组合起来用一下来得到我们想要的结果吧。用法很简单:首先我们要先把linearlayout弄出来呀,
声明:private LinearLayoutForListView mListView = null;然后获取:mListView = (LinearLayoutForListView) findViewById(R.id.list_myself);
给丫声明一个点击事件mListView.setOnClickListener(testListener);这个点击事件类似于listviewitemOnClick事件。需要注意的是这个事件里面你需要用view.getTag()来获取你在adapter里设置的东西来判断。类似:int id = Integer.parseInt(v.getTag().toString());

好了,万事大吉祥如意。一切搞定,运行看效果吧。哦,对了,把adapter忘了,
adapter = new AdapterForLinearLayout(ListViewTest.this,

(List<? extends Map<String, ?>>) res, resource//这个是你子项的layout,from, to);
调用的时候一定要注意顺序如下:mListView = (LinearLayoutForListView) findViewById(R.id.list_hotcity);//声明

mListView.setOnClickListener(itemClickListener);//点击事件
mListView.setAdapter(adapter);//
设置适配器

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注