您的位置:首页 > 移动开发 > Android开发

android中禁止GridView上下滑动的方法

2012-09-21 15:23 459 查看
1,定义一个类继承GridView,如下:

public class SourcePanel extends GridView {

public SourcePanel(Context context) {

super(context);

}

public SourcePanel(Context context, AttributeSet attrs) {

super(context, attrs);

}

public SourcePanel(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

}

}

注意:构造方法要将GridView中的三种构造全部写上,否则很可能出现解析xml文件异常的错误。

2,在该类中重写dispatchTouchEvent()方法,如下:

@Override

public boolean dispatchTouchEvent(MotionEvent ev) {

if (ev.getAction() == MotionEvent.ACTION_MOVE) {

return true; //禁止GridView滑动

}

return super.dispatchTouchEvent(ev);

}

3,在布局文件(xml)中定义该控件时写全包名,如下(属性跟GridView控件属性一样,按需求自加):

<com.kz.steerwheel.view.SourcePanel

android:id="@+id/sourcePanel"

android:layout_width="match_parent"

android:layout_height="match_parent"

/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: