如何禁止Gridview上下滑动

之前开发遇到这种问题  今天看到了 mark下

之前解决的方法是

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
if(ev.getAction() == MotionEvent.ACTION_MOVE){
                 return true;//禁止Gridview进行滑动
           }
return super.dispatchTouchEvent(ev);
}
}

禁止了整个activity的滑动


原文如下(未测试是否成功):

因为android没有提供直接禁止Gridview滑动的API,也没有提供相应的属性来在XML布局文件中直接禁止滑动,当我们做菜单时要禁止Gridview上下滑动怎么办呢?

1、自定义一个Gridview
2、通过重新dispatchTouchEvent方法来禁止滑动
public class GrapeGridview extends GridView {

public GrapeGridview(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public GrapeGridview(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}

public GrapeGridview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
       
           //通过重新dispatchTouchEvent方法来禁止滑动
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
if(ev.getAction() == MotionEvent.ACTION_MOVE){
                 return true;//禁止Gridview进行滑动
           }
return super.dispatchTouchEvent(ev);
}
}

3、在xml布局中引用时记得写上自己的定义的gridview全路径名称
如:
<com.wjq.menu.GrapeGridview android:id="@+id/gridview"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:numColumns="4" android:verticalSpacing="10dip"
android:horizontalSpacing="10dip" android:stretchMode="columnWidth"
android:gravity="center" />
本人欢迎转载:但是请注明原文地址: http://blog.sina.com.cn/s/blog_4a4f9fb50100tyfk.html
谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值