Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android——Tuch測試+MyView+MySwiperLayout

Android——Tuch測試+MyView+MySwiperLayout

編輯:關於Android編程

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>
\
  1. packagec.example.jreduch10.view;
  2.  
  3. importandroid.os.Bundle;
  4. importandroid.support.v7.app.AppCompatActivity;
  5. importandroid.util.Log;
  6. importandroid.view.MotionEvent;
  7. importandroid.view.View;
  8. importandroid.widget.Button;
  9.  
  10. importc.example.jreduch10.R;
  11.  
  12. publicclassTextTuchActivityextendsAppCompatActivity{
  13. privateButtonbt1;
  14. @Override
  15. protectedvoidonCreate(BundlesavedInstanceState){
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_text_tuch);
  18. bt1=(Button)findViewById(R.id.bt1);
  19. bt1.setOnClickListener(newView.OnClickListener(){
  20. @Override
  21. publicvoidonClick(Viewview){
  22. Log.d("======","ButtonOnClick");
  23. }
  24. });
  25. bt1.setOnTouchListener(newView.OnTouchListener(){
  26. @Override
  27. publicbooleanonTouch(Viewview,MotionEventmotionEvent){
  28.  
  29. switch(motionEvent.getAction()){
  30. caseMotionEvent.ACTION_DOWN:
  31. Log.d("======","ActivityonTouchEventDOWN");
  32. break;
  33. caseMotionEvent.ACTION_MOVE:
  34. Log.d("======","ActivityonTouchEventMOVE");
  35. break;
  36. caseMotionEvent.ACTION_UP:
  37. Log.d("======","ActivityonTouchEventUp");
  38. break;
  39. }
  40. returnfalse;//ture不走Log.d("======","ButtonOnClick");
  41. }
  42. });
  43.  
  44. }
  45. //事件分發
  46. @Override
  47. publicbooleandispatchTouchEvent(MotionEventev){
  48. switch(ev.getAction()){
  49. caseMotionEvent.ACTION_DOWN:
  50. Log.d("======","ActivitydispatchTouchEventDOWN");
  51. break;
  52. caseMotionEvent.ACTION_MOVE:
  53. Log.d("======","ActivitydispatchTouchEventMOVE");
  54. break;
  55. caseMotionEvent.ACTION_UP:
  56. Log.d("======","ActivitydispatchTouchEventUp");
  57. break;
  58.  
  59. }
  60. returnsuper.dispatchTouchEvent(ev);
  61. }
  62.  
  63.  
  64. }
  65.  
 
  1. packagec.example.jreduch10.view;
  2.  
  3. importandroid.content.Context;
  4. importandroid.support.v4.widget.SwipeRefreshLayout;
  5. importandroid.util.AttributeSet;
  6. importandroid.util.Log;
  7. importandroid.view.MotionEvent;
  8.  
  9. /**
  10. *Createdby沖天之峰on2016/9/23.
  11. */
  12. publicclassMySwiperLayoutextendsSwipeRefreshLayout{
  13.  
  14.  
  15. publicMySwiperLayout(Contextcontext){
  16. super(context);
  17. }
  18.  
  19. publicMySwiperLayout(Contextcontext,AttributeSetattrs){
  20. super(context,attrs);
  21. }
  22.  
  23. //攔截Touch監聽
  24. @Override
  25. publicbooleanonInterceptTouchEvent(MotionEventev){
  26. Log.d("======","SwiperonInterceptTouchEventlan攔截");
  27. returnsuper.onInterceptTouchEvent(ev);
  28. }
  29.  
  30. //事件分發
  31. @Override
  32. publicbooleandispatchTouchEvent(MotionEventev){
  33. switch(ev.getAction()){
  34. caseMotionEvent.ACTION_DOWN:
  35. Log.d("======","SwiperdispatchTouchEventDOWN");
  36. break;
  37. caseMotionEvent.ACTION_MOVE:
  38. Log.d("======","SwiperdispatchTouchEventMOVE");
  39. break;
  40. caseMotionEvent.ACTION_UP:
  41. Log.d("======","SwiperdispatchTouchEventUp");
  42. break;
  43.  
  44. }
  45. returnsuper.dispatchTouchEvent(ev);
  46. }
  47.  
  48. @Override
  49. publicbooleanonTouchEvent(MotionEventevent){
  50. switch(event.getAction()){
  51. caseMotionEvent.ACTION_DOWN:
  52. Log.d("======","SwiperonTouchEventDOWN");
  53. break;
  54. caseMotionEvent.ACTION_MOVE:
  55. Log.d("======","SwiperonTouchEventMOVE");
  56. break;
  57. caseMotionEvent.ACTION_UP:
  58. Log.d("======","SwiperonTouchEventUp");
  59. break;
  60.  
  61. }
  62.  
  63. returnsuper.onTouchEvent(event);
  64. }
  65. }
  66. packagec.example.jreduch10.view;
  67.  
  68. importandroid.content.Context;
  69. importandroid.util.AttributeSet;
  70. importandroid.util.Log;
  71. importandroid.view.MotionEvent;
  72. importandroid.widget.LinearLayout;
  73.  
  74. /**
  75. *Createdby沖天之峰on2016/9/23.
  76. */
  77. publicclassMyViewextendsLinearLayout{
  78. publicMyView(Contextcontext){
  79. super(context);
  80. }
  81.  
  82. publicMyView(Contextcontext,AttributeSetattrs){
  83. super(context,attrs);
  84. }
  85. //事件分發
  86. @Override
  87. publicbooleandispatchTouchEvent(MotionEventev){
  88. switch(ev.getAction()){
  89. caseMotionEvent.ACTION_DOWN:
  90. Log.d("======","MyViewdispatchTouchEventDOWN");
  91. break;
  92. caseMotionEvent.ACTION_MOVE:
  93. Log.d("======","MyViewdispatchTouchEventMOVE");
  94. break;
  95. caseMotionEvent.ACTION_UP:
  96. Log.d("======","MyViewdispatchTouchEventUp");
  97. break;
  98.  
  99. }
  100. returnsuper.dispatchTouchEvent(ev);
  101. }
  102.  
  103. @Override
  104. publicbooleanonTouchEvent(MotionEventevent){
  105. switch(event.getAction()){
  106. caseMotionEvent.ACTION_DOWN:
  107. Log.d("======","MyViewonTouchEventDOWN");
  108. break;
  109. caseMotionEvent.ACTION_MOVE:
  110. Log.d("======","MyViewonTouchEventMOVE");
  111. break;
  112. caseMotionEvent.ACTION_UP:
  113. Log.d("======","MyViewonTouchEventUp");
  114. break;
  115.  
  116. }
  117.  
  118. returnsuper.onTouchEvent(event);
  119. }
  120. }

\
左右滑動距離監聽
  1. packagec.example.jreduch09.view;
  2.  
  3. importandroid.content.Context;
  4. importandroid.support.v4.widget.SwipeRefreshLayout;
  5. importandroid.util.AttributeSet;
  6. importandroid.util.Log;
  7. importandroid.view.MotionEvent;
  8. importandroid.view.ViewConfiguration;
  9.  
  10. publicclassVerticalSwipeRefreshLayoutextendsSwipeRefreshLayout{
  11. privateintmTouchSlop;
  12. privatefloatmPrevX;//上一次觸摸時的X坐標
  13. publicVerticalSwipeRefreshLayout(Contextcontext,AttributeSetattrs){
  14. super(context,attrs);
  15. //觸發移動事件的最短距離,如果小於這個距離就不觸發移動控件
  16. mTouchSlop=ViewConfiguration.get(context).getScaledTouchSlop();
  17. }
  18. @Override
  19. publicbooleanonInterceptTouchEvent(MotionEventevent){
  20. switch(event.getAction()){
  21. caseMotionEvent.ACTION_DOWN:
  22. mPrevX=event.getX();
  23. break;
  24. caseMotionEvent.ACTION_MOVE:
  25. finalfloateventX=event.getX();
  26. floatxDiff=Math.abs(eventX-mPrevX);
  27. //增加60的容差,讓下拉刷新在豎直滑動時就可以觸發
  28. Log.d("=====mTouchSlop=",""+mTouchSlop);
  29. Log.d("=====xDiff=",""+xDiff);
  30. if(xDiff>mTouchSlop+200){
  31. returnfalse;
  32. }
  33. }
  34. returnsuper.onInterceptTouchEvent(event);
  35. }
  36. }
<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>
  1. packagec.example.jreduch09;
  2.  
  3. importandroid.os.AsyncTask;
  4. importandroid.os.Bundle;
  5. importandroid.support.v4.widget.SwipeRefreshLayout;
  6. importandroid.support.v7.app.AppCompatActivity;
  7. importandroid.view.View;
  8. importandroid.widget.AbsListView;
  9. importandroid.widget.ArrayAdapter;
  10. importandroid.widget.ListView;
  11.  
  12. importjava.util.ArrayList;
  13. importjava.util.List;
  14.  
  15. importc.example.jreduch09.util.RefreshLayout;
  16. importc.example.jreduch09.view.VerticalSwipeRefreshLayout;
  17.  
  18. publicclassMain4RefreshlayoutActivityextendsAppCompatActivity{
  19. privateRefreshLayoutrefreshLayout;
  20. privateVerticalSwipeRefreshLayoutsrl;
  21. privateListlist;
  22. privateViewv;
  23. privateArrayAdapteraa;
  24. privateListViewlv;
  25. @Override
  26. protectedvoidonCreate(BundlesavedInstanceState){
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main4);
  29. lv=(ListView)findViewById(R.id.lv);
  30. //refreshLayout=(RefreshLayout)findViewById(R.id.AArticleSwipeRefresh);
  31. srl=(VerticalSwipeRefreshLayout)findViewById(R.id.srl);
  32. list=newArrayList();
  33. aa=newArrayAdapter(this,android.R.layout.simple_list_item_1,list);
  34. lv.setAdapter(aa);
  35.  
  36. //refreshLayout.setOnRefreshListener(newSwipeRefreshLayout.OnRefreshListener(){
  37. //@Override
  38. //publicvoidonRefresh(){
  39. //list.clear();
  40. //for(inti=0;i<20;i++){
  41. //list.add("di"+i+"xiaoxi");
  42. //
  43. //}
  44. //aa.notifyDataSetChanged();
  45. //refreshLayout.setRefreshing(false);
  46. //}
  47. //});
  48. //上拉加載更多
  49. //refreshLayout.setOnLoadListener(newRefreshLayout.OnLoadListener(){
  50. //@Override
  51. //publicvoidonLoad(){
  52. //for(inti=0;i<20;i++){
  53. //list.add("新增加22di22"+i+"xiaoxi");
  54. //
  55. //}
  56. //aa.notifyDataSetChanged();
  57. //refreshLayout.setLoading(false);
  58. //}
  59. //});
  60.  
  61.  
  62.  
  63. srl.setOnRefreshListener(newSwipeRefreshLayout.OnRefreshListener(){
  64. @Override
  65. publicvoidonRefresh(){
  66. list.clear();
  67. for(inta=0;a<20;a++){
  68. list.add("刷新"+a+"數據");
  69. }
  70. aa.notifyDataSetChanged();
  71. srl.setRefreshing(false);
  72. }
  73. });
  74.  
  75.  
  76.  
  77. lv.setOnScrollListener(newAbsListView.OnScrollListener(){
  78. @Override
  79. publicvoidonScrollStateChanged(AbsListViewabsListView,inti){
  80.  
  81. }
  82. @Override
  83. publicvoidonScroll(AbsListViewabsListView,inti,inti1,inti2){
  84. if(i2==0){
  85. return;
  86. }
  87. intcount=lv.getFooterViewsCount();
  88. if(count==0){
  89. if(i+i1==i2){
  90. if(lv.getFooterViewsCount()==1){
  91. return;
  92. }
  93. lv.addFooterView(v);
  94. aa.notifyDataSetChanged();
  95. newTest().execute();
  96. }
  97. }
  98. }
  99. });
  100. }
  101. publicclassTestextendsAsyncTask{
  102. @Override
  103. protectedVoiddoInBackground(Void...voids){
  104. try{
  105. Thread.sleep(3000);
  106. }catch(InterruptedExceptione){
  107. e.printStackTrace();
  108. }
  109. returnnull;
  110. }
  111. @Override
  112. protectedvoidonPostExecute(VoidaVoid){
  113. super.onPostExecute(aVoid);
  114. for(inta=0;a<20;a++){
  115. list.add("新增"+a+"數據");
  116. }
  117. lv.removeFooterView(v);
  118. aa.notifyDataSetChanged();
  119. }
  120. }
  121. }
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved