在项目里使用到的引导页
直接上代码
View包下
AbsGuideActivity 类
package view.com.firstviewpage.view;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import java.util.List;
public abstract class AbsGuideActivity extends FragmentActivity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
List guideContent = buildGuideContent();
if (guideContent == null)
{
return;
}
FrameLayout container = new FrameLayout(this);
ViewPager pager = new ViewPager(this);
pager.setId(getPagerId());
container.addView(pager, new ViewGroup.LayoutParams(-1,
-1));
setContentView(container);
FragmentPagerAdapter adapter = new FragmentTabAdapter(this, guideContent);
pager.setAdapter(adapter);
// GuideView guideView = new GuideView(this, guideContent, drawDot(), dotDefault(), dotSelected());
// pager.setOnPageChangeListener(guideView);
//
// container.addView(guideView, new FrameLayout.LayoutParams(-1,
// -1));
}
public abstract List<SinglePage> buildGuideContent();
// public abstract boolean drawDot();
//
// public abstract Bitmap dotDefault();
//
// public abstract Bitmap dotSelected();
public abstract int getPagerId();
}
View包下
FragmentTabAdapter 适配
package view.com.firstviewpage.view;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.util.List;
public class FragmentTabAdapter extends FragmentPagerAdapter
{
private List<SinglePage> mGuideContent;
private Context mCtx;
public FragmentTabAdapter(FragmentActivity a, List<SinglePage> guideContent)
{
super(a.getSupportFragmentManager());
this.mCtx = a;
this.mGuideContent = guideContent;
}
public Fragment getItem(int position)
{
SinglePage sp = (SinglePage)this.mGuideContent.get(position);
if (sp.mCustomFragment != null)
{
return sp.mCustomFragment;
}
PageFragment pageFragment = (PageFragment)Fragment.instantiate(this.mCtx, PageFragment.class.getName());
if (sp.mBackground != null) {
pageFragment.setBg(sp.mBackground);
}
return pageFragment;
}
public int getCount()
{
return this.mGuideContent.size();
}
public static final class PageFragment extends Fragment
{
private Drawable mBg;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
ImageView iv = new ImageView(getActivity());
if (this.mBg != null) {
iv.setBackground(this.mBg);
}
return iv;
}
public void setBg(Drawable mBackground) {
this.mBg = mBackground;
}
}
}
View 包下
SinglePage
package view.com.firstviewpage.view;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
/**
* Created by onebot on 2016/12/16.
*/
public class SinglePage
{
public Drawable mBackground;
// public List<SingleElement> mElementsList = new ArrayList();
public Fragment mCustomFragment;
}
fragment
package view.com.firstviewpage;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
public class EntryFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_entry, null);
v.findViewById(R.id.btn_entry).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ExampleGuideActivity activity = (ExampleGuideActivity) getActivity();
activity.entryApp();
}
});
return v;
}
}
ExampleGuideActivity 引导页面
package view.com.firstviewpage;
import java.util.ArrayList;
import java.util.List;
import view.com.firstviewpage.view.AbsGuideActivity;
import view.com.firstviewpage.view.SinglePage;
public class ExampleGuideActivity extends AbsGuideActivity {
@Override
public List<SinglePage> buildGuideContent() {
// prepare the information for our guide
List<SinglePage> guideContent = new ArrayList<SinglePage>();
SinglePage page01 = new SinglePage();
page01.mBackground = getResources().getDrawable(R.drawable.bg_page_01);
guideContent.add(page01);
SinglePage page02 = new SinglePage();
page02.mBackground = getResources().getDrawable(R.drawable.bg_page_02);
guideContent.add(page02);
SinglePage page03 = new SinglePage();
page03.mBackground = getResources().getDrawable(R.drawable.bg_page_03);
guideContent.add(page03);
SinglePage page04 = new SinglePage();
page04.mBackground = getResources().getDrawable(R.drawable.bg_page_04);
guideContent.add(page04);
SinglePage page05 = new SinglePage();
page05.mCustomFragment = new EntryFragment();
guideContent.add(page05);
return guideContent;
}
// @Override
// public Bitmap dotDefault() {
// return BitmapFactory.decodeResource(getResources(), R.drawable.ic_dot_default);
// }
//
// @Override
// public Bitmap dotSelected() {
// return BitmapFactory.decodeResource(getResources(), R.drawable.ic_dot_selected);
// }
//
// @Override
// public boolean drawDot() {
// return true;
// }
public void entryApp() {
// Time to entry your app! We just finish the activity, replace it with
// your code.
finish();
}
/**
* You need provide an id to the pager. You could define an id in
* values/ids.xml and use it.
*/
@Override
public int getPagerId() {
return R.id.guide_container;
}
}
MainActivity
package view.com.firstviewpage;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_start).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, ExampleGuideActivity.class);
startActivity(i);
}
});
}
}
布局和其他文件 我会在后面放一个demo 大家可以自己下载 很简单的引导页
demo demo ec或as都可以