解决listview与scrollview结合使用时出现的问题
2012-07-20 15:55
597 查看
这是我最近遇到的问题,想实现全屏滚动大家都知道在最外层嵌套一个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。
好了,一切准备就绪,我们就把他们组合起来用一下来得到我们想要的结果吧。用法很简单:首先我们要先把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
我们可以重写一个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
相关文章推荐
- 解决listview与scrollview结合使用时出现的问题
- 解决listview与scrollview结合使用时出现的问题
- ScrollView和ListView综合使用出现的问题解决方法
- 解决ListView和ScrollView同时使用时滑动的冲突问题
- Android ExpandableListview+Listview嵌套使用出现的问题解决办法
- ScrollView包含ListView出现的问题解决
- ListView嵌入ScrollView中出现的问题及解决方法
- 使用scrollview嵌套listview或gridview后出现scrollview乱滚动的问题
- 解决SwipeRefreshLayout结合ListView EmptyView使用不起作用的问题
- listview 放入 scrollview出现显示问题的解决方法
- ScrollView和ListView一起使用,解决ListView只显示一行问题!
- ScrollView和ListView一起使用 文本换行getMeasuredHeight等问题的解决
- 解决Android使用ScrollView和 ListView时底部空间随着输入法向上移动的问题
- volley+listView结合使用时出现的问题以及解决方案
- ScrollView与WebView结合使用时,设置webview字体大小的时候出现下方留白解决
- png库结合zlib库使用出现的一个链接问题的解决
- png库结合zlib库使用出现的一个链接问题的解决
- 解决SwipeRefreshLayout结合ListView EmptyView使用不起作用的问题