一 .引导页 倒计时跳转 第二次进入直接进入内容也
1.布局页面
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".ui.activity.MainActivity">
<ImageView
android:id="@+id/image"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@mipmap/ic_launcher"
/>
<!-- 自动计算 不是正方形 -->
<TextView
android:id="@+id/time"
android:layout_width="60dp"
android:layout_height="60dp"
app:layout_constraintTop_toTopOf="@id/image"
app:layout_constraintRight_toRightOf="@id/image"
android:text="3s"
android:gravity="center"
android:textSize="30sp"
android:padding="10dp"
android:background="@drawable/shape_time"
/>
</android.support.constraint.ConstraintLayout>
2.MainActivity代码页
package com.example.mouth_lianxi01.ui.activity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.mouth_lianxi01.R;
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private TextView textView;
private int time = 3;
@SuppressLint("HandlerLeak")
private Handler handler = new Handler(){
@SuppressLint("SetTextI18n")
@Override
public void handleMessage(Message msg) {
if(time>0){
time--;
textView.setText(time+"s");
handler.sendEmptyMessageDelayed(0,1000);
}else{
Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
//存取一个状态值
edit.putBoolean("isChecked",true);
//提交
edit.commit();
finish();
}
}
};
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取资源id
imageView = findViewById(R.id.image);
textView = findViewById(R.id.time);
handler.sendEmptyMessageDelayed(0,1000);
//创建SharedPreferences
sharedPreferences = getSharedPreferences("User",MODE_PRIVATE);
//创建Editor
edit = sharedPreferences.edit();
//将状态值取出
boolean isChecked = sharedPreferences.getBoolean("isChecked", false);
if(isChecked){
handler.removeCallbacksAndMessages(null);
Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
finish();
}
}
}
二.侧拉页面及代码(包括点击侧拉条目跳转和viewpage+tablayout关联和二次采样)
效果图
1.activity_login.xml布局页面(包括tablayout+viewpagerliandong+侧拉占位布局)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="@+id/t_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:tabTextColor="@color/colorPrimary"
app:tabSelectedTextColor="@color/colorAccent"
app:tabIndicatorHeight="1dp"
app:tabIndicatorColor="@android:color/transparent"
app:tabBackground="@color/colorPrimaryDark"
/>
<android.support.v4.view.ViewPager
android:id="@+id/v_pager"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/t_layout"
/>
</android.support.constraint.ConstraintLayout>
<FrameLayout
android:id="@+id/f_layout"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
/>
</android.support.v4.widget.DrawerLayout>
2.LoginActivity页面(包括底部tablayout+vierpager联动+点击侧拉条目切换对应的viewoager)
package com.example.mouth_lianxi01.ui.activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.MenuItem;
import com.example.mouth_lianxi01.R;
import com.example.mouth_lianxi01.ui.adaper.LoginAdaper;
import com.example.mouth_lianxi01.ui.fragment.LeftFragment;
public class LoginActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
private DrawerLayout layout;
private LoginAdaper adaper;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle toggle;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//获取资源id
tabLayout = findViewById(R.id.t_layout);
viewPager = findViewById(R.id.v_pager);
layout = findViewById(R.id.drawer);
//创建适配器
adaper = new LoginAdaper(getSupportFragmentManager());
viewPager.setAdapter(adaper);
tabLayout.setupWithViewPager(viewPager);
//当savedInstanceState为空时加载fragment
if(savedInstanceState == null){
getSupportFragmentManager().beginTransaction()
.add(R.id.f_layout,new LeftFragment())
.commit();
}
//显示侧边栏
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//获取资源id
drawerLayout = findViewById(R.id.drawer);
toggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.open,R.string.close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
}
//侧拉右上角可点击
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(toggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
//点击
public void showPage(int i) {
viewPager.setCurrentItem(i);
drawerLayout.closeDrawer(Gravity.START);
}
}
3.LoginAdaper适配器页面(主要是完成tablayout和viewpager联动)
package com.example.mouth_lianxi01.ui.adaper;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.example.mouth_lianxi01.ui.fragment.CinemaFragment;
import com.example.mouth_lianxi01.ui.fragment.FilmFragment;
import com.example.mouth_lianxi01.ui.fragment.MemberFragment;
import com.example.mouth_lianxi01.ui.fragment.SetFragment;
public class LoginAdaper extends FragmentPagerAdapter {
private String[] page = new String[]{
"影片","影院","会员","设置"
};
public LoginAdaper(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i){
case 0:
return new FilmFragment();
case 1:
return new CinemaFragment();
case 2:
return new MemberFragment();
case 3:
return new SetFragment();
default:
return new Fragment();
}
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return page[position];
}
@Override
public int getCount() {
return page.length;
}
}
4.侧拉页面需要的fragment布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ListView
android:id="@+id/left_list"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="@android:color/white"
/>
</android.support.constraint.ConstraintLayout>
5.侧拉页面需要fragment页面的代码
package com.example.mouth_lianxi01.ui.fragment;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.CursorLoader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.example.mouth_lianxi01.R;
import com.example.mouth_lianxi01.Util.ImageUtil;
import com.example.mouth_lianxi01.ui.activity.LoginActivity;
import com.example.mouth_lianxi01.ui.adaper.LeftAdaper;
import com.example.mouth_lianxi01.ui.adaper.LoginAdaper;
import java.util.ArrayList;
import java.util.List;
import static android.app.Activity.RESULT_OK;
public class LeftFragment extends Fragment {
private ListView listView;
private LeftAdaper adaper;
private Uri uri;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.left_fragment,container,false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreate