1,定义一个类继承GridView,如下:
public class MyGridView extends GridView {
public MyGridView (Context context) {
super(context);
}
public MyGridView (Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
注意:构造方法要将GridView中的三种构造全部写上,否则很可能出现解析xml文件异常的错误。
2,在该类中重写dispatchTouchEvent()方法,如下:
设置gridview 的touch事件,是ACTION_MOVE 的话返回true mDragGrid.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return MotionEvent.ACTION_MOVE == event.getAction() ? true : false; } });
3,在布局文件(xml)中定义该控件时写全包名,如下(属性跟GridView控件属性一样,按需求自加):
<com.kz.steerwheel.view.SourcePanel
Android:id="@+id/sourcePanel"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>