解决listview与scrollview结合使用时出现的问题

这是我最近遇到的问题,想实现全屏滚动大家都知道在最外层嵌套一个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 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);这个点击事件类似于listview的itemOnClick事件。需要注意的是这个事件里面你需要用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);//设置适配器

试一下吧


原文出自:http://www.eoeandroid.com/thread-25319-1-1.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值