第八天。。案例
方法:
第一种方法,fragment循环
activity代码
package com.example.myday08lianxi002;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private LinearLayout linearLayout;
private List<Fragment> list=new ArrayList<>();
private List<ImageView> image=new ArrayList<>();
private Handler handler=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager=findViewById(R.id.main_pager);
linearLayout=findViewById(R.id.ll);
for (int i = 0; i < 3 ; i++) {
list.add(new OneFragment());
}
for (int i = 0; i <list.size() ; i++) {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.drawable.is_flase);
linearLayout.addView(imageView);
image.add(imageView);
}
ImageView imageView = image.get(0);
imageView.setImageResource(R.drawable.is_true);
MyAdapter myAdapter = new MyAdapter(getSupportFragmentManager(),list);
viewPager.setAdapter(myAdapter);
show();
time();
}
private void time() {
SharedPreferences aaa = getSharedPreferences("uuu", MODE_PRIVATE);
int iii = aaa.getInt("iii", 0);
if (iii!=0){
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}else{
SharedPreferences.Editor edit = aaa.edit();
edit.putInt("iii",5);
edit.commit();
}
}
private void show() {
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
for (int j = 0; j <image.size() ; j++) {
ImageView imageView = image.get(j);
imageView.setImageResource(R.drawable.is_flase);
}
ImageView imageView = image.get(i);
imageView.setImageResource(R.drawable.is_true);
if (i==list.size()-1){
Button button=findViewById(R.id.one_button);
button.setVisibility(Button.VISIBLE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
});
final TextView textView = findViewById(R.id.one_text);
textView.setVisibility(TextView.VISIBLE);
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int c=5;
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
textView.setText(c+"");
}
});
c--;
if (c==0){
timer.cancel();
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
}
},0,1000);
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int i=0;
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
viewPager.setCurrentItem(i);
}
});
i++;
if (i==list.size()){
timer.cancel();
}
}
},0,1000);
}
}
activity布局代码:
<?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"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:id="@+id/one_text"
android:visibility="gone"
/>
<android.support.v4.view.ViewPager
android:id="@+id/main_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
</android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/ll">
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳过广告"
android:visibility="gone"
android:id="@+id/one_button"
/>
</LinearLayout>
pager适配器代码:
package com.example.myday08lianxi002;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import java.util.List;
public class MyAdapter extends FragmentStatePagerAdapter {
private List<Fragment> list;
public MyAdapter(FragmentManager fm,List<Fragment> list) {
super(fm);
this.list=list;
}
@Override
public Fragment getItem(int i) {
return list.get(i);
}
@Override
public int getCount() {
return list.size();
}
}
fragment 适配器
package com.example.myday08lianxi002;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
/**
* A simple {@link Fragment} subclass.
*/
public class OneFragment extends Fragment {
public OneFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_one, container, false);
ImageView imageView=inflate.findViewById(R.id.one_image);
imageView.setImageResource(R.mipmap.ic_launcher);
return inflate;
}
}
fragment 布局代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".OneFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/one_image"
/>
</LinearLayout>
</FrameLayout>
第二种方法 三个fragment:
activity代码:
package com.example.day08_.fragment;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.example.day008.MainActivity;
import com.example.day008.R;
import java.util.Timer;
import java.util.TimerTask;
/**
* A simple {@link Fragment} subclass.
*/
public class WelcomeFragmentThree extends Fragment {
private TextView time;
private Timer timer;
private int count =5;
private static final String TAG = "WelcomeFragmentThree";
@SuppressLint("HandlerLeak")
public Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what == 110){
Log.i(TAG, "handleMessage: "+msg.what);
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(111);
}
},0,1000);
}else if(msg.what == 111){
count--;
Log.i(TAG, "handleMessage: 1111"+msg.what);
time.setText("倒计时"+count+"秒");
if(count == 0){
timer.cancel();
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
getActivity().finish();
}
}
}
};
private Button btn;
public WelcomeFragmentThree() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_welcome_fragment_three, container, false);
time = (TextView) inflate.findViewById(R.id.time);
btn = (Button) inflate.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
timer.cancel();
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
getActivity().finish();
}
});
return inflate;
}
}
fragment布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.WelcomeFragmentThree">
<ImageView
android:id="@+id/three"
android:src="@drawable/reba"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="倒计时"
android:layout_marginRight="30dp"
android:layout_marginTop="30dp"
android:layout_alignParentRight="true"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="立即跳转"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginBottom="50dp"
/>
</RelativeLayout>