无线轮播+webView

废话不多说直接上图



小圆点

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorDark"></solid>
    <corners android:radius="5dp"></corners>
    <size
        android:width="10dp"
        android:height="10dp"></size>
</shape>

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorDark"></solid>
    <corners android:radius="5dp"></corners>
    <size
        android:width="10dp"
        android:height="10dp"></size>
</shape>


图片加载


public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        //创建默认的ImageLoader配置参数
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration
                .createDefault(this);
        //Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(configuration);
    }
}


Banner


public class MyBanner extends LinearLayout {

    private ViewPager vp;
    private LinearLayout ll;
    private List<ImageView> imgList = new ArrayList<>();
    private Handler handler = new Handler();
    private int index = 1;
    private int position;
    public MyBanner(Context context) {
        this(context, null);
    }

    public MyBanner(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.banner, this);
        vp = findViewById(R.id.vp);
        ll = findViewById(R.id.ll);

    }

    /**
     * http://img.taopic.com/uploads/allimg/120727/201995-120HG1030762.jpg
     *
     * @param list
     */
    public void setData(List<String> list) {
        if (list == null) {
            throw new RuntimeException("集合不能为空");
        }
        //因为传过来的list里存的是图片下载地址,所以,vp的适配器,不能用
        for (int i = 0; i < list.size(); i++) {
            ImageView imageView = new ImageView(getContext());
            imgList.add(imageView);
            ImageLoader.getInstance().displayImage(list.get(i), imageView);

            imageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getContext(),WebViewActivity.class);
                    intent.putExtra("url","https://www.baidu.com/");
                    getContext().startActivity(intent);
                }
            });

            //创建小圆点
            ImageView ivCircle = new ImageView(getContext());
            LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.leftMargin = 5;
            ivCircle.setLayoutParams(layoutParams);
            ivCircle.setBackgroundResource(R.drawable.circle_nomal);
            ll.addView(ivCircle);
        }
        ImageView iv = (ImageView) ll.getChildAt(0);
        iv.setBackgroundResource(R.drawable.circle_select);
        MyAdapter adapter = new MyAdapter();
        vp.setAdapter(adapter);
        vp.setOnPageChangeListener(new MyPageChangeListener());

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        vp.setCurrentItem((++index) % imgList.size());
                    }
                });
            }
        }, 1000, 3000);

    }


    class MyPageChangeListener implements ViewPager.OnPageChangeListener {



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

        }

        @Override
        public void onPageSelected(int position) {
            //重置所有的圆点装点
            reset();
            ImageView iv = (ImageView) ll.getChildAt(position);
            iv.setBackgroundResource(R.drawable.circle_select);

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    private void reset() {
        int childCount = ll.getChildCount();
        for (int i = 0; i < childCount; i++) {
            ImageView iv = (ImageView) ll.getChildAt(i);
            iv.setBackgroundResource(R.drawable.circle_nomal);
        }
    }


    class MyAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return imgList.size();
        }

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = imgList.get(position);
            container.addView(imageView);
            return imageView;
        }

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


WebView类

public class WebViewActivity extends AppCompatActivity {
    private WebView mW;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        initView();
        Intent intent = getIntent();
        String url = intent.getStringExtra("url");
        mW.loadUrl(url);
    }

    private void initView() {
        mW = (WebView) findViewById(R.id.w);
        WebSettings settings = mW.getSettings();
        settings.setJavaScriptEnabled(true);
    }
}


main.xml

banner布局加载进去

webView.xml

<WebView
        android:id="@+id/w"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></WebView>

banner

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:orientation="vertical">

    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="200dp" />

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"

        android:orientation="horizontal"></LinearLayout>
</RelativeLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值