自定义轮播图+网络请求

1.创建viewpa.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
 <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

</RelativeLayout>

2.创建自定义控件viewpage类

public class viewpager  extends RelativeLayout implements ViewPager.OnPageChangeListener {


    //定义一个定时器
    private Timer timer;
    private  int  currentItemindex;
     private ViewPager viewp;

   private Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
             currentItemindex = viewp.getCurrentItem();
            viewp.setCurrentItem(currentItemindex+1);



        }
    };
    private ArrayList imageViews;
    private ImageView imageView;


    public viewpager(Context context) {
        this(context,null);
    }

    public viewpager(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public viewpager(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //创建自定义组件视图
        View view= View.inflate(context, R.layout.viewpa,this);
        viewp = (ViewPager) view.findViewById(R.id.viewpager);

        viewp.addOnPageChangeListener(this);

        //初始化定时器
        timer = new Timer();
      }
   public void setAdapter(MyPagerAdapter myPagerAdapter){

        viewp.setAdapter(myPagerAdapter);

    }





    //开启轮播
  public void startAuto(){

        //开始定时器
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
             handler.sendEmptyMessage(0);

            }
        },0,3000);

    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {


    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


}

3.创建适配器MyPagerAdapter类

public class MyPagerAdapter extends PagerAdapter {
    private Context context;
   private List<TT>  list;


    public MyPagerAdapter(Context context, List<TT> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        ImageView imageView = new ImageView(context);

        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        //从网络上获取图片 ,需要到入一个Myapp的工具类
        ImageLoader.getInstance().displayImage(list.get(position%list.size()).getIcon(),imageView, MyApp.getDefaultDisplayOption());
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(list.get(position%list.size()).getUrl()==""||list.get(position%list.size()).getUrl()==null)
                {



                }else{

                    Intent intent = new Intent(context,Main2Activity.class);
                    //  ("url",list.get(position).getUrl());
                    Bundle bundle = new Bundle();
                    bundle.putString("url",list.get(position%list.size()).getUrl());
                    intent.putExtras(bundle);
                    context.startActivity(intent);

                }
 }
        });

        container.addView(imageView);

        return imageView;




    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
       // super.destroyItem(container, position, object);

        container.removeView((View) object);

    }
}

4.MainAcitiviy

public class MainActivity extends AppCompatActivity {
    ArrayList<TT> list = new ArrayList<>();
    private viewpager dyvp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dyvp = (viewpager) findViewById(R.id.dyvp);

        qingqiu();

}
    //网络请求
    public  void qingqiu() {
     MyTask myTask = new MyTask(new MyTask.Icallbacks() {
            @Override
            public void updateUiByjson(String jsonstr) {
                Gson gson = new Gson();
                Bean bean = gson.fromJson(jsonstr, Bean.class);
                List<Bean.DataBean> data = bean.getData();
                for (int i = 0; i < data.size(); i++) {
                    String icon = data.get(i).getIcon();
                    String title = data.get(i).getTitle();
                    String url = data.get(i).getUrl();
                    String[] split = title.split("|");

                    list.add(new TT(icon, split[1],url));

                }

                MyPagerAdapter myPagerAdapter = new MyPagerAdapter(MainActivity.this ,list);
                dyvp.setAdapter(myPagerAdapter);
                dyvp.startAuto();

 }
});
        myTask.execute("http://120.27.23.105/ad/getAd");


    }


}

5.在布局中使用自定义控件
导入工具类
Bean 类自己封装
添加网络权限’
TT是bean类自己封装

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值