事件传递、 手势GestureDetector
2015-09-24 17:36
337 查看
事件传递
Android的事件传递有下面三种方法:1、dispatchTouchEvent 表示事件的分发
2、onInterceptTouchEvent 表示事件的拦截
3、onTouchEvent 表示事件的处理
返回值为false表示不拦截、不分发
下拉刷新:
package com.example.administrator.myrefreshdemo; import android.animation.ObjectAnimator; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.widget.ArrayAdapter; import android.widget.FrameLayout; import android.widget.ListView; /** * Created by Administrator on 2015/9/24. */ public class MyRefreshLayout extends FrameLayout { ListView content; public MyRefreshLayout(Context context) { super(context); } public MyRefreshLayout(Context context, AttributeSet attrs) { super(context, attrs); //先添加一个下拉刷新 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View header = inflater.inflate(R.layout.activity_fresh,null); addView(header); //再添加一个上边的view content = (ListView) inflater.inflate(R.layout.activity_listview,null); addView(content); ArrayAdapter <String> adapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,new String[]{"a","b","c","d","e","f","a","b","c","d","e","f"}); content.setAdapter(adapter); } public MyRefreshLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { return super.dispatchTouchEvent(ev); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN){ Log.d("mytouch","linearlayoutonInterceptTouchEvent"+ev.getAction()+super.onInterceptTouchEvent(ev)); } if (content.getFirstVisiblePosition() == 0){ View firstView = content.getChildAt(content.getFirstVisiblePosition()); if (firstView.getY() >=0){ Log.d("mytouch","拦截到事件"); return true; } } return super.onInterceptTouchEvent(ev); } float y = 0; float oldY = 0; @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()){ case MotionEvent.ACTION_DOWN: oldY = event.getY(); break; case MotionEvent.ACTION_MOVE: Log.d("mytouch","处理事件"); y = event.getY(); float distance = y - oldY; content.setTranslationY(content.getTranslationY()+distance); oldY = y; break; case MotionEvent.ACTION_UP: ObjectAnimator.ofFloat(content,"translationY",content.getTranslationY(),0).setDuration(300).start(); break; } return true; } }
布局文件
<com.example.administrator.myrefreshdemo.MyRefreshLayout android:layout_width="match_parent" android:layout_height="match_parent"> </com.example.administrator.myrefreshdemo.MyRefreshLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/refresh" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="下拉刷新" android:textSize="20dp" android:gravity="center" android:padding="10dp"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff"> </ListView>
谷歌自己写的下拉刷新,用时需要support.v4包
public class MainActivity extends Activity { private SwipeRefreshLayout mSwipeRefreshLayout; private ListView mListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefreshlayout); mListView = (ListView) findViewById(R.id.listview); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,new String[]{"a","b","c","d","e","f","a","b","c","d","e","f"}); mListView.setAdapter(adapter); mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){ @Override public void onRefresh() { mSwipeRefreshLayout.setRefreshing(false); } }); } }
布局文件
<android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swiperefreshlayout" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </android.support.v4.widget.SwipeRefreshLayout>
手势
双击、滑动、按住移动package com.example.administrator.myrefreshdemo; import android.animation.ObjectAnimator; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import java.util.Objects; /** * Created by Administrator on 2015/9/24. */ public class MyButton extends Button { private GestureDetector mGesture; //监听者模式 interface onDoubleClick{ public void onDoubleClick(View view); } private onDoubleClick onDoubleClickListener; public void setOnDoubleClickListener(onDoubleClick onDoubleClickListener) { this.onDoubleClickListener = onDoubleClickListener; } public MyButton(Context context) { super(context); } public MyButton(Context context, AttributeSet attrs) { super(context, attrs); mGesture = new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){ //双击按钮 @Override public boolean onDoubleTap(MotionEvent e) { if (onDoubleClickListener!=null){ onDoubleClickListener.onDoubleClick(MyButton.this); } Log.d("mydoubletap","连续点击两次"); return true; } //划一下,手离开屏幕后移动 @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (Math.abs(e2.getX()-e1.getX())>50){ Log.d("mydoubletap", "在X轴滑动"); // setTranslationX(e2.getX() - e1.getX()); ObjectAnimator.ofFloat(MyButton.this,"translationX",getTranslationX(),e2.getX() - e1.getX()).setDuration(800).start(); return true; } return super.onFling(e1, e2, velocityX, velocityY); } //随着手指在屏幕上move而move @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { setTranslationX(getTranslationX()+e2.getX() - e1.getX()); setTranslationY(getTranslationY()+e2.getY() - e1.getY()); return true; } }); } public MyButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean dispatchTouchEvent(MotionEvent event) { return super.dispatchTouchEvent(event); } @Override public boolean onTouchEvent(MotionEvent event) { mGesture.onTouchEvent(event); return super.onTouchEvent(event); } }
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MyButton button = (MyButton) findViewById(R.id.mybutton); button.setOnDoubleClickListener(new MyButton.onDoubleClick() { @Override public void onDoubleClick(View view) { Toast.makeText(getApplicationContext(),"双击两次",Toast.LENGTH_SHORT).show(); } }); } }
相关文章推荐
- TIME_WAIT状态的必要性
- ksh 引用
- appstore应用信息获取 lookup
- 写十年代码是一种什么样的体验?
- Flarum v0.1.0 Beta 2 发布了
- ant脚本中用beanshell实现简单的自定义task
- UI - iOS事件之TouchEnvent
- arm-linux-gcc的总结
- 第12章 Java内存模型与线程
- Android : GestureDetector手势检测
- php-redis中文文档
- 在centos卸载mysql
- C语言strtok()函数:字符串分割
- ci控制器controller只支持1级目录,如需多级要扩展路由类
- logback源码分析
- C#知识体系(二)用案例来理解委托与事件
- 2015年个人和团队博客地址
- 分页
- canvas(4)图像数据
- java网络编程(二) 客户端和服务端(TCP)