riku
2021-02-25 e102578ebfc95c27aeb13dce13fb82af53a2bead
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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<Fragment> 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 <em>not</em> 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);
    }
 
    //<editor-fold desc="检查更新">
    private void checkUpdate() {
        UpdateAppUtil updateAppUtil = new UpdateAppUtil((CommonApplication) getApplication(), this);
        updateAppUtil.checkUpdateApp(false);
    }
    //</editor-fold>
 
}