ScrollView中嵌套GridView,Listview的办法

按照android的标准,ScrollView中是不能嵌套具有滑动特性的View的。

在SrollView中嵌套ListView,ListView的问题:
1,在SrollView中嵌套ListView,ListView的显示会有问题,只显示一行或显示效果与预期不同,这是因为android禁止这样使用,放入ScrollView中的ListView的高度是无法计算的
2,嵌套中的子ListView和GridvIew是无法滑动的,因为子控件的滑动事件会被外面的ScrollView吃掉,如果想让子控件可以滑动,只能强行的截取滑动的相关事件了。
3, 点击GridView中某个item时,页面会滑动到最上端
解决方案:
1,第一种方案,也是我比较推荐的方案,就是重写ListView与GridView,让其失去滑动特性:

首先让子控件的内容全部显示出来,禁用了它的滚动。如果超过了父控件的范围则显示父控件的scrollbar滚动显示内容

public class NoScrollGridView extends GridView{

     public NoScrollGridView(Context context, AttributeSet attrs){
          super(context, attrs);
     }
//设置不滚动,onMeasure函数决定了组件显示的高度与宽度:
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
          int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
          super.onMeasure(widthMeasureSpec, mExpandSpec);
     }
}
makeMeasureSpec函数中第一个参数决定布局空间的大小,第二个参数是布局模式, MeasureSpec.AT_MOST的意思就是子控件需要多大的控件就扩展到多大的空间, 之后在ScrollView中添加这个组件就OK了,同样的道理,ListView也适用。
public class NoScrollListView extends ListView{ public NoScrollListView(Context context, AttributeSet attrs){ super(context, attrs); } public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, mExpandSpec); } }

2,第二种方案,也是网上流行的一种解决办法,人工计算子控件的尺寸,解决办法:

listview.setAdapter()之后调用Utility.setListViewHeightBasedOnChilren(listview)就Okay 了。

原理就是: 设置完ListView的Adapter后,根据ListView的子项目重新计算ListView的高度,然后把高度再作为LayoutParams设置给ListView,这样它的高度就正确了,通过人工算取控件的应有高度,再设置给ListView。
注意:这个方案中 子ListView的每个Item必须是LinearLayout,不能是其他的,因为其他的Layout(如RelativeLayout)没有重写onMeasure(),所以会在onMeasure()时抛出异常

问题3 solution

(1)、通过监听scrollview的OnTouchListener来得到滑动的X、Y.

sv = (ScrollView) this.findViewById(R.id.scrollView1);
sv.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_UP){

//可以监听到ScrollView的滚动事件
scrollY = sv.getScrollY();
scrollX = sv.getScrollX();
}
return false;
}
});

在gridview的onItemSelectListener中设置scroll的位置:

business_gv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) {
businessClickCount ++ ;
busPackAdpater.changeState(position);
if(businessClickCount == 1){
business_gv.requestFocus();
mHandler.post(new Runnable() {
@Override
public void run() {
sv.scrollTo(scrollX, scrollY);
}
});

}
selectBusinessPosition = position;
}
});

ScrollView嵌套ListView只显示一行的问题

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: