Android——Tuch测试+MyView+MySwiperLayout
<?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>
- <span style="font-size:18px;">package c.example.jreduch10.view;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.View;
- import android.widget.Button;
- import c.example.jreduch10.R;
- public class TextTuchActivity extends AppCompatActivity {
- private Button bt1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_text_tuch);
- bt1=(Button)findViewById(R.id.bt1);
- bt1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Log.d("======","Button OnClick");
- }
- });
- bt1.setOnTouchListener(new View.OnTouchListener() {
- @Override
- public boolean onTouch(View view, MotionEvent motionEvent) {
- switch (motionEvent.getAction()) {
- case MotionEvent.ACTION_DOWN:
- Log.d("======", "Activity onTouchEventDOWN");
- break;
- case MotionEvent.ACTION_MOVE:
- Log.d("======", "Activity onTouchEventMOVE");
- break;
- case MotionEvent.ACTION_UP:
- Log.d("======", "Activity onTouchEventUp");
- break;
- }
- return false;//ture不走 Log.d("======","Button OnClick");
- }
- });
- }
- //事件分发
- @Override
- public boolean dispatchTouchEvent(MotionEvent ev) {
- switch (ev.getAction()) {
- case MotionEvent.ACTION_DOWN:
- Log.d("======", "Activity dispatchTouchEventDOWN");
- break;
- case MotionEvent.ACTION_MOVE:
- Log.d("======", "Activity dispatchTouchEventMOVE");
- break;
- case MotionEvent.ACTION_UP:
- Log.d("======", "Activity dispatchTouchEventUp");
- break;
- }
- return super.dispatchTouchEvent(ev);
- }
- }</span><span style="font-size:24px;">
- </span>
- package c.example.jreduch10.view;
- import android.content.Context;
- import android.support.v4.widget.SwipeRefreshLayout;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.MotionEvent;
- /**
- * Created by 冲天之峰 on 2016/9/23.
- */
- public class MySwiperLayout extends SwipeRefreshLayout {
- public MySwiperLayout(Context context) {
- super(context);
- }
- public MySwiperLayout(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- //拦截Touch监听
- @Override
- public boolean onInterceptTouchEvent(MotionEvent ev) {
- Log.d("======","Swiper onInterceptTouchEventlan 拦截");
- return super.onInterceptTouchEvent(ev);
- }
- //事件分发
- @Override
- public boolean dispatchTouchEvent(MotionEvent ev) {
- switch (ev.getAction()) {
- case MotionEvent.ACTION_DOWN:
- Log.d("======", "Swiper dispatchTouchEventDOWN");
- break;
- case MotionEvent.ACTION_MOVE:
- Log.d("======", "Swiper dispatchTouchEventMOVE");
- break;
- case MotionEvent.ACTION_UP:
- Log.d("======", "Swiper dispatchTouchEventUp");
- break;
- }
- return super.dispatchTouchEvent(ev);
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- Log.d("======", "Swiper onTouchEventDOWN");
- break;
- case MotionEvent.ACTION_MOVE:
- Log.d("======", "Swiper onTouchEventMOVE");
- break;
- case MotionEvent.ACTION_UP:
- Log.d("======", "Swiper onTouchEventUp");
- break;
- }
- return super.onTouchEvent(event);
- }
- }
- package c.example.jreduch10.view;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.widget.LinearLayout;
- /**
- * Created by 冲天之峰 on 2016/9/23.
- */
- public class MyView extends LinearLayout {
- public MyView(Context context) {
- super(context);
- }
- public MyView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- //事件分发
- @Override
- public boolean dispatchTouchEvent(MotionEvent ev) {
- switch (ev.getAction()) {
- case MotionEvent.ACTION_DOWN:
- Log.d("======", "My View dispatchTouchEventDOWN");
- break;
- case MotionEvent.ACTION_MOVE:
- Log.d("======", "My View dispatchTouchEventMOVE");
- break;
- case MotionEvent.ACTION_UP:
- Log.d("======", "My View dispatchTouchEventUp");
- break;
- }
- return super.dispatchTouchEvent(ev);
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- Log.d("======", "My View onTouchEventDOWN");
- break;
- case MotionEvent.ACTION_MOVE:
- Log.d("======", "My View onTouchEventMOVE");
- break;
- case MotionEvent.ACTION_UP:
- Log.d("======", "My View onTouchEventUp");
- break;
- }
- return super.onTouchEvent(event);
- }
- }
public class VerticalSwipeRefreshLayout extends SwipeRefreshLayout左右滑动距离监听
- package c.example.jreduch09.view;
- import android.content.Context;
- import android.support.v4.widget.SwipeRefreshLayout;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.ViewConfiguration;
- public class VerticalSwipeRefreshLayout extends SwipeRefreshLayout {
- private int mTouchSlop;
- private float mPrevX; // 上一次触摸时的X坐标
- public VerticalSwipeRefreshLayout(Context context, AttributeSet attrs) {
- super(context, attrs);
- // 触发移动事件的最短距离,如果小于这个距离就不触发移动控件
- mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
- }
- @Override
- public boolean onInterceptTouchEvent(MotionEvent event) {
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- mPrevX = event.getX();
- break;
- case MotionEvent.ACTION_MOVE:
- final float eventX = event.getX();
- float xDiff = Math.abs(eventX - mPrevX);
- // 增加60的容差,让下拉刷新在竖直滑动时就可以触发
- Log.d("=====mTouchSlop=",""+mTouchSlop);
- Log.d("=====xDiff=",""+xDiff);
- if (xDiff > mTouchSlop + 200) {
- return false;
- }
- }
- return super.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>
- package c.example.jreduch09;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.support.v4.widget.SwipeRefreshLayout;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.AbsListView;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import java.util.ArrayList;
- import java.util.List;
- import c.example.jreduch09.util.RefreshLayout;
- import c.example.jreduch09.view.VerticalSwipeRefreshLayout;
- public class Main4RefreshlayoutActivity extends AppCompatActivity {
- private RefreshLayout refreshLayout;
- private VerticalSwipeRefreshLayout srl;
- private List list;
- private View v;
- private ArrayAdapter aa;
- private ListView lv;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- 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=new ArrayList();
- aa=new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);
- lv.setAdapter(aa);
- //refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
- // @Override
- // public void onRefresh() {
- // list.clear();
- // for (int i=0;i<20;i++){
- // list.add("di"+i+"xiaoxi");
- //
- // }
- // aa.notifyDataSetChanged();
- // refreshLayout.setRefreshing(false);
- // }
- //});
- //上拉加载更多
- // refreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
- // @Override
- // public void onLoad() {
- // for (int i=0;i<20;i++){
- // list.add("新增加22di22"+i+"xiaoxi");
- //
- // }
- // aa.notifyDataSetChanged();
- // refreshLayout.setLoading(false);
- // }
- // });
- srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
- @Override
- public void onRefresh() {
- list.clear();
- for (int a=0;a<20;a++){
- list.add("刷新"+a+"数据");
- }
- aa.notifyDataSetChanged();
- srl.setRefreshing(false);
- }
- });
- lv.setOnScrollListener(new AbsListView.OnScrollListener() {
- @Override
- public void onScrollStateChanged(AbsListView absListView, int i) {
- }
- @Override
- public void onScroll(AbsListView absListView, int i, int i1, int i2) {
- if (i2==0){
- return;
- }
- int count =lv.getFooterViewsCount();
- if(count==0){
- if (i+i1==i2){
- if(lv.getFooterViewsCount()==1){
- return;
- }
- lv.addFooterView(v);
- aa.notifyDataSetChanged();
- new Test().execute();
- }
- }
- }
- });
- }
- public class Test extends AsyncTask<Void,Void,Void>{
- @Override
- protected Void doInBackground(Void... voids) {
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onPostExecute(Void aVoid) {
- super.onPostExecute(aVoid);
- for (int a=0;a<20;a++){
- list.add("新增"+a+"数据");
- }
- lv.removeFooterView(v);
- aa.notifyDataSetChanged();
- }
- }
- }