如果你明明知道这个故事的结局,你或者选择说出来,或者装作不知道,万不要欲言又止。有时候留给别人的伤害,选择沉默比选择坦白要痛多了。
SelectorActivity.java代码:
package siso.slidingt.selector;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import siso.slidingt.MainActivity;
import siso.slidingt.R;
public class SelectorActivity extends ListActivity implements AdapterView.OnItemClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(R.string.app_name);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.options, android.R.layout.simple_list_item_1);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
MainActivity.start(this);
break;
case 1:
MainActivity.start(this);
break;
}
}
}
MainActivity.java代码:
package siso.slidingt
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.annotation.LayoutRes
import android.support.annotation.NonNull
import android.support.v4.content.ContextCompat
import android.view.View
import android.widget.Toast
import siso.stlib.Direction
import siso.stlib.IndicatorOptions
import siso.stlib.PageOptions
import siso.stlib.TransformItem
import siso.stlib.TutorialFragment
import siso.stlib.TutorialOptions
import siso.stlib.TutorialPageOptionsProvider
public class MainActivity extends Activity implements View.OnClickListener {
private static final int TOTAL_PAGES = 6
private static final int ACTUAL_PAGES_COUNT = 3
private int[] mPagesColors
public static void start(Context context) {
context.startActivity(new Intent(context, MainActivity.class))
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById(R.id.bRetry).setOnClickListener(this)
mPagesColors = new int[]{
ContextCompat.getColor(this, android.R.color.darker_gray),
ContextCompat.getColor(this, android.R.color.holo_green_dark),
ContextCompat.getColor(this, android.R.color.holo_red_dark),
ContextCompat.getColor(this, android.R.color.holo_blue_dark),
ContextCompat.getColor(this, android.R.color.holo_purple),
ContextCompat.getColor(this, android.R.color.holo_orange_dark),
}
if (savedInstanceState == null) {
replaceTutorialFragment()
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bRetry:
replaceTutorialFragment()
break
}
}
public void replaceTutorialFragment() {
final IndicatorOptions indicatorOptions = IndicatorOptions.newBuilder(this)
.build()
final TutorialOptions tutorialOptions = TutorialFragment.newTutorialOptionsBuilder(this)
.setUseAutoRemoveTutorialFragment(true)
.setUseInfiniteScroll(true)
.setPagesColors(mPagesColors)
.setPagesCount(TOTAL_PAGES)
.setIndicatorOptions(indicatorOptions)
.setTutorialPageProvider(new TutorialPagesProvider())
.setOnSkipClickListener(new OnSkipClickListener(this))
.build()
final TutorialFragment tutorialFragment = TutorialFragment.newInstance(tutorialOptions)
getFragmentManager()
.beginTransaction()
.replace(R.id.container, tutorialFragment)
.commit()
}
private static final class TutorialPagesProvider implements TutorialPageOptionsProvider {
@NonNull
@Override
public PageOptions provide(int position) {
@LayoutRes int pageLayoutResId
TransformItem[] tutorialItems
position %= ACTUAL_PAGES_COUNT
switch (position) {
case 0: {
pageLayoutResId = R.layout.fragment_page_first
tutorialItems = new TransformItem[]{
TransformItem.create(R.id.ivFirstImage, Direction.LEFT_TO_RIGHT, 0.20f),
TransformItem.create(R.id.ivSecondImage, Direction.RIGHT_TO_LEFT, 0.06f),
TransformItem.create(R.id.ivThirdImage, Direction.LEFT_TO_RIGHT, 0.08f),
TransformItem.create(R.id.ivFourthImage, Direction.RIGHT_TO_LEFT, 0.1f),
TransformItem.create(R.id.ivFifthImage, Direction.RIGHT_TO_LEFT, 0.03f),
TransformItem.create(R.id.ivSixthImage, Direction.RIGHT_TO_LEFT, 0.09f),
TransformItem.create(R.id.ivSeventhImage, Direction.RIGHT_TO_LEFT, 0.14f),
TransformItem.create(R.id.ivEighthImage, Direction.RIGHT_TO_LEFT, 0.07f)
}
break
}
case 1: {
pageLayoutResId = R.layout.fragment_page_third
tutorialItems = new TransformItem[]{
TransformItem.create(R.id.ivFirstImage, Direction.RIGHT_TO_LEFT, 0.20f),
TransformItem.create(R.id.ivSecondImage, Direction.LEFT_TO_RIGHT, 0.06f),
TransformItem.create(R.id.ivThirdImage, Direction.RIGHT_TO_LEFT, 0.08f),
TransformItem.create(R.id.ivFourthImage, Direction.LEFT_TO_RIGHT, 0.1f),
TransformItem.create(R.id.ivFifthImage, Direction.LEFT_TO_RIGHT, 0.03f),
TransformItem.create(R.id.ivSixthImage, Direction.LEFT_TO_RIGHT, 0.09f),
TransformItem.create(R.id.ivSeventhImage, Direction.LEFT_TO_RIGHT, 0.14f)
}
break
}
case 2: {
pageLayoutResId = R.layout.fragment_page_second
tutorialItems = new TransformItem[]{
TransformItem.create(R.id.ivFirstImage, Direction.RIGHT_TO_LEFT, 0.2f),
TransformItem.create(R.id.ivSecondImage, Direction.LEFT_TO_RIGHT, 0.06f),
TransformItem.create(R.id.ivThirdImage, Direction.RIGHT_TO_LEFT, 0.08f),
TransformItem.create(R.id.ivFourthImage, Direction.LEFT_TO_RIGHT, 0.1f),
TransformItem.create(R.id.ivFifthImage, Direction.LEFT_TO_RIGHT, 0.03f),
TransformItem.create(R.id.ivSixthImage, Direction.LEFT_TO_RIGHT, 0.09f),
TransformItem.create(R.id.ivSeventhImage, Direction.LEFT_TO_RIGHT, 0.14f),
TransformItem.create(R.id.ivEighthImage, Direction.LEFT_TO_RIGHT, 0.07f)
}
break
}
default: {
throw new IllegalArgumentException("Unknown position: " + position)
}
}
return PageOptions.create(pageLayoutResId, position, tutorialItems)
}
}
private static final class OnSkipClickListener implements View.OnClickListener {
@NonNull
private final Context mContext
OnSkipClickListener(@NonNull Context context) {
mContext = context.getApplicationContext()
}
@Override
public void onClick(View v) {
Toast.makeText(mContext, "跳过按钮单击了", Toast.LENGTH_SHORT).show()
}
}
}
AndroidManifest.xml内容:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="siso.slidingt">
<application
android:allowBackup="true"
android:name=".SampleApplication"
android:icon="@mipmap/s_0_1"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".support.MainActivity"
android:label="@string/st_app_name"/>
<activity
android:name=".MainActivity"
android:label="@string/st_app_name"/>
<activity
android:name=".selector.SelectorActivity"
android:label="@string/st_app_name"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "siso.slidingt"
minSdkVersion 22
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5'
compile project(path: ':stlib')
}
项目结构如图:
依赖库项目结构如图:
库项目文件里的build.gradle
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 22
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:percent:23.0.1'
}
项目运行结果如图: