Android 启动页 Splash 和 Viewpager
2018-07-22 本文已影响440人
Zebraaa
Splash :
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:scaleType="fitXY"
android:background="@drawable/splash"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
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:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/vp_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/ll_indicator"
android:layout_width="wrap_content"
android:background="@android:color/transparent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="60dp"
android:orientation="horizontal"
>
</LinearLayout>
</FrameLayout>
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<Fragment> fragmentList = new ArrayList<Fragment>();
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<fragmentList.size();i++){
View view = new View(this);
view.setId(i);
view.setBackgroundResource( i==0?R.drawable.dot_press_shap:R.drawable.dot_normal_shap);
view.setLayoutParams(lp);
ll_indicator.addView(view,i);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mImmersionBar != null)
mImmersionBar.destroy(); //必须调用该方法,防止内存泄漏,不调用该方法,如果界面bar发生改变,在不关闭app的情况下,退出此界面再进入将记忆最后一次bar改变的状态
}
private void initView() {
vp_main = (ViewPager) findViewById(R.id.vp_main);
ll_indicator = (LinearLayout) findViewById(R.id.ll_indicator);
//创建fragment
for(int i=0;i<3;i++){
ContentFragment fragment = new ContentFragment();
Bundle bundle = new Bundle();
bundle.putInt("index",i);
Log.e(TAG,""+i);
fragment.setArguments(bundle);
fragmentList.add(fragment);
}
adapter = new ViewPagerAdapter(getSupportFragmentManager(),fragmentList);
vp_main.setAdapter(adapter);
vp_main.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
for(int i = 0;i<fragmentList.size();i++){
ll_indicator.getChildAt(i).setBackgroundResource(position == i?R.drawable.dot_press_shap:R.drawable.dot_normal_shap);
}
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
Drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>
<solid android:color="@color/colorPrimary"></solid>
<corners android:radius="10dp"></corners>
</shape>
中间的难点就是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:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/iv_content"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ImageView>
<Button
android:id="@+id/bt_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textSize="22sp"
android:text="进入体验"
android:layout_marginBottom="10dp"
/>
在其中我遇到了fragment加载不出来图片加载出来全是白色
原因是ViewPagerAdapter写错了:
正确的:
public class ViewPagerAdapter extends FragmentPagerAdapter{
private List<Fragment> mlist;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public ViewPagerAdapter(FragmentManager fm,List<Fragment> 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