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的加載:
首先我們要新建一個(gè)WelcomeActivity的頁(yè)面來(lái)搭載viewpager 以及原點(diǎn)指示器:
布局選擇當(dāng)然是幀布局了
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(); //必須調(diào)用該方法民珍,防止內(nèi)存泄漏低零,不調(diào)用該方法悄但,如果界面bar發(fā)生改變傅寡,在不關(guān)閉app的情況下,退出此界面再進(jìn)入將記憶最后一次bar改變的狀態(tài)
}
private void initView() {
vp_main = (ViewPager) findViewById(R.id.vp_main);
ll_indicator = (LinearLayout) findViewById(R.id.ll_indicator);
//創(chuàng)建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>
中間的難點(diǎn)就是fragment的創(chuàng)建以及怎么能判斷哪個(gè)fragment對(duì)應(yīng)哪一個(gè)陨献。
我們用到了這個(gè)
Bundle bundle = new Bundle();
bundle.putInt("index",i);
fragment.setArguments(bundle);
來(lái)傳遞信息爪膊;
在我們的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="進(jìn)入體驗(yàn)"
android:layout_marginBottom="10dp"
/>
在其中我遇到了fragment加載不出來(lái)圖片加載出來(lái)全是白色
原因是ViewPagerAdapter寫(xiě)錯(cuò)了:
正確的:
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導(dǎo)致多了這個(gè)方法
@Override
public boolean isViewFromObject(View view, Object object) {
return false;
}
只要改為FragmentAdapter并且刪除這個(gè)方法就行
那我的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(); //必須調(diào)用該方法缰揪,防止內(nèi)存泄漏陨享,不調(diào)用該方法,如果界面bar發(fā)生改變钝腺,在不關(guān)閉app的情況下抛姑,退出此界面再進(jìn)入將記憶最后一次bar改變的狀態(tài)
}
}
這是一個(gè)沉浸式框架 ,具體看
gitHub地址:https://github.com/gyf-dev/ImmersionBar