android开发:ViewFlipper实现图片轮播

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_39027256/article/details/102643309

效果图:

    

 上代码:

Activity:

package com.example.ViewFlipper;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ViewFlipper;

import com.example.recyclearapplication.R;

public class ViewFlipperActivity extends AppCompatActivity {
    private GestureDetector mDetector;
    private ViewFlipper mViewFlipper;
    private RadioGroup mRadiioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_flipper);
        initView();
        initData();

    }

    public void initView() {
        mViewFlipper = (ViewFlipper) findViewById(R.id.vflp_help);
        mRadiioGroup = findViewById(R.id.radio_group);
    }

    public void initData() {
        //创建手势识别对象,传入我们的回调监听对象
        mDetector = new GestureDetector(this, new MyGestureDetector());
        RadioButton radioButton = (RadioButton) mRadiioGroup.getChildAt(0);
        mRadiioGroup.setEnabled(false);
        radioButton.setChecked(true);
    }

    /**
     * 手势回调方法
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mDetector.onTouchEvent(event);
    }

    /**
     * 手势监听类
     */
    class MyGestureDetector implements GestureDetector.OnGestureListener {

        @Override
        public boolean onDown(MotionEvent motionEvent) {
            return false;
        }

        @Override
        public void onShowPress(MotionEvent motionEvent) {

        }

        @Override
        public boolean onSingleTapUp(MotionEvent motionEvent) {
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
            return false;
        }

        @Override
        public void onLongPress(MotionEvent motionEvent) {

        }

        @Override
        public boolean onFling(MotionEvent m1, MotionEvent m2, float v, float v1) {
            //左滑
            if (m1.getX() > m2.getX()) {
                //左滑后下一张进来的动画
                mViewFlipper.setInAnimation(ViewFlipperActivity.this, R.anim.right_in);
                //左滑当前页面消息的动画
                mViewFlipper.setOutAnimation(ViewFlipperActivity.this, R.anim.right_out);
                //下一张
                mViewFlipper.showNext();
                //mViewFlipper.getDisplayedChild()获取当前展示的position
                RadioButton radioButton = (RadioButton) mRadiioGroup.getChildAt(mViewFlipper.getDisplayedChild());
                radioButton.setChecked(true);
                Log.e("TAG", "" + mViewFlipper.getDisplayedChild());
            }
            //右滑
            else if (m1.getX() < m2.getX()) {
                //右滑后下一张进来的动画
                mViewFlipper.setInAnimation(ViewFlipperActivity.this, R.anim.left_in);
                //右滑当前页面消息的动画
                mViewFlipper.setOutAnimation(ViewFlipperActivity.this, R.anim.left_out);
                //上一张
                mViewFlipper.showPrevious();
                RadioButton radioButton = (RadioButton) mRadiioGroup.getChildAt(mViewFlipper.getDisplayedChild());
                radioButton.setChecked(true);
                Log.e("TAG", "" + mViewFlipper.getDisplayedChild());

            }
            return false;
        }
    }
}

xml:

<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=".MainActivity">

    <ViewFlipper
        android:id="@+id/vflp_help"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        >

        <include layout="@layout/page_help_one" />

        <include layout="@layout/page_help_two" />

        <include layout="@layout/page_help_three" />

        <include layout="@layout/page_help_four" />

    </ViewFlipper>

    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="50dp"
        android:orientation="horizontal">

        <RadioButton
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:enabled="false"></RadioButton>

        <RadioButton
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:enabled="false"></RadioButton>

        <RadioButton
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:enabled="false"></RadioButton>

        <RadioButton
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:enabled="false"></RadioButton>
    </RadioGroup>
</RelativeLayout>

github地址:https://github.com/David-lvfujiang/RecyclearApplication

猜你喜欢

转载自blog.csdn.net/qq_39027256/article/details/102643309
今日推荐