概述:
Android事件構成:
在Android中,事件主要包括點按、長按、拖拽、滑動等,點按又包括單擊和雙擊,另外還包括單指操作和多指操作。所有這些都構成了Android中的事件響應。總的來說,所有的事件都由如下三個部分作爲基礎:
- 按下(ACTION_DOWN)
- 移動(ACTION_MOVE)
- 擡起(ACTION_UP)
跟touch事件相關的3個方法:
- public boolean dispatchTouchEvent(MotionEvent ev); //用來分派event
- public boolean onInterceptTouchEvent(MotionEvent ev); //用來攔截event
- public boolean onTouchEvent(MotionEvent ev); //用來處理event
Android事件處理的機制:
onInterceptTouchEvent():
決定是否允許Touch事件繼續向下(子控件)傳遞,一但返回True(代表事件在當前的viewGroup中會被處理),則向下傳遞之路被截斷(所有子控件將沒有機會參與Touch事件),同時把事件傳遞給當前的控件的onTouchEvent()處理;返回false,則把事件交給子控件的onInterceptTouchEvent()。
dispatchTouchEvent():
返回值決定是否向下分發這個事件,返回true,則分發事件給子控件,否則,不分發touch事件給子控件。
onTouchEvent():
返回值決定當前控件是否消費onTouch了這個事件,返回false,則向上傳遞給父控件,返回true,則消費此事件。
Demo
模擬下拉刷新:
/**
* 事件的傳遞:新建一個類繼承於FrameLayout,實現其構造方法
* 必須實現三個處理事件的方法:onInterceptTouchEvent()、
* dispatchTouchEvent()、onTouchEvent()
*/
public class MyFreshLayout extends FrameLayout {
private ListView mListView;
private float oldY;
private float y;
public MyFreshLayout(Context context) {
super(context);
}
public MyFreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View header = inflater.inflate(R.layout.header,null);
addView(header);
mListView = (ListView) inflater.inflate(R.layout.content,null);
addView(mListView);
ArrayAdapter<String> adapter = new ArrayAdapter(context,android.R.layout.simple_list_item_1
,new String[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P"});
mListView.setAdapter(adapter);
}
public MyFreshLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if(mListView.getFirstVisiblePosition()==0){
View firstView = mListView.getChildAt(0);
if(firstView.getY()>=0){
return true;
}
}
return super.onInterceptHoverEvent(event);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
/*if(mListView.getFirstVisiblePosition()==0){
View firstView = mListView.getChildAt(mListView.getFirstVisiblePosition());
if(firstView.getY()>=0){
return true;
}
}*/
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
/**
* 得到觸碰焦點的y座標
*/
oldY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
y = event.getY();
float distance = y - oldY;
//讓mListView移動距離等於手指滑動距離
mListView.setTranslationY(mListView.getTranslationY()+distance);
oldY = y;
break;
case MotionEvent.ACTION_UP:
//手指釋放後mListView恢復初始狀態
ObjectAnimator.ofFloat(mListView,"translationY",mListView.getTranslationY(),0)
.setDuration(300).start();
break;
}
return true;
}
}
header,作爲首部的佈局,首部初始時被覆蓋:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="30dp"
android:gravity="center"
android:text="刷新"/>
</FrameLayout>
content,主要內容:
<?xml version="1.0" encoding="utf-8"?>
<ListView android:id="@+id/listView_downDrap"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
</ListView>
佈局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.administrator.selfishgroupview.my_groupview.MyFreshLayout
android:id="@+id/MyFreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.example.administrator.selfishgroupview.my_groupview.MyFreshLayout>
</RelativeLayout>
主活動:
public class MainActivity extends Activity {
private MyFreshLayout myFreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myFreshLayout = (MyFreshLayout) findViewById(R.id.my_surfaceView);
}
}
結果演示:
我們猿類工作壓力大,很需要有自己的樂趣,於是乎,我開通了音樂人賬號,以後的作品將會上傳到我的音樂人小站上。如果這篇博客幫助到您,希望您能多關注,支持,鼓勵我將創作進行下去,同時也祝你能在工作和生活樂趣兩發麪都能出彩!