TouchEvent的基础实现

来源:互联网 发布:淘宝发物流怎么填单号 编辑:程序博客网 时间:2024/06/15 19:58
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.bwie.toucheventdemoday14.MainActivity">    <Button        android:id="@+id/btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="我是按钮" />    <com.bwie.toucheventdemoday14.MyScrollView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_below="@+id/btn">        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:orientation="vertical"            >            <com.bwie.toucheventdemoday14.MyListView                android:id="@+id/lv"                android:layout_width="match_parent"                android:layout_height="200dp"/>            <Button                android:text="我是按钮"                android:layout_marginTop="5dp"                android:layout_width="match_parent"                android:layout_height="wrap_content" />            <Button                android:text="我是按钮"                android:layout_marginTop="5dp"                android:layout_width="match_parent"                android:layout_height="wrap_content" />            <Button                android:text="我是按钮"                android:layout_marginTop="5dp"                android:layout_width="match_parent"                android:layout_height="wrap_content" />            <Button                android:text="我是按钮"                android:layout_marginTop="5dp"                android:layout_width="match_parent"                android:layout_height="wrap_content" />            <Button                android:text="我是按钮"                android:layout_marginTop="5dp"                android:layout_width="match_parent"                android:layout_height="wrap_content" />            <Button                android:text="我是按钮"                android:layout_marginTop="5dp"                android:layout_width="match_parent"                android:layout_height="wrap_content" />            <Button                android:text="我是按钮"                android:layout_marginTop="5dp"                android:layout_width="match_parent"                android:layout_height="wrap_content" />            <Button                android:text="我是按钮"                android:layout_marginTop="5dp"                android:layout_width="match_parent"                android:layout_height="wrap_content" />            <Button                android:text="我是按钮"                android:layout_marginTop="5dp"                android:layout_width="match_parent"                android:layout_height="wrap_content" />            <Button                android:text="我是按钮"                android:layout_marginTop="5dp"                android:layout_width="match_parent"                android:layout_height="wrap_content" />            <Button                android:text="我是按钮"                android:layout_marginTop="5dp"                android:layout_width="match_parent"                android:layout_height="wrap_content" />            <Button                android:text="我是按钮"                android:layout_marginTop="5dp"                android:layout_width="match_parent"                android:layout_height="wrap_content" />        </LinearLayout>    </com.bwie.toucheventdemoday14.MyScrollView>

</RelativeLayout>

MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {    /**     * 我是按钮     */    private Button mBtn;    private ListView mLv;    private String[] names = {"张三1","张三2","张三3","张三4","张三5","张三6","张三7","张三8","张三9","张三10"};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        //配置数据        mLv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,names));    }    @Override    public boolean dispatchTouchEvent(MotionEvent ev) {        boolean b = super.dispatchTouchEvent(ev);        Log.i("main","MainActivity。。。。dispatchTouchEvent...."+b);        return b;    }    @Override    public boolean onTouchEvent(MotionEvent event) {        boolean b = super.onTouchEvent(event);        Log.i("main","MainActivity。。。。onTouchEvent....."+b);        return b;    }    private void initView() {        mBtn = (Button) findViewById(R.id.btn);        mLv = (ListView) findViewById(R.id.lv);        mBtn.setOnClickListener(this);        mBtn.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                Toast.makeText(MainActivity.this, "触摸了我111", Toast.LENGTH_SHORT).show();                if (event.getAction() == MotionEvent.ACTION_DOWN) {//按下                    Toast.makeText(MainActivity.this, "按下", Toast.LENGTH_SHORT).show();                    Log.i("main", "按下。。。。。。。。。。。");                } else if (event.getAction() == MotionEvent.ACTION_UP) {                    Toast.makeText(MainActivity.this, "抬起", Toast.LENGTH_SHORT).show();                    Log.i("main", "抬起。。。。。。。。。。。");                } else {                    Toast.makeText(MainActivity.this, "在组件上滚动move", Toast.LENGTH_SHORT).show();                    Log.i("main", "滚动。。。。。。。。。。。");                }                return false;//false没有处理触摸事件,会衍生出按钮的点击事件;true表示消费了,到此为止            }        });    }    @Override    public void onClick(View v) {        switch (v.getId()) {            default:                break;            case R.id.btn:                Toast.makeText(this, "点击了我222", Toast.LENGTH_SHORT).show();                break;        }    }}
public class MyScrollView extends ScrollView {    public MyScrollView(Context context) {        super(context);    }    public MyScrollView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    public boolean dispatchTouchEvent(MotionEvent ev) {        boolean b = super.dispatchTouchEvent(ev);        Log.i("main","MyScrollView。。。。dispatchTouchEvent...."+b);        return b;    }    @Override    public boolean onInterceptTouchEvent(MotionEvent ev) {        boolean b = super.onInterceptTouchEvent(ev);        Log.i("main","MyScrollView。。。。onInterceptTouchEvent...."+b);        return b;    }    @Override    public boolean onTouchEvent(MotionEvent ev) {        boolean b = super.onTouchEvent(ev);        Log.i("main","MyScrollView。。。。onTouchEvent...."+b);        return b;    }}

public class MyButton extends Button {    public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    public boolean dispatchTouchEvent(MotionEvent event) {        return super.dispatchTouchEvent(event);    }}

public class MyListView extends ListView {    public MyListView(Context context) {        super(context);    }    public MyListView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    public boolean dispatchTouchEvent(MotionEvent ev) {       // boolean b = ;        //Log.i("main","MyListView。。。。dispatchTouchEvent...."+b);        getParent().requestDisallowInterceptTouchEvent(true);//请求不允许父亲(ScrollView)截断触摸事件        return super.dispatchTouchEvent(ev);    }    @Override    public boolean onInterceptTouchEvent(MotionEvent ev) {        boolean b = super.onInterceptTouchEvent(ev);         Log.i("main","MyListView。。。。onInterceptTouchEvent...."+b);        return b;    }    @Override    public boolean onTouchEvent(MotionEvent ev) {        boolean b = super.onTouchEvent(ev);        Log.i("main","MyListView。。。。onTouchEvent...."+b);        return b;    }}