android中禁止GridView上下滑動的方法
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
super(context, attrs, defStyle);
}
}
注意:構造方法要將GridView中的三種構造全部寫上,否則很可能出現解析xml檔案異常的錯誤。
2,在該類中重寫dispatchTouchEvent()方法,如下:
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_MOVE) {
return true
}
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"
/>