MainActivity
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private MyBanner mMbanner;
private List<String> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
list.add("http://img.zcool.cn/community/017274582000cea84a0e282b576a32.jpg");
list.add("http://pic4.nipic.com/20091217/3885730_124701000519_2.jpg");
list.add("http://img.zcool.cn/community/017274582000cea84a0e282b576a32.jpg");
list.add("http://img.zcool.cn/community/014a52554064690000005b03d35d4e.jpg@900w_1l_2o_100sh.jpg");
mMbanner.setData(list);
}
private void initView() {
mMbanner = (MyBanner) findViewById(R.id.mbanner);
}
}
MyAbaaner
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by peng on 2017/12/2.
*/
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);
}
}
}
WebViewActivity
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.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);
}
}
cicrle_select
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorGreen"></solid>
<corners android:radius="5dp"></corners>
<size
android:width="10dp"
android:height="10dp"></size>
</shape>
circle_normal
<?xml version="1.0" encoding="utf-8"?>
<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>