- packagecom.main.mouthtest;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.support.v4.view.PagerAdapter;
- import android.support.v4.view.ViewPager;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.Toast;
- import com.google.gson.Gson;
- import com.lidroid.xutils.HttpUtils;
- import com.lidroid.xutils.exception.HttpException;
- import com.lidroid.xutils.http.ResponseInfo;
- import com.lidroid.xutils.http.callback.RequestCallBack;
- import com.lidroid.xutils.http.client.HttpRequest;
- import com.main.bean.Weather_bean;
- import com.nostra13.universalimageloader.core.DisplayImageOptions;
- import com.nostra13.universalimageloader.core.ImageLoader;
- import java.lang.ref.WeakReference;
- import java.util.ArrayList;
- import java.util.List;
- /**
- *时间:2017/4/28
- * 类用途:Fragment1的类查询天气
- */
- public class Fragment1extends Fragment {
- private staticViewPager view_pager;
- private static List<View>dots;
- private static int lodposition= 0;
- private static int index= 0;
- private ArrayList<String>arr;
- private LinearLayoutlin_dot;
- private boolean istrue= true;
- private MyHandlermyHandler;
- protected voidonCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.f1);
- istrue =true;
- initview();
- pricture();
- //实例化
- MyViewPagerAdapter adapter =new MyViewPagerAdapter();
- view_pager.setAdapter(adapter);
- setdot();
- myHandler =new MyHandler((MainActivity) getActivity());
- new Thread(){
- @Override
- public voidrun() {
- while(istrue){
- try{
- sleep(1000);
- myHandler.sendEmptyMessage(0);
- }catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }.start();
- }
- //view_pager和设置小圆点的地方
- public voidsetdot() {
- //监听
- view_pager.addOnPageChangeListener(newViewPager.OnPageChangeListener() {
- @Override
- public voidonPageScrolled(intposition, floatpositionOffset, intpositionOffsetPixels) {
- }
- @Override
- public voidonPageSelected(intposition) {
- dots.get(position %dots.size()).setBackgroundResource(R.mipmap.dot_true);
- dots.get(lodposition).setBackgroundResource(R.mipmap.doy_false);
- lodposition= (position % dots.size());
- index= position;
- }
- @Override
- public voidonPageScrollStateChanged(intstate) {
- }
- });
- }
- //加载图片
- private voidpricture() {
- arr= newArrayList<>();
- arr.add("https://timgsa.baidu.com/timg?"+
- "image&quality=80&size=b9999_10000&sec=1493378180369&di=ea0f7952dd3fc7e202b26e7d70e3cbd9&"+
- "imgtype=0&src=http%3A%2F%2Fimg15.3lian.com%2F2015%2Fh1%2F35%2Fd%2F192.jpg");
- arr.add("https://timgsa.baidu.com/timg?"+
- "image&quality=80&size=b9999_10000&sec=1493378222373&di=acc62bc138e69acacbf6c8a9a2439f3d&"+
- "imgtype=0&src=http%3A%2F%2Fimg3.duitang.com%2Fuploads%2Fitem%2F201608%2F08%2F20160808124358_sH4Ea.thumb.700_0.jpeg");
- arr.add("https://timgsa.baidu.com/timg?"+
- "image&quality=80&size=b9999_10000&sec=1493378274676&di=f3349d7217e0f3b7269f16abc0d35b7e&"+
- "imgtype=0&src=http%3A%2F%2Fimg3.duitang.com%2Fuploads%2Fitem%2F201608%2F03%2F20160803105655_QMznk.thumb.700_0.jpeg");
- /**
- * for循环 小圆点的
- */
- dots= newArrayList<View>();
- for (inti = 0;i < arr.size();i++) {
- View view = newView(getActivity());
- LinearLayout.LayoutParams layoutParams =new LinearLayout.LayoutParams(30,30);
- view.setLayoutParams(layoutParams);
- dots.add(view);
- if (i ==0) {
- dots.get(i).setBackgroundResource(R.mipmap.dot_true);
- }else {
- dots.get(i).setBackgroundResource(R.mipmap.doy_false);
- }
- lin_dot.addView(dots.get(i));
- }
- }
- //各个控件的id和button_act的监听
- private voidinitview() {
- lin_dot= (LinearLayout) getActivity().findViewById(R.id.lin_dot);
- //找到view_pager的控件
- view_pager= (ViewPager) getActivity().findViewById(R.id.view_pager);
- button_act.setOnClickListener(newView.OnClickListener() {
- @Override
- public voidonClick(View v) {
- getweather(edit_act.getText().toString());
- }
- });
- }
- /**
- * viewpager的适配器
- */
- private classMyViewPagerAdapter extendsPagerAdapter {
- @Override
- public intgetCount() {
- returnInteger.MAX_VALUE;
- }
- @Override
- public booleanisViewFromObject(View view,Object object) {
- returnview == object;
- }
- @Override
- public voiddestroyItem(ViewGroup container, intposition,Object object) {
- }
- @Override
- publicObject instantiateItem(ViewGroup container, final intposition) {
- ImageView imageView = new ImageView(getActivity());
- DisplayImageOptions builder =new DisplayImageOptions.Builder()
- .showImageOnLoading(R.mipmap.ic_launcher)
- .showImageOnFail(R.mipmap.ic_launcher)
- .cacheOnDisk(true)
- .cacheInMemory(false)
- .build();
- //youcuo
- ImageLoader.getInstance().displayImage(arr.get(position % arr.size()),imageView,builder);
- imageView.setOnClickListener(newView.OnClickListener() {
- @Override
- public voidonClick(View v) {
- Toast.makeText(getActivity(),"点击了"+ position,Toast.LENGTH_SHORT).show();
- }
- });
- container.addView(imageView);
- return imageView;
- }
- }
- //失去焦点
- @Override
- public voidonPause() {
- super.onPause();
- istrue= false;
- }
- private static classMyHandler extendsHandler{
- private finalWeakReference<MainActivity> reference;
- public MyHandler(MainActivity mainActivity) {
- reference= newWeakReference<>(mainActivity);
- }
- @Override
- public voidhandleMessage(Message msg) {
- super.handleMessage(msg);
- if (reference.get() !=null) {
- switch(msg.what){
- case0:
- {
- view_pager.setCurrentItem(index,false);
- index++;
- }
- break;
- }
- }
- }
- }
- }
viewpager轮播图--狸菇凉_
最新推荐文章于 2019-07-22 15:33:48 发布