1. 程式人生 > >輪播圖加登陸,主要是輪播圖

輪播圖加登陸,主要是輪播圖

public class HomeActivity extends AppCompatActivity {
    public static final int FLAG = 123;

    private ViewPager vpBanner;

    private List<String> imgList;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == FLAG) {
                int current = vpBanner.getCurrentItem();
                if (current < imgList.size() - 1) {
                    current++;
                } else {
                    current = 0;
                }
                vpBanner.setCurrentItem(current);
                sendEmptyMessageDelayed(FLAG, 2000);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        vpBanner = findViewById(R.id.vp_banner);

        imgList = new ArrayList<>();
        imgList.add("http://01.imgmini.eastday.com/mobile/20180512/20180512072505_0fe08f494e7c090764244e3581b3e5ca_5_mwpm_03200403.jpg");
        imgList.add("http://01.imgmini.eastday.com/mobile/20180512/20180512072505_0fe08f494e7c090764244e3581b3e5ca_1_mwpm_03200403.jpg");
        imgList.add("http://06.imgmini.eastday.com/mobile/20180512/20180512_38f5183808987be3783b180740d12a2a_cover_mwpm_03200403.jpg");


        vpBanner.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return imgList.size();
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return view == object;
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                ImageView img = new ImageView(HomeActivity.this);
                Glide.with(HomeActivity.this).load(imgList.get(position)).into(img);
                container.addView(img);
                return img;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView((View) object);
            }
        });

        handler.sendEmptyMessageDelayed(FLAG, 2000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }

}



///ppp
public class LoginPresenter {

    private IView iv;
    private LoginModel loginModel;

    public void attach(IView iv) {
        this.iv = iv;
        loginModel = new LoginModel();
    }

    public void detach() {
        if (iv != null) {
            iv = null;
        }
    }
//011111111111111111

    // 檢驗
    public void check() {
        if (TextUtils.isEmpty(iv.getUsername()) || TextUtils.isEmpty(iv.getPassword())) {
            iv.check(false);
        } else {
            iv.check(true);
        }
    }


    public void isFirst() {
        SharedPreferences sp = iv.getContext().getSharedPreferences("config", Context.MODE_PRIVATE);
        String username = sp.getString("username", "");
        String password = sp.getString("password", "");
        // 不是第一次登入
        if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {
//            iv.gotoMain();
            iv.setUsername(username);
            iv.setPassword(password);
        }
    }


    public void login(String url) {
        iv.show();

        final String username = iv.getUsername();
        final String password = iv.getPassword();

        url = url.concat("?username=").concat(username).concat("&password=").concat(password);
        Type type = new TypeToken<LoginBean>() {
        }.getType();
        loginModel.login(url, new ICallBack() {
            @Override
            public void onSuccess(Object obj) {
                iv.dissmiss();
                iv.success(obj);

                SharedPreferences sp = iv.getContext().getSharedPreferences("config", Context.MODE_PRIVATE);
                sp.edit().putString("username", username)
                        .putString("password", password)
                        .commit();

                iv.gotoMain();
            }

            @Override
            public void onFailed(Exception e) {
                iv.dissmiss();
                iv.failed(e);
            }
        }, type);
    }
}




///http
public class HttpUtils {
    private static volatile HttpUtils instance;

    private OkHttpClient client;

    private Handler handler = new Handler();

    private HttpUtils() {
        client = new OkHttpClient();
    }

    public static HttpUtils getInstance() {
        if (instance == null) {
            synchronized (HttpUtils.class) {
                if (null == instance) {
                    instance = new HttpUtils();
                }
            }
        }
        return instance;
    }


    public void get(String url, final ICallBack callBack, final Type type) {
        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.onFailed(e);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Gson gson = new Gson();
                final Object o = gson.fromJson(result, type);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.onSuccess(o);
                    }
                });
            }
        });
    }
}


///homeactivity
public class HomeActivity extends AppCompatActivity {
    public static final int FLAG = 123;

    private ViewPager vpBanner;

    private List<String> imgList;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == FLAG) {
                int current = vpBanner.getCurrentItem();
                if (current < imgList.size() - 1) {
                    current++;
                } else {
                    current = 0;
                }
                vpBanner.setCurrentItem(current);
                sendEmptyMessageDelayed(FLAG, 2000);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        vpBanner = findViewById(R.id.vp_banner);

        imgList = new ArrayList<>();
        imgList.add("http://01.imgmini.eastday.com/mobile/20180512/20180512072505_0fe08f494e7c090764244e3581b3e5ca_5_mwpm_03200403.jpg");
        imgList.add("http://01.imgmini.eastday.com/mobile/20180512/20180512072505_0fe08f494e7c090764244e3581b3e5ca_1_mwpm_03200403.jpg");
        imgList.add("http://06.imgmini.eastday.com/mobile/20180512/20180512_38f5183808987be3783b180740d12a2a_cover_mwpm_03200403.jpg");


        vpBanner.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return imgList.size();
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return view == object;
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                ImageView img = new ImageView(HomeActivity.this);
                Glide.with(HomeActivity.this).load(imgList.get(position)).into(img);
                container.addView(img);
                return img;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView((View) object);
            }
        });

        handler.sendEmptyMessageDelayed(FLAG, 2000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }

}