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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="c.example.jreduch10.view.TextTuchActivity">
<c.example.jreduch10.view.MyView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mv"
android:orientation="vertical"
>
<c.example.jreduch10.view.MySwiperLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f405"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/bt1"
android:textSize="30sp"
android:gravity="center"
android:text="Tuch測試"
/>
LinearLayout>
c.example.jreduch10.view.MySwiperLayout>
c.example.jreduch10.view.MyView>
RelativeLayout>
- packagec.example.jreduch10.view;
-
- importandroid.os.Bundle;
- importandroid.support.v7.app.AppCompatActivity;
- importandroid.util.Log;
- importandroid.view.MotionEvent;
- importandroid.view.View;
- importandroid.widget.Button;
-
- importc.example.jreduch10.R;
-
- publicclassTextTuchActivityextendsAppCompatActivity{
- privateButtonbt1;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_text_tuch);
- bt1=(Button)findViewById(R.id.bt1);
- bt1.setOnClickListener(newView.OnClickListener(){
- @Override
- publicvoidonClick(Viewview){
- Log.d("======","ButtonOnClick");
- }
- });
- bt1.setOnTouchListener(newView.OnTouchListener(){
- @Override
- publicbooleanonTouch(Viewview,MotionEventmotionEvent){
-
- switch(motionEvent.getAction()){
- caseMotionEvent.ACTION_DOWN:
- Log.d("======","ActivityonTouchEventDOWN");
- break;
- caseMotionEvent.ACTION_MOVE:
- Log.d("======","ActivityonTouchEventMOVE");
- break;
- caseMotionEvent.ACTION_UP:
- Log.d("======","ActivityonTouchEventUp");
- break;
- }
- returnfalse;//ture不走Log.d("======","ButtonOnClick");
- }
- });
-
- }
- //事件分發
- @Override
- publicbooleandispatchTouchEvent(MotionEventev){
- switch(ev.getAction()){
- caseMotionEvent.ACTION_DOWN:
- Log.d("======","ActivitydispatchTouchEventDOWN");
- break;
- caseMotionEvent.ACTION_MOVE:
- Log.d("======","ActivitydispatchTouchEventMOVE");
- break;
- caseMotionEvent.ACTION_UP:
- Log.d("======","ActivitydispatchTouchEventUp");
- break;
-
- }
- returnsuper.dispatchTouchEvent(ev);
- }
-
-
- }
-
- packagec.example.jreduch10.view;
-
- importandroid.content.Context;
- importandroid.support.v4.widget.SwipeRefreshLayout;
- importandroid.util.AttributeSet;
- importandroid.util.Log;
- importandroid.view.MotionEvent;
-
- /**
- *Createdby沖天之峰on2016/9/23.
- */
- publicclassMySwiperLayoutextendsSwipeRefreshLayout{
-
-
- publicMySwiperLayout(Contextcontext){
- super(context);
- }
-
- publicMySwiperLayout(Contextcontext,AttributeSetattrs){
- super(context,attrs);
- }
-
- //攔截Touch監聽
- @Override
- publicbooleanonInterceptTouchEvent(MotionEventev){
- Log.d("======","SwiperonInterceptTouchEventlan攔截");
- returnsuper.onInterceptTouchEvent(ev);
- }
-
- //事件分發
- @Override
- publicbooleandispatchTouchEvent(MotionEventev){
- switch(ev.getAction()){
- caseMotionEvent.ACTION_DOWN:
- Log.d("======","SwiperdispatchTouchEventDOWN");
- break;
- caseMotionEvent.ACTION_MOVE:
- Log.d("======","SwiperdispatchTouchEventMOVE");
- break;
- caseMotionEvent.ACTION_UP:
- Log.d("======","SwiperdispatchTouchEventUp");
- break;
-
- }
- returnsuper.dispatchTouchEvent(ev);
- }
-
- @Override
- publicbooleanonTouchEvent(MotionEventevent){
- switch(event.getAction()){
- caseMotionEvent.ACTION_DOWN:
- Log.d("======","SwiperonTouchEventDOWN");
- break;
- caseMotionEvent.ACTION_MOVE:
- Log.d("======","SwiperonTouchEventMOVE");
- break;
- caseMotionEvent.ACTION_UP:
- Log.d("======","SwiperonTouchEventUp");
- break;
-
- }
-
- returnsuper.onTouchEvent(event);
- }
- }
- packagec.example.jreduch10.view;
-
- importandroid.content.Context;
- importandroid.util.AttributeSet;
- importandroid.util.Log;
- importandroid.view.MotionEvent;
- importandroid.widget.LinearLayout;
-
- /**
- *Createdby沖天之峰on2016/9/23.
- */
- publicclassMyViewextendsLinearLayout{
- publicMyView(Contextcontext){
- super(context);
- }
-
- publicMyView(Contextcontext,AttributeSetattrs){
- super(context,attrs);
- }
- //事件分發
- @Override
- publicbooleandispatchTouchEvent(MotionEventev){
- switch(ev.getAction()){
- caseMotionEvent.ACTION_DOWN:
- Log.d("======","MyViewdispatchTouchEventDOWN");
- break;
- caseMotionEvent.ACTION_MOVE:
- Log.d("======","MyViewdispatchTouchEventMOVE");
- break;
- caseMotionEvent.ACTION_UP:
- Log.d("======","MyViewdispatchTouchEventUp");
- break;
-
- }
- returnsuper.dispatchTouchEvent(ev);
- }
-
- @Override
- publicbooleanonTouchEvent(MotionEventevent){
- switch(event.getAction()){
- caseMotionEvent.ACTION_DOWN:
- Log.d("======","MyViewonTouchEventDOWN");
- break;
- caseMotionEvent.ACTION_MOVE:
- Log.d("======","MyViewonTouchEventMOVE");
- break;
- caseMotionEvent.ACTION_UP:
- Log.d("======","MyViewonTouchEventUp");
- break;
-
- }
-
- returnsuper.onTouchEvent(event);
- }
- }
左右滑動距離監聽
- packagec.example.jreduch09.view;
-
- importandroid.content.Context;
- importandroid.support.v4.widget.SwipeRefreshLayout;
- importandroid.util.AttributeSet;
- importandroid.util.Log;
- importandroid.view.MotionEvent;
- importandroid.view.ViewConfiguration;
-
- publicclassVerticalSwipeRefreshLayoutextendsSwipeRefreshLayout{
- privateintmTouchSlop;
- privatefloatmPrevX;//上一次觸摸時的X坐標
- publicVerticalSwipeRefreshLayout(Contextcontext,AttributeSetattrs){
- super(context,attrs);
- //觸發移動事件的最短距離,如果小於這個距離就不觸發移動控件
- mTouchSlop=ViewConfiguration.get(context).getScaledTouchSlop();
- }
- @Override
- publicbooleanonInterceptTouchEvent(MotionEventevent){
- switch(event.getAction()){
- caseMotionEvent.ACTION_DOWN:
- mPrevX=event.getX();
- break;
- caseMotionEvent.ACTION_MOVE:
- finalfloateventX=event.getX();
- floatxDiff=Math.abs(eventX-mPrevX);
- //增加60的容差,讓下拉刷新在豎直滑動時就可以觸發
- Log.d("=====mTouchSlop=",""+mTouchSlop);
- Log.d("=====xDiff=",""+xDiff);
- if(xDiff>mTouchSlop+200){
- returnfalse;
- }
- }
- returnsuper.onInterceptTouchEvent(event);
- }
- }
<c.example.jreduch09.view.VerticalSwipeRefreshLayout
android:id="@+id/srl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv"
>
ListView>c.example.jreduch09.view.VerticalSwipeRefreshLayout>
- packagec.example.jreduch09;
-
- importandroid.os.AsyncTask;
- importandroid.os.Bundle;
- importandroid.support.v4.widget.SwipeRefreshLayout;
- importandroid.support.v7.app.AppCompatActivity;
- importandroid.view.View;
- importandroid.widget.AbsListView;
- importandroid.widget.ArrayAdapter;
- importandroid.widget.ListView;
-
- importjava.util.ArrayList;
- importjava.util.List;
-
- importc.example.jreduch09.util.RefreshLayout;
- importc.example.jreduch09.view.VerticalSwipeRefreshLayout;
-
- publicclassMain4RefreshlayoutActivityextendsAppCompatActivity{
- privateRefreshLayoutrefreshLayout;
- privateVerticalSwipeRefreshLayoutsrl;
- privateListlist;
- privateViewv;
- privateArrayAdapteraa;
- privateListViewlv;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main4);
- lv=(ListView)findViewById(R.id.lv);
- //refreshLayout=(RefreshLayout)findViewById(R.id.AArticleSwipeRefresh);
- srl=(VerticalSwipeRefreshLayout)findViewById(R.id.srl);
- list=newArrayList();
- aa=newArrayAdapter(this,android.R.layout.simple_list_item_1,list);
- lv.setAdapter(aa);
-
- //refreshLayout.setOnRefreshListener(newSwipeRefreshLayout.OnRefreshListener(){
- //@Override
- //publicvoidonRefresh(){
- //list.clear();
- //for(inti=0;i<20;i++){
- //list.add("di"+i+"xiaoxi");
- //
- //}
- //aa.notifyDataSetChanged();
- //refreshLayout.setRefreshing(false);
- //}
- //});
- //上拉加載更多
- //refreshLayout.setOnLoadListener(newRefreshLayout.OnLoadListener(){
- //@Override
- //publicvoidonLoad(){
- //for(inti=0;i<20;i++){
- //list.add("新增加22di22"+i+"xiaoxi");
- //
- //}
- //aa.notifyDataSetChanged();
- //refreshLayout.setLoading(false);
- //}
- //});
-
-
-
- srl.setOnRefreshListener(newSwipeRefreshLayout.OnRefreshListener(){
- @Override
- publicvoidonRefresh(){
- list.clear();
- for(inta=0;a<20;a++){
- list.add("刷新"+a+"數據");
- }
- aa.notifyDataSetChanged();
- srl.setRefreshing(false);
- }
- });
-
-
-
- lv.setOnScrollListener(newAbsListView.OnScrollListener(){
- @Override
- publicvoidonScrollStateChanged(AbsListViewabsListView,inti){
-
- }
- @Override
- publicvoidonScroll(AbsListViewabsListView,inti,inti1,inti2){
- if(i2==0){
- return;
- }
- intcount=lv.getFooterViewsCount();
- if(count==0){
- if(i+i1==i2){
- if(lv.getFooterViewsCount()==1){
- return;
- }
- lv.addFooterView(v);
- aa.notifyDataSetChanged();
- newTest().execute();
- }
- }
- }
- });
- }
- publicclassTestextendsAsyncTask{
- @Override
- protectedVoiddoInBackground(Void...voids){
- try{
- Thread.sleep(3000);
- }catch(InterruptedExceptione){
- e.printStackTrace();
- }
- returnnull;
- }
- @Override
- protectedvoidonPostExecute(VoidaVoid){
- super.onPostExecute(aVoid);
- for(inta=0;a<20;a++){
- list.add("新增"+a+"數據");
- }
- lv.removeFooterView(v);
- aa.notifyDataSetChanged();
- }
- }
- }