Android event transfer (like pull-down refresh)
Overview:
Android event composition:
In Android, events mainly include click, long press, drag, and slide. Click and double-click, as well as single-finger and multi-finger operations. All of these constitute event responses in Android. In general, all events are based on the following three parts:
Press (ACTION_DOWN) to move (ACTION_MOVE) and lift (ACTION_UP)
Three methods related to touch events:
Public boolean dispatchTouchEvent (MotionEvent ev); // used to assign event public boolean onInterceptTouchEvent (MotionEvent ev); // used to intercept event public boolean onTouchEvent (MotionEvent ev); // used to process event
Android event processing mechanism:
OnInterceptTouchEvent ():
Determines whether to allow the Touch event to be passed down (sub-control). If the first parameter is set to True, the event will be processed in the current viewGroup ), the next pass-down path is truncated (all child controls will not be able to participate in the Touch event), and the event is passed to the onTouchEvent () of the current control for processing; false is returned, the event is handed over to the onInterceptTouchEvent () of the sub-control (). <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Examples/LPwrfWt6LV4rj2ysK8/examples + NfTv9i8/qOst/examples + uPjX07/examples/srHt/HP + examples/yyc + 0q7XduPi4uL/examples/Examples "here write picture description" src = "http://www.bkjia.com/uploads/allimg/151012/050I43358-1.png" title = "\"/>
Demo
Simulate pull-down Refresh:
/*** Event transfer: Create a class that inherits from FrameLayout and implements its constructor. * Three event handling methods must be implemented: onInterceptTouchEvent () and * 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
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) {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:/*** get the y coordinate of the touch focus */oldY = event. getY (); break; case MotionEvent. ACTION_MOVE: y = event. getY (); float distance = y-oldY; // Let mListView move the distance equal to the sliding distance of the finger to mListView. setTranslationY (mListView. getTranslationY () + distance); oldY = y; break; case MotionEvent. ACTION_UP: // After the finger is released, mListView resumes its initial state ObjectAnimator. ofFloat (mListView, translationY, mListView. getTranslationY (), 0 ). setDuration (300 ). start (); break;} return true ;}}
Header, as the first layout, is overwritten at the beginning:
<Framelayout android: layout_height = "match_parent" android: layout_width = "match_parent" android: orientation = "vertical" xmlns: android = "http://schemas.android.com/apk/res/android">
</Framelayout>
Content, main content:
Layout:
Main activity:
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); }}
Result demonstration: