package cn.flightfeather.thirdapp.activity; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.content.ContextCompat; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.LinearLayout; import java.util.ArrayList; import java.util.List; import cn.flightfeather.thirdapp.R; import cn.flightfeather.thirdapp.adapter.ContentFragmentAdapter; import cn.flightfeather.thirdapp.CommonApplication; import cn.flightfeather.thirdapp.fragment.CardFragment; import cn.flightfeather.thirdapp.util.updateApp.UpdateAppUtil; import cn.flightfeather.thirdapp.view.OrientedViewPager; import cn.flightfeather.thirdapp.view.VerticalStackTransformer; public class LoginActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener { //region 定义变量及对象 //横向ViewPager private OrientedViewPager movp_main; //线性布局 private LinearLayout mln; //卡片碎片 private CardFragment fragmentCurrent; //内容碎片适配器 private ContentFragmentAdapter adapter; //是否显示 private Boolean show = false; //碎片集合 List fragmentList = new ArrayList<>(); //当前位置 private int positionCurrent = 0; //endregion //region Activity创建 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); // //将此Activity加入集合中 // MyApplication.getInstance().addActivity(this); //初始化控件 initControl(); //初始化碎片集合数据 initFragmentListData(); //创建内容碎片适配器 createContentFragmentAdapter(); //设置横向ViewPager setOrientationViewPager(); //设置监听 setListener(); //初始化半透明状态栏 initTransparentStatsBar(); //更新检查 checkUpdate(); } /** * Dispatch onResume() to fragments. Note that for better inter-operation * with older versions of the platform, at the point of this call the * fragments attached to the activity are not resumed. This means * that in some cases the previous state may still be saved, not allowing * fragment transactions that modify the state. To correctly interact * with fragments in their proper state, you should instead override * {@link #onResumeFragments()}. */ @Override protected void onResume() { super.onResume(); } //endregion //region 设置监听 private void setListener() { //设置ViewPager滑动监听 movp_main.setOnPageChangeListener(this); } //endregion //region 设置横向ViewPager private void setOrientationViewPager() { movp_main.setOrientation(OrientedViewPager.Orientation.VERTICAL); movp_main.setOffscreenPageLimit(2); movp_main.setPageTransformer(true, (ViewPager.PageTransformer) new VerticalStackTransformer(this)); movp_main.setAdapter(adapter); movp_main.setDisableTouch(true); SharedPreferences sharedPre = getSharedPreferences("config", MODE_PRIVATE); positionCurrent = sharedPre.getInt("pos", 0); //设置默认显示碎片 fragmentCurrent = (CardFragment) adapter.instantiateItem(movp_main, positionCurrent); } //endregion //region 创建内容碎片适配器 private void createContentFragmentAdapter() { adapter = new ContentFragmentAdapter(getSupportFragmentManager(), fragmentList); } //endregion //region 初始化Fragment集合数据 private void initFragmentListData() { fragmentList.add(new CardFragment()); fragmentList.add(new CardFragment()); fragmentList.add(new CardFragment()); } //endregion //region 初始化控件 private void initControl() { movp_main = (OrientedViewPager) findViewById(R.id.ovp_main); mln = (LinearLayout) findViewById(R.id.mln); } //endregion //region 初始化半透明状态栏 public void initTransparentStatsBar() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); this.getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.transStatusBar)); mln.setPadding(0, getStatusBarHeight(), 0, 0); } } //endregion //region 获得状态栏高度 public int getStatusBarHeight() { int result = 0; int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = getResources().getDimensionPixelSize(resourceId); } return result; } //endregion //region 显示登录按钮 public void showButton() { // fragmentCurrent = adapter.getCardFragment(); fragmentCurrent.showLoginButton(); } //endregion //region 隐藏登录按钮 public void hideButton() { fragmentCurrent.hideLoginButtion(); } //endregion //region ViewPager滑动 @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if (position == 0 && positionOffsetPixels == 0) { show = true; } else { show = false; } } //endregion //region ViewPager选中 @Override public void onPageSelected(int position) { fragmentCurrent = (CardFragment) adapter.instantiateItem(movp_main, position); showButton(); int a = position % 3; if (a == 0) { mln.setBackgroundColor(getResources().getColor(R.color.lightBlue)); fragmentCurrent.changeLoginColor("#03A9F4"); } else if (a == 1) { mln.setBackgroundColor(getResources().getColor(R.color.lightGreen)); fragmentCurrent.changeLoginColor("#8BC34A"); } else if (a == 2) { mln.setBackgroundColor(getResources().getColor(R.color.orange)); fragmentCurrent.changeLoginColor("#f26d44"); } } //endregion //region ViewPager滑动状态改变 @Override public void onPageScrollStateChanged(int state) { if (state == 1) { hideButton(); } else if (state == 2) { showButton(); } else if (state == 0 && show) { showButton(); } } //endregion public void setCurFragment(int pos) { movp_main.setCurrentItem(pos); } // private void checkUpdate() { UpdateAppUtil updateAppUtil = new UpdateAppUtil((CommonApplication) getApplication(), this); updateAppUtil.checkUpdateApp(false); } // }