1,在build.gradle下面加
allprojects {
repositories {
google()
jcenter()
maven { url “https://jitpack.io” }
maven { url “https://maven.google.com” }
flatDir {
dirs ‘libs’
}
}
}
2,依赖(比较多)
//导入design,原因是通用广告栏ConvenientBanner使用了里面的元素
implementation ‘com.android.support:design:28.0.0’
implementation ‘com.android.support.constraint:constraint-layout:1.1.3’
testImplementation ‘junit:junit:4.12’
androidTestImplementation ‘com.android.support.test?1.0.2’
androidTestImplementation ‘com.android.support.test.espresso:espresso-core:3.0.2’
//butterKnife,不是必须添加,如果你使用的项目使用的是DataBinding,可以不添加该依赖
implementation ‘com.jakewharton:butterknife:8.8.1’
annotationProcessor ‘com.jakewharton:butterknife-compiler:8.8.1’
//通用广告栏ConvenientBanner
implementation ‘com.bigkoo:ConvenientBanner:2.1.4’
3,引导页Activity的代码
public class GuidePageActivity extends AppCompatActivity {
private ArrayList<Integer> arrayList;
private ConvenientBanner cbTest;
private Button btnTest;
Unbinder unbinder;
public static final String IS_LOGIN = "islogin";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guide_page);
//在上面要接收值,进行一个判断
SharedPreferences a = getSharedPreferences("a", MODE_PRIVATE);
if (a.getBoolean(IS_LOGIN, false)) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
cbTest = findViewById(R.id.cb_test);
btnTest = findViewById(R.id.btn_test);
unbinder = ButterKnife.bind(this);
initView();
initGuidePage();
}
@Override
protected void onDestroy() {
super.onDestroy();
unbinder.unbind();
}
private void initGuidePage() {
cbTest.setPages(new CBViewHolderCreator() {
@Override
public Holder createHolder(View itemView) {
return new LocalImageHolderView(itemView);
}
@Override
public int getLayoutId() {
//设置加载哪个布局
return R.layout.item_guide_page;
}
}, arrayList)
.setPageIndicator(new int[]{R.mipmap.ic_page_indicator, R.mipmap.ic_page_indicator_focused})
.setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
.setPointViewVisible(true)
.setCanLoop(false)
.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
}
@Override
public void onPageSelected(int index) {
//总共添加了三张图片,如果index为2表示到了最后一张图片,隐藏下面的指示器,显示跳转到主页的按钮
if (index == 2) {
btnTest.setVisibility(View.VISIBLE);
cbTest.setPointViewVisible(false);
} else {
btnTest.setVisibility(View.GONE);
cbTest.setPointViewVisible(true);
}
}
});
}
private void initView() {
arrayList = new ArrayList<>();
arrayList.add(R.mipmap.b1);
arrayList.add(R.mipmap.b2);
arrayList.add(R.mipmap.b3);
}
@OnClick(R.id.btn_test)
public void onViewClicked() {
//跳转到主activity
Intent intent = new Intent(GuidePageActivity.this, MainActivity.class);
//这里是首次进入传的值
SharedPreferences a = getSharedPreferences("a", MODE_PRIVATE);
a.edit().putBoolean(IS_LOGIN, true).commit();
startActivity(intent);
}
/**
* 轮播图2 对应的holder
*/
public class LocalImageHolderView extends Holder<Integer> {
private ImageView mImageView;
//构造器
public LocalImageHolderView(View itemView) {
super(itemView);
}
@Override
protected void initView(View itemView) {
mImageView = itemView.findViewById(R.id.iv_guide_page);
mImageView.setScaleType(ImageView.ScaleType.FIT_XY);
}
@Override
public void updateUI(Integer data) {
mImageView.setImageResource(data);
}
}
}
4,图片什么的自己加
5,MainActivity的代码(这里加上了一个点击两次返回键退出App)
private long exitTime = 0;
@Override
public void onBackPressed() {
if ((System.currentTimeMillis() - exitTime) > 2000) {
Toast.makeText(getApplicationContext(), “再按一次退出程序”, Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
//彻底关闭整个APP
int currentVersion = android.os.Build.VERSION.SDK_INT;
if (currentVersion > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
System.exit(0);
} else {// android2.1
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
am.restartPackage(getPackageName());
}
}
}
6,引导页布局
<com.bigkoo.convenientbanner.ConvenientBanner
android:id="@+id/cb_test"
android:layout_width=“match_parent”
android:layout_height=“match_parent” />
<Button
android:id="@+id/btn_test"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="30dp"
android:text="进入app"
android:visibility="gone" />
6,里面需要的布局
<ImageView
android:id="@+id/iv_guide_page"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/ic_launcher" />
整个引导页加首次进入App就是这样了,不喜勿喷!!!