代码
MainActivity
package com.example.day1011_3;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.day1011_3.Bean.JavaBean;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private ViewPager viewPagerId;
private Button mainButton;
private TextView mainTextview;
private List<Fragment> lists=new ArrayList<>();
private Timer timer;
private Timer timer1;
private int index=0;
private int second=5;
private Handler handler=new Handler();
private List<JavaBean.DataBean> javalist=new ArrayList<>();
private String path="http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1";
private FragmentManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
viewPagerId = (ViewPager) findViewById(R.id.viewPagerId);
mainButton = (Button) findViewById(R.id.main_button);
mainTextview = (TextView) findViewById(R.id.main_textview);
manager=getSupportFragmentManager();
mainButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
timer1.cancel();
finish();
}
});
viewPagerId.setAdapter(new FragmentPagerAdapter(manager) {
@Override
public Fragment getItem(int i) {
return lists.get(i);
}
@Override
public int getCount() {
return lists.size();
}
});
timer=new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
viewPagerId.setCurrentItem(index);
index++;
if (index==lists.size()){
index=0;
}
}
});
}
},0,2000);
viewPagerId.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
if (index==lists.size()-1){
timer.cancel();
mainButton.setVisibility(View.VISIBLE);
mainTextview.setVisibility(View.VISIBLE);
timer1=new Timer();
timer1.schedule(new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
mainTextview.setText("倒计时:"+second);
second--;
if (second==0){
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
finish();
timer1.cancel();
}
}
});
}
},0,2000);
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
new MyAsynckTask(lists,javalist,manager,viewPagerId).execute(path);
}
}
MyAsynckTask
package com.example.day1011_3;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import com.alibaba.fastjson.JSON;
import com.example.day1011_3.Bean.JavaBean;
import java.util.ArrayList;
import java.util.List;
public class MyAsynckTask extends AsyncTask<String,Void, List<JavaBean.DataBean>> {
private List<Fragment> lists;
private List<JavaBean.DataBean> javalists;
private FragmentManager manager;
private ViewPager viewPager;
public MyAsynckTask(List<Fragment> lists, List<JavaBean.DataBean> javalists, FragmentManager manager, ViewPager viewPager) {
this.lists = lists;
this.javalists = javalists;
this.manager = manager;
this.viewPager = viewPager;
}
@Override
protected List<JavaBean.DataBean> doInBackground(String... strings) {
String s = HttpUtils.loadUrl(strings[0]);
if (s!=null){
JavaBean javaBean = JSON.parseObject(s, JavaBean.class);
List<JavaBean.DataBean> data = javaBean.getData();
return data;
}
return null;
}
@Override
protected void onPostExecute(List<JavaBean.DataBean> dataBeans) {
super.onPostExecute(dataBeans);
if (dataBeans!=null){
javalists.addAll(dataBeans);
for (int i = 0; i <javalists.size(); i++) {
BlankFragment blankFragment=new BlankFragment();
Bundle bundle=new Bundle();
bundle.putString("keytext",javalists.get(i).getTitle());
bundle.putString("keyimage",javalists.get(i).getPic());
blankFragment.setArguments(bundle);
lists.add(blankFragment);
}
}
viewPager.setAdapter(new FragmentPagerAdapter(manager) {
@Override
public Fragment getItem(int i) {
return lists.get(i);
}
@Override
public int getCount() {
return lists.size();
}
});
}
}
```