android导航页制作appintro

1、注入依赖
project:

 allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
}

app:

  implementation 'com.github.apl-devs:appintro:v4.2.2'

2、①需要注册Activity(manifest钟注册AppIntroActivity)

public class AppIntroActivity extends AppIntro {
    @Override
    public void init(@Nullable Bundle savedInstanceState) {

    addSlide(SlideFragment.newInstance(R.layout.layout01));
    addSlide(SlideFragment.newInstance(R.layout.layout02));
    addSlide(SlideFragment.newInstance(R.layout.layout03));
    addSlide(SlideFragment.newInstance(R.layout.layout04));
    addSlide(SlideFragment.newInstance(R.layout.layout05));
    setSeparatorColor(getResources().getColor(R.color.colorAccent));
    setVibrateIntensity(30);
    setDoneText("完成");
    setSkipText("跳过");
}



@Override
public void onSkipPressed() {
    //当执行跳过动作时触发
    Intent intent = new Intent(AppIntroActivity.this, MainActivity.class);
    startActivity(intent);
    SpUtils.putBoolean(AppIntroActivity.this, AppConstants.FIRST_OPEN, true);
    finish();
}

@Override
public void onNextPressed() {
    //当执行下一步动作时触发
}

@Override
public void onDonePressed() {
    //当执行完成动作时触发
    Intent intent = new Intent(AppIntroActivity.this, MainActivity.class);
    startActivity(intent);
    SpUtils.putBoolean(AppIntroActivity.this, AppConstants.FIRST_OPEN, true);
    finish();
}

@Override
public void onSlideChanged() {
    //当执行变化动作时触发
}

}

②MainActicity中使用
// 判断是否是第一次开启应用

boolean isFirstOpen = SpUtils.getBoolean(this, AppConstants.FIRST_OPEN);
Log.d("fenxi", "onCreate: " + isFirstOpen);
// 如果是第一次启动,则先进入功能引导页
if (!isFirstOpen) {
    Intent intent = new Intent(this, AppIntroActivity.class);
    startActivity(intent);
    finish();
    return;
}

③AppConstants类

public class AppConstants {
    public static final String FIRST_OPEN = "first_open";
}

④SlideFragment类

public class SlideFragment extends Fragment {
    private static final String ARG_LAYOUT_RES_ID = "layoutResId";

public static SlideFragment newInstance(int layoutResId) {
    SlideFragment slideFragment = new SlideFragment();

    Bundle args = new Bundle();
    args.putInt(ARG_LAYOUT_RES_ID, layoutResId);
    slideFragment.setArguments(args);

    return slideFragment;
}

private int layoutResId;

public SlideFragment() {
}

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

    if (getArguments() != null && getArguments().containsKey(ARG_LAYOUT_RES_ID))
        layoutResId = getArguments().getInt(ARG_LAYOUT_RES_ID);
}

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

⑤SpUtils类

public class SpUtils {

private static final String spFileName = "app";

public static String getString(Context context, String strKey) {
    SharedPreferences setPreferences = context.getSharedPreferences(
            spFileName, Context.MODE_PRIVATE);
    String result = setPreferences.getString(strKey, "");
    return result;
}

public static String getString(Context context, String strKey,
                               String strDefault) {
    SharedPreferences setPreferences = context.getSharedPreferences(
            spFileName, Context.MODE_PRIVATE);
    String result = setPreferences.getString(strKey, strDefault);
    return result;
}

public static void putString(Context context, String strKey, String strData) {
    SharedPreferences activityPreferences = context.getSharedPreferences(
            spFileName, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = activityPreferences.edit();
    editor.putString(strKey, strData);
    editor.commit();
}

public static Boolean getBoolean(Context context, String strKey) {
    SharedPreferences setPreferences = context.getSharedPreferences(
            spFileName, Context.MODE_PRIVATE);
    Boolean result = setPreferences.getBoolean(strKey, false);
    return result;
}

public static Boolean getBoolean(Context context, String strKey,
                                 Boolean strDefault) {
    SharedPreferences setPreferences = context.getSharedPreferences(
            spFileName, Context.MODE_PRIVATE);
    Boolean result = setPreferences.getBoolean(strKey, strDefault);
    return result;
}


public static void putBoolean(Context context, String strKey,
                              Boolean strData) {
    SharedPreferences activityPreferences = context.getSharedPreferences(
            spFileName, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = activityPreferences.edit();
    editor.putBoolean(strKey, strData);
    editor.commit();
}

public static int getInt(Context context, String strKey) {
    SharedPreferences setPreferences = context.getSharedPreferences(
            spFileName, Context.MODE_PRIVATE);
    int result = setPreferences.getInt(strKey, -1);
    return result;
}

public static int getInt(Context context, String strKey, int strDefault) {
    SharedPreferences setPreferences = context.getSharedPreferences(
            spFileName, Context.MODE_PRIVATE);
    int result = setPreferences.getInt(strKey, strDefault);
    return result;
}

public static void putInt(Context context, String strKey, int strData) {
    SharedPreferences activityPreferences = context.getSharedPreferences(
            spFileName, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = activityPreferences.edit();
    editor.putInt(strKey, strData);
    editor.commit();
}

public static long getLong(Context context, String strKey) {
    SharedPreferences setPreferences = context.getSharedPreferences(
            spFileName, Context.MODE_PRIVATE);
    long result = setPreferences.getLong(strKey, -1);
    return result;
}

public static long getLong(Context context, String strKey, long strDefault) {
    SharedPreferences setPreferences = context.getSharedPreferences(
            spFileName, Context.MODE_PRIVATE);
    long result = setPreferences.getLong(strKey, strDefault);
    return result;
}

public static void putLong(Context context, String strKey, long strData) {
    SharedPreferences activityPreferences = context.getSharedPreferences(
            spFileName, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = activityPreferences.edit();
    editor.putLong(strKey, strData);
    editor.commit();
}
}

⑥XML(导航对应的01、02、03、04、05)




    

你可能感兴趣的:(android)