1. 程式人生 > >網路請求資料的viewpager

網路請求資料的viewpager

import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private String picurl="http://172.17.8.100/movieApi/movie/v1/findReleaseMovieList?count=10&page=1";
    private ViewPager view_pager;
    private List<String> list=new ArrayList<>();
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
          if (msg.what == 0){
                int currentItem = view_pager.getCurrentItem();
                currentItem++;
                view_pager.setCurrentItem(currentItem);
                handler.sendEmptyMessageDelayed(0,2000);
           }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        view_pager= findViewById(R.id.view_pager);
        doHttp();

    }

    private void doHttp() {
        new Helder().get(picurl).result(new Helder.Httplister() {
            @Override
            public void success(String data) {
                Gson gson = new Gson();
                Bean bean = gson.fromJson(data, Bean.class);
                for (int i=0;i<bean.getResult().size();i++){
                    String image = bean.getResult().get(i).getImageUrl();
                    list.add(image);
                }
                Adapterr adapterr = new Adapterr(list, MainActivity.this);
                view_pager.setAdapter(adapterr);
                handler.sendEmptyMessageDelayed(0,2000);
            }
        });
    }
}