第四十七天 下拉刷新、手勢(雙擊、移動等)

下拉刷新

佈局文件:

<com.example.administrator.mytouthevent.MyRefreshLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
</com.example.administrator.mytouthevent.MyRefreshLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textview_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下拉刷新"
        android:gravity="center"
        android:padding="10dp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listview_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">
</ListView>

MyRefreshLayout

public class MyRefreshLayout extends FrameLayout {
    private ListView content;
    public MyRefreshLayout(Context context) {
        super(context);
    }

    public MyRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View header=inflater.inflate(R.layout.activity_refresh,null);
        addView(header);
        content= (ListView) inflater.inflate(R.layout.activity_listview,null);
        addView(content);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1
                ,new String[]{"a","b","c","d","e","f","g","h","i","g","k","l","m","n"});
        content.setAdapter(adapter);
    }

    public MyRefreshLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if(ev.getAction()==MotionEvent.ACTION_DOWN){
            Log.d("mytouch",""+ev.getAction()+super.dispatchTouchEvent(ev));
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if(ev.getAction()==MotionEvent.ACTION_DOWN){
            Log.d("mytouch",""+ev.getAction()+super.onInterceptTouchEvent(ev));
        }
        if(content.getFirstVisiblePosition()==0){
            View firstView=content.getChildAt(content.getFirstVisiblePosition());
            if(firstView.getY()>=0){
                Log.d("mytouch","攔截到事件");
            }
            return  true;
        }
        return super.onInterceptTouchEvent(ev);
    }
    float y=0;
    float oldY=0;
    float offest=0;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d("mytouch","運行到事件處理"+(event.getAction()==MotionEvent.ACTION_MOVE));
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                oldY=event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                y=event.getY();
                offest=y-oldY;
                content.setTranslationY(content.getTranslationY()+offest);
                oldY=y;
                invalidate();
            case MotionEvent.ACTION_UP:
                ObjectAnimator.ofFloat(content,"translationY",content.getTranslationY(),0).setDuration(1000).start();
                break;
        }
        return true;
    }
}

這裏寫圖片描述

谷歌推出的下拉刷新

導入包support-v4-22.2.0

項目

佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
            android:id="@+id/listview1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
activity_swipe
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
            android:id="@+id/listview1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
MainActivity
public class MainActivity extends Activity {
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private ListView mListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         setContentView(R.layout.activity_swipe);
        mListView= (ListView) findViewById(R.id.listview1);
        mSwipeRefreshLayout= (SwipeRefreshLayout) findViewById(R.id.swipe);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,new String[]{"a","b","c","d","e","f","g","h","i","g","k","l","m","n"});
        mListView.setAdapter(adapter);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mSwipeRefreshLayout.setRefreshing(false);
            }
        });
    }
}

手勢

佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.example.administrator.mytouthevent.MyButton
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="雙擊"/>
</LinearLayout>

MyButton

public class MyButton extends Button {
    //觀察者模式
    interface OnDoubleClick{
        public void onDoubleClick(View view);
    }
    private OnDoubleClick onDoubleClickListener;

    public void setOnDoubleClickListener(OnDoubleClick onDoubleClickListener) {
        this.onDoubleClickListener = onDoubleClickListener;
    }

    private GestureDetector mGestureDetector;
    public MyButton(Context context) {
        super(context);
    }

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector=new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
            @Override
            public boolean onDoubleTap(MotionEvent e) {
                if(onDoubleClickListener!=null){
                    onDoubleClickListener.onDoubleClick(MyButton.this);
                }
                Log.d("click","連續點擊了兩次");
                return true;
            }

            //按鈕在X方向上移動
//            @Override
//            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//                if(Math.abs(e2.getX()-e1.getX())>50){
//                    ObjectAnimator.ofFloat(MyButton.this,"translationX",getTranslationX(),getTranslationX()+e2.getX()-e1.getX()).setDuration(1000).start();
//                    return true;
//                }
//                return super.onFling(e1, e2, velocityX, velocityY);
//            }

            //按鈕可以移動至屏幕任意位置,但不能劃出屏幕(不能超過LinearLayout的範圍)
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                setTranslationX(getTranslationX()+e2.getX()-e1.getX());
                setTranslationY(getTranslationY()+e2.getY()-e1.getY());
                return true;
            }
        });
    }

    public MyButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mGestureDetector.onTouchEvent(event);//注意此處不能漏寫
        return super.onTouchEvent(event);
    }
}

MainActivity

public class MainActivity extends Activity {
    private MyButton mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button);
        mButton= (MyButton) findViewById(R.id.button);
        mButton.setOnDoubleClickListener(new MyButton.OnDoubleClick() {
            @Override
            public void onDoubleClick(View view) {

            }
        });
    }
}

這裏寫圖片描述

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章