Android 簡單引導頁實現
阿新 • • 發佈:2018-11-29
頁面邏輯
1.使用者開啟App,進入歡迎頁,在歡迎頁中判斷使用者是否第一次使用App(可以用Shared Preference記錄使用者使用狀態)。
2.如果使用者第一次使用App,進入引導頁,使用者可以通過左右滑動檢視當前展示頁面,頁面可以是廣告,也可以是App使用說明。
3.當用戶滑動到最後一頁時,讓指示器消失,同時顯示立即進入按鈕
廢話不多說,直接上乾貨!
1.指示器
這裡我選擇用一個輕量級的viewpager指示器專案地址;https://github.com/ongakuer/CircleIndicator
dependencies {
compile 'me.relex:circleindicator: [email protected]r'
}
2.XML(R.layout.activity_hello2)
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<me.relex.circleindicator.CircleIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
app:ci_drawable="@drawable/dian"
app:ci_drawable_unselected="@drawable/dian_un" />
</RelativeLayout>
3.XML(R.layout.fragment_four)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/wel4">
<ImageView
android:id="@+id/iv_enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="45dp"
android:src="@drawable/enter"/>
</RelativeLayout>
4.Hello2Activity
public class Hello2Activity extends BaseActivity {
private static final String TAB = "Hello2Activity";
private View view1, view2, view3, view4;
private ArrayList<View> viewList = new ArrayList<>();
private PagerAdapter mAdapter;
@BindView(R.id.viewpager)
ViewPager viewpager;
@BindView(R.id.indicator)
CircleIndicator indicator;
@Override
protected void initLayout() {
setContentView(R.layout.activity_hello2);
}
@Override
protected void initData() {
view1 = Utils.getInflater().inflate(R.layout.fragment_one, null);
view2 = Utils.getInflater().inflate(R.layout.fragment_two, null);
view3 = Utils.getInflater().inflate(R.layout.fragment_three, null);
view4 = Utils.getInflater().inflate(R.layout.fragment_four, null);
viewList.add(view1);
viewList.add(view2);
viewList.add(view3);
viewList.add(view4);
mAdapter = new PagerAdapter() {
@Override
public int getCount() {
return viewList.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(viewList.get(position));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewList.get(position));
return viewList.get(position);
}
};
viewpager.setAdapter(mAdapter);
indicator.setViewPager(viewpager);
view4.findViewById(R.id.iv_enter).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it = new Intent(mActivity, MainActivity.class);
startActivity(it);
finish();
}
});
viewpager.setCurrentItem(0);
}
@Override
protected void initView() {
viewpager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
if (position == 3) {
indicator.setVisibility(View.GONE);
} else {
indicator.setVisibility(View.VISIBLE);
}
}
});
}
}
OK,到此一個簡單的引導頁就完成了,感謝^_^