實現ViewFlipper帶圓點導航,手勢切換頁面

直接上代碼:
佈局文件:android

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ViewFlipper
        android:id="@+id/fragment_home_viewflipper"
        android:inAnimation="@anim/push_left_in"
        android:outAnimation="@anim/push_left_out"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:background="@color/colorBackgroud"
        android:layout_alignParentTop="true">
    </ViewFlipper>

    <LinearLayout
        android:id="@+id/fragment_home_viewflipper_roundLayout"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_marginTop="100dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

    </LinearLayout>

</RelativeLayout>

動畫以下: push_left_in.xmlapp

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="100%p" android:toXDelta="0"
        android:duration="500" />
    <alpha android:fromAlpha="0.1" android:toAlpha="1.0"
        android:duration="500" />
</set>

push_left_out.xmlide

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-100%p"
        android:duration="500" />
    <alpha android:fromAlpha="1.0" android:toAlpha="0.1"
        android:duration="500" />
</set>

push_right_in.xml佈局

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="-100%p" android:toXDelta="0"
        android:duration="500" />
    <alpha android:fromAlpha="0.1" android:toAlpha="1.0"
        android:duration="500" />
</set>

push_right_out.xml動畫

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="100%p"
        android:duration="500" />
    <alpha android:fromAlpha="1.0" android:toAlpha="0.1"
        android:duration="500" />
</set>

對應的Fragment代碼以下:this

public class FragmentHome extends Fragment {
    private static final String TAG = "Fragment_home";
    private ViewFlipper viewFlipper;
    private LinearLayout viewFlipper_roundLayout;
    private ImageView[] imageViews;//viewflipper圓點
    private float startX;// 手指觸摸屏幕的x軸座標
    private float endX;

    public static FragmentHome newInstance() {
        FragmentHome fragment = new FragmentHome();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        viewFlipper = getActivity().findViewById(R.id.fragment_home_viewflipper);
        viewFlipper_roundLayout = getActivity().findViewById(R.id.fragment_home_viewflipper_roundLayout);


        initViewFlipper();
    }

    //初始化viewflipper
    private void initViewFlipper() {
        //初始化圖片
        int viewFlipperImageViews[] = {R.drawable.demo1, R.drawable.demo2, R.drawable.demo3};
        ImageView viewFlipperImageView = null;
        for(int i=0; i<3; i++){
            viewFlipperImageView = new ImageView(getActivity());
            viewFlipperImageView.setBackgroundResource(viewFlipperImageViews[i]);
            viewFlipper.addView(viewFlipperImageView);
        }
        viewFlipper.setFlipInterval(Settings.fragment_home_viewflipper_change_time);
        viewFlipper.startFlipping();

        //設置viewFlipper圖片切換的圓點
        imageViews = new ImageView[viewFlipper.getChildCount()];
        ImageView view = null;
        for (int i = 0; i < imageViews.length; i++) {
            view = new ImageView(getActivity());
            imageViews[i] = view;
            if(i == 0){
                view.setBackgroundResource(R.drawable.ic_round_12dp_checked);
            }else{
                view.setBackgroundResource(R.drawable.ic_round_12dp_unchecked);
            }
            viewFlipper_roundLayout.addView(imageViews[i]);
        }

        Animation.AnimationListener viewFlipperAnimation = new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) { }

            @Override
            public void onAnimationEnd(Animation animation) {
                setViewFlipperIndexRound();
            }

            @Override
            public void onAnimationRepeat(Animation animation) { }
        };
        viewFlipper.getInAnimation().setAnimationListener(viewFlipperAnimation);
        viewFlipper.getOutAnimation().setAnimationListener(viewFlipperAnimation);

        MainActivity.MyTouchListener myTouchListener = new MainActivity.MyTouchListener() {
            @Override
            public void onTouchEvent(MotionEvent event) {
                //限定viewflipper區域
                if(event.getY() >= (viewFlipper.getHeight() + 100)){
                    return;
                }
                switch (event.getAction()) {
                    // 手指接觸屏幕
                    case MotionEvent.ACTION_DOWN: {
                        startX = event.getX(); // 將手指接觸屏幕的X軸的座標保存下來
                        break;
                    }

                    // 手指滑動屏幕
                    case MotionEvent.ACTION_UP: {
                        //向右滑動
                        if (event.getX() - startX > 100) {
                            Log.e(TAG, "向右滑動");
                            viewFlipper.stopFlipping();
                            viewFlipper.setInAnimation(getActivity(), R.anim.push_right_in);
                            viewFlipper.setOutAnimation(getActivity(), R.anim.push_right_out);
                            viewFlipper.showPrevious();
                            setViewFlipperIndexRound();
                        }
                        //向左滑動
                        if (startX - event.getX() > 100) {
                            Log.e(TAG, "向左滑動");
                            viewFlipper.stopFlipping();
                            viewFlipper.setInAnimation(getActivity(), R.anim.push_left_in);
                            viewFlipper.setOutAnimation(getActivity(), R.anim.push_left_out);
                            viewFlipper.showNext();
                            setViewFlipperIndexRound();
                        }
                        break;
                    }
                }
            }
        };

        // 將myTouchListener註冊到分發列表
        ((MainActivity)this.getActivity()).registerMyTouchListener(myTouchListener);

        viewFlipper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), viewFlipper.getDisplayedChild()+"", Toast.LENGTH_LONG).show();
            }
        });
    }

    //設置viewflipper的圓點index
    private void setViewFlipperIndexRound() {
        int index = viewFlipper.getDisplayedChild();
        for (int i = 0; i < imageViews.length; i++) {
            imageViews[index].setBackgroundResource(R.drawable.ic_round_12dp_checked);
            if (index != i) {
                imageViews[i].setBackgroundResource(R.drawable.ic_round_12dp_unchecked);
            }
        }
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }


}

其中MainActivity增長以下代碼:spa

public interface MyTouchListener {
        public void onTouchEvent(MotionEvent event);
    }

    // 保存MyTouchListener接口的列表
    private ArrayList<MyTouchListener> myTouchListeners = new ArrayList<MainActivity.MyTouchListener>();

    /**
     * 提供給Fragment經過getActivity()方法來註冊本身的觸摸事件的方法
     * @param listener
     */
    public void registerMyTouchListener(MyTouchListener listener) {
        myTouchListeners.add(listener);
    }

    /**
     * 提供給Fragment經過getActivity()方法來取消註冊本身的觸摸事件的方法
     * @param listener
     */
    public void unRegisterMyTouchListener(MyTouchListener listener) {
        myTouchListeners.remove( listener );
    }

    /**
     * 分發觸摸事件給全部註冊了MyTouchListener的接口
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        for (MyTouchListener listener : myTouchListeners) {
            listener.onTouchEvent(ev);
        }
        return super.dispatchTouchEvent(ev);
    }

以上就能夠實現帶有小圓點的輪播了。code

相關文章
相關標籤/搜索