天天看點

Anddroid如何禁止Gridview上下滑動

因為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);

public grapegridview(context context, attributeset attrs, int defstyle) {

super(context, attrs, defstyle);

       //通過重新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

繼續閱讀