无线轮播+webView

来源:互联网 发布:nginx 判断是否有参数 编辑:程序博客网 时间:2024/05/07 10:02

废话不多说直接上图



小圆点

<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>


原创粉丝点击