1.写一个布局,用于自定义组合控件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="200dp">
android.support.v4.view.ViewPager>
<LinearLayout
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:gravity="center"
android:id="@+id/ll_buttom"
android:background="#999999"
android:layout_width="match_parent"
android:layout_height="40dp">
LinearLayout>
RelativeLayout>
2.写一个类继承RelativeLayout,也就是自定义组合控件的类
package com.bawei.com.zhanglu201801021511j.Views;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.bawei.com.zhanglu201801021511j.R;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
/**
* Created by lenovo on 2018/1/2.
*/
public class MyViewGrop extends RelativeLayout {
private List list;
private MyHandler2 myHandler2;
private ViewPager viewById;
private LinearLayout ll_buttom;
private Context Mcontext;
private Setonhuizhi setonhuizhi;
public MyViewGrop(Context context) {
this(context, null);
}
public MyViewGrop(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyViewGrop(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
LayoutInflater from = LayoutInflater.from(context);
View view = from.inflate(R.layout.myviewgrop, this, true);
viewById = view.findViewById(R.id.viewpager);
ll_buttom = view.findViewById(R.id.ll_buttom);
Mcontext = context;
}
public void setMyAdapter(Context context, List list) {
MyAdapter myAdapter = new MyAdapter(context, list);
viewById.setAdapter(myAdapter);
final ArrayList points = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
ImageView point = new ImageView(context);
point.setBackgroundResource(R.drawable.selected);
points.add(point);
ll_buttom.addView(point);
}
points.get(0).setSelected(true);
viewById.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
position = position % points.size();
for (int i = 0; i < points.size(); i++) {
if (i == position) {
points.get(position).setSelected(true);
} else {
points.get(i).setSelected(false);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
viewById.setCurrentItem(points.size()*100000);
myHandler2 = new MyHandler2();
autoPlay();
}
private void autoPlay() {
myHandler2.sendEmptyMessageDelayed(0, 1000);
}
class MyAdapter extends PagerAdapter {
private Context context;
private List list;
public MyAdapter(Context context, List 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, int position) {
position = position % list.size();
ImageView imageView = new ImageView(context);
Glide.with(context).load(list.get(position).getIcon()).into(imageView);
container.addView(imageView);
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
myHandler2.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_MOVE:
myHandler2.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_CANCEL:
myHandler2.sendEmptyMessageDelayed(0,2000);
break;
case MotionEvent.ACTION_UP:
myHandler2.sendEmptyMessageDelayed(0,2000);
break;
}
return false;
}
});
final int finalPosition = position;
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int type = list.get(finalPosition).getType();
String icon = list.get(finalPosition).getUrl();
setonhuizhi.onsethuizhi(type,icon);
}
});
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
class MyHandler2 extends Handler {
@Override
public void handleMessage(Message msg) {
viewById.setCurrentItem(viewById.getCurrentItem() + 1);
myHandler2.sendEmptyMessageDelayed(0, 1000);
}
}
public interface Setonhuizhi {
void onsethuizhi(int type,String Url);
}
public void setonda(Setonhuizhi setonhuizhi) {
this.setonhuizhi = setonhuizhi;
}
}
3.小圆点的xml布局背景 建立2个shape.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/shape1">item>
<item android:state_selected="false" android:drawable="@drawable/shape2">item>
selector>
//默认
<shape
android:shape="oval"
xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="10dp" android:height="10dp">size>
<solid android:color="@android:color/holo_red_dark">solid>
shape>
//为true是默认
<shape
android:shape="oval"
xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="10dp" android:height="10dp">size>
<solid android:color="@android:color/holo_green_dark">solid>
shape>
4.mainACtivity的布局和应用
//布局
"1.0" encoding="utf-8"?>
.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bawei.com.zhanglu201801021511j.MainActivity">
<com.bawei.com.zhanglu201801021511j.Views.MyViewGrop
android:id="@+id/banner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
.support.constraint.ConstraintLayout>
//应用
package com.bawei.com.zhanglu201801021511j
import android.content.Intent
import android.os.Handler
import android.os.Message
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import com.bawei.com.zhanglu201801021511j.Views.MyViewGrop
import com.bawei.com.zhanglu201801021511j.Views.bean
import com.google.gson.Gson
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.MalformedURLException
import java.net.URL
import java.net.URLConnection
import java.util.List
public class MainActivity extends AppCompatActivity {
private MyViewGrop banner
String url = "https://www.zhaoapi.cn/ad/getAd"
private MyHandler myHandler
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//获取自定义的控件id
banner = (MyViewGrop) findViewById(R.id.banner)
//请求网络数据
jiexi(url)
//handler机制
myHandler = new MyHandler()
//调用接口传值的方法
banner.setonda(new MyViewGrop.Setonhuizhi() {
@Override
public void onsethuizhi(int type, String Url) {
//type==1
if (type == 1) {
//意图
Intent intent = new Intent(MainActivity.this, Main2Activity.class)
//跳转传值
intent.putExtra("url", Url)
//跳转
startActivity(intent)
} else {
//不为1吐司
Toast.makeText(MainActivity.this, type + "", Toast.LENGTH_SHORT).show()
}
}
})
}
//数据解析
public void jiexi(final String url) {
new Thread() {
@Override
public void run() {
super.run()
try {
URL u = new URL(url)
HttpURLConnection urlConnection = (HttpURLConnection) u.openConnection()
urlConnection.setConnectTimeout(8000)
if (urlConnection.getResponseCode() == 200) {
InputStream inputStream = urlConnection.getInputStream()
String s = in2String(inputStream)
Gson g = new Gson()
bean bean = g.fromJson(s, bean.class)
List<com.bawei.com.zhanglu201801021511j.Views.bean.DataBean> data = bean.getData()
Log.d("TAG", "gson----" + bean)
Message message = new Message()
message.obj = data
myHandler.sendMessage(message)
}
} catch (MalformedURLException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
}
}
}.start()
}
//将输入流转成字符串
public String in2String(InputStream is) throws IOException {
StringBuffer buffer = new StringBuffer()
BufferedReader br = new BufferedReader(new InputStreamReader(is))
String len = ""
while ((len = br.readLine()) != null) {
buffer.append(len)
}
return buffer.toString()
}
//创建Handler对象
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
List.DataBean> data1 = (List.DataBean>) msg.obj
//在这里设置适配器
banner.setMyAdapter(MainActivity.this, data1)
}
}
}
"http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bwie.zengxianglin1511j20180102.Main2Activity"
>
"@+id/mWebView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
public class Main2Activity extends AppCompatActivity {
WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent intent = getIntent();
String url = intent.getStringExtra("url");
Toast.makeText(Main2Activity.this,url+"aaaa",Toast.LENGTH_LONG).show();
mWebView = (WebView) findViewById(R.id.mWebView);
mWebView.loadUrl(url);
mWebView.requestFocusFromTouch();
mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
/**覆盖调用系统或自带浏览器行为打开网页*/
mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
/**判断加载过程*/
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
} else {
}
}
});
initListener();
}
private void initListener() {
/**打开页面时, 自适应屏幕*/
WebSettings webSettings = mWebView .getSettings();
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
/**便页面支持缩放*/
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setSupportZoom(true);
}
}