解析AndroidProject 引导页 GuideActivity

  • 效果演示

  

 

  •  布局文件

guide_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".ui.activity.GuideActivity">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/vp_guide_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="30dp" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginBottom="30dp">

        <me.relex.circleindicator.CircleIndicator3
            android:id="@+id/cv_guide_indicator"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            app:ci_drawable="@drawable/guide_indicator_selected"
            app:ci_drawable_unselected="@drawable/guide_indicator_unselected" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_guide_complete"
            style="@style/ButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:minWidth="160dp"
            android:text="了解详情"
            android:visibility="invisible" />

    </FrameLayout>

</FrameLayout>
GuideAcitvity.java
public final class GuideActivity extends AppActivity {

    private ViewPager2 mViewPager;
    private CircleIndicator3 mIndicatorView;
    private View mCompleteView;

    private GuideAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.guide_activity;
    }

    @Override
    protected void initView() {
        mViewPager = findViewById(R.id.vp_guide_pager);
        mIndicatorView = findViewById(R.id.cv_guide_indicator);
        mCompleteView = findViewById(R.id.btn_guide_complete);
        setOnClickListener(mCompleteView);
    }

    @Override
    protected void initData() {
        mAdapter = new GuideAdapter(this);
        mAdapter.addItem(R.drawable.guide_1_bg);
        mAdapter.addItem(R.drawable.guide_2_bg);
        mAdapter.addItem(R.drawable.guide_3_bg);
        mViewPager.setAdapter(mAdapter);
        mViewPager.registerOnPageChangeCallback(mCallback);
        mIndicatorView.setViewPager(mViewPager);
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        if (view == mCompleteView) {
            HomeActivity.start(getContext());
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mViewPager.unregisterOnPageChangeCallback(mCallback);
    }

    private final ViewPager2.OnPageChangeCallback mCallback = new ViewPager2.OnPageChangeCallback() {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (mViewPager.getCurrentItem() == mAdapter.getItemCount() - 1 && positionOffsetPixels > 0) {
                mIndicatorView.setVisibility(View.VISIBLE);
                mCompleteView.setVisibility(View.INVISIBLE);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            if (state == ViewPager2.SCROLL_STATE_IDLE) {
                boolean last = mViewPager.getCurrentItem() == mAdapter.getItemCount() - 1;
                mIndicatorView.setVisibility(last ? View.INVISIBLE : View.VISIBLE);
                mCompleteView.setVisibility(last ? View.VISIBLE : View.INVISIBLE);
            }
        }
    };
}

 

  • ViewPager2

博文参考:

 官方viewPage2
 探索取代ViewPager的ViewPager2

⭐viewPage2基于RecycleView ,自然用法也相类似。引导页实现方式,创建一个Adapter,itemview内放置ImageView,绑定每页的引导页图片。

 

核心代码

mAdapter = new GuideAdapter(this);
        mAdapter.addItem(R.drawable.guide_1_bg);
        mAdapter.addItem(R.drawable.guide_2_bg);
        mAdapter.addItem(R.drawable.guide_3_bg);
        mViewPager.setAdapter(mAdapter);

⭐有关AndroidProject GuideAdatper extends AppAdapter ,extends BaseAdatper 进行一些处理,主要功能是为了实现图片与item布局的绑定,此处代码省略。

 

  • CircleIndicator3 指示器

  引入依赖

// 指示器框架:https://github.com/ongakuer/CircleIndicator
    implementation 'me.relex:circleindicator:2.1.4'

  核心代码

	indicator.setViewPager(viewpager);

 

  • 末页隐藏指示器显示按钮

  registerOnPageChangeCallback 页面切换回调

private final ViewPager2.OnPageChangeCallback mCallback = new ViewPager2.OnPageChangeCallback() {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (mViewPager.getCurrentItem() == mAdapter.getItemCount() - 1 && positionOffsetPixels > 0) {
                mIndicatorView.setVisibility(View.VISIBLE);
                mCompleteView.setVisibility(View.INVISIBLE);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            if (state == ViewPager2.SCROLL_STATE_IDLE) {
                boolean last = mViewPager.getCurrentItem() == mAdapter.getItemCount() - 1;
                mIndicatorView.setVisibility(last ? View.INVISIBLE : View.VISIBLE); //指示器
                mCompleteView.setVisibility(last ? View.VISIBLE : View.INVISIBLE);//按钮
            }
        }
    };
mViewPager.registerOnPageChangeCallback(mCallback);

 

posted @ 2021-07-16 11:10  茄子鱼  阅读(289)  评论(0编辑  收藏  举报