Splash :
xml:
activity:
public class SplashActivity extends BaseActivity {
private static final long DELAY_TIME = 3000L;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(SplashActivity.this,MainActivity.class));
finish();
}
},DELAY_TIME);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
ViewPager的加载:
首先我们要新建一个WelcomeActivity的页面来搭载viewpager 以及原点指示器:
布局选择当然是帧布局了
xml:
WelcomeActivity:
public class WelcomeActivity extends FragmentActivity{
private static final String TAG = "WelcomeActivity";
private ViewPager vp_main;
private LinearLayout ll_indicator;
private ViewPagerAdapter adapter;
private List fragmentList = new ArrayList();
private ImmersionBar mImmersionBar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//判断是否是第一次登录
if(SpUtils.getSp(this,"Login",false)){
startActivity(new Intent(this,MainActivity.class));
finish();
}
mImmersionBar = ImmersionBar.with(this);
mImmersionBar.init(); //所有子类都将继承这些相同的属性
setContentView(R.layout.activity_welcome);
initView();
initDicator();
}
//初始化芝士条
private void initDicator() {
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,10f,getResources().getDisplayMetrics());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width,width);
lp.setMargins(4,0,4,0);
for(int i=0;i
Drawable.xml
中间的难点就是fragment的创建以及怎么能判断哪个fragment对应哪一个。
我们用到了这个
Bundle bundle = new Bundle();
bundle.putInt("index",i);
fragment.setArguments(bundle);
来传递信息;
在我们的ContFragment中:
public class ContentFragment extends Fragment {
private static final String TAG = "ContentFragment";
private Button bt_open;
private ImageView iv_content;
private int[] BgImg = {R.drawable.splash,R.drawable.cotent,R.drawable.open};
public ContentFragment(){
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_content,null);
iv_content = (ImageView) view.findViewById(R.id.iv_content);
bt_open = (Button) view.findViewById(R.id.bt_open);
int index = getArguments().getInt("index");
// iv_content.setBackgroundResource(BgImg[index]);
iv_content.setImageResource(BgImg[index]);
Log.e(TAG,""+index);
bt_open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getActivity().startActivity(new Intent(getActivity(),MainActivity.class));
getActivity().finish();
SpUtils.setSp(getActivity(),"Login",true);
}
});
bt_open.setVisibility(index == 2 ? View.VISIBLE:View.GONE);
return view;
}
}
xml:
在其中我遇到了fragment加载不出来图片加载出来全是白色
原因是ViewPagerAdapter写错了:
正确的:
public class ViewPagerAdapter extends FragmentPagerAdapter{
private List mlist;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public ViewPagerAdapter(FragmentManager fm,List mlist) {
super(fm);
this.mlist = mlist;
}
@Override
public int getCount() {
return mlist.size();
}
@Override
public Fragment getItem(int position) {
return mlist.get(position);
}
}
而我原先继承的是PagerAdapter导致多了这个方法
@Override
public boolean isViewFromObject(View view, Object object) {
return false;
}
只要改为FragmentAdapter并且删除这个方法就行
那我的BaseActivity是啥呢?
public class BaseActivity extends AppCompatActivity {
private ImmersionBar mImmersionBar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mImmersionBar = ImmersionBar.with(this);
mImmersionBar.init(); //所有子类都将继承这些相同的属性
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mImmersionBar != null)
mImmersionBar.destroy(); //必须调用该方法,防止内存泄漏,不调用该方法,如果界面bar发生改变,在不关闭app的情况下,退出此界面再进入将记忆最后一次bar改变的状态
}
}
这是一个沉浸式框架 ,具体看
gitHub地址:https://github.com/gyf-dev/ImmersionBar