1.MainAdapter
//视图调用
package com.bwie.skn.customviewpagerdemo2;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
import com.bwie.skn.customviewpagerdemo2.view.CustomViewPagerClass;
public class MainActivity extends AppCompatActivity {
private CustomViewPagerClass cvp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cvp = (CustomViewPagerClass) findViewById(R.id.cvp);
cvp.setOnClick(new CustomViewPagerClass.onClickListener() {
@Override
public void onGoWebView(View v, String url, String type) {
if(type.trim().equals("0")){
Intent intent = new Intent(MainActivity.this,WebViewActivity.class);
intent.putExtra("url",url);
startActivity(intent);
}else{
Toast.makeText(MainActivity.this,"我要跳转页面",Toast.LENGTH_SHORT).show();
}
}
});
}
}
2.自定义ViewPager
--------视图
--------view类 【自定义ViewPager+WebView跳转】
package com.bwie.skn.customviewpagerdemo2.view;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.bwie.skn.customviewpagerdemo2.R;
import com.bwie.skn.customviewpagerdemo2.bean.ImageBean;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
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.util.ArrayList;
import java.util.List;
/**
* author:Created by WangZhiQiang on 2017/12/3.
*/public class CustomViewPagerClass extends LinearLayout {
private ViewPager vp;
private LinearLayout ll;
private List list;
private List ivList = new ArrayList<>();
private onClickListener listener;
public interface onClickListener{
void onGoWebView(View v,String url,String type);
}public void setOnClick(onClickListener listener){
this.listener = listener;
}
//Handler实现自动轮播
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int i = vp.getCurrentItem() + 1;
vp.setCurrentItem(i);
handler.sendEmptyMessageDelayed(0, 2000);
}
};
public CustomViewPagerClass(Context context) {
this(context,null);
}public CustomViewPagerClass(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}public CustomViewPagerClass(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = View.inflate(context, R.layout.viewpager_layout,this);
vp = view.findViewById(R.id.vp);
ll = view.findViewById(R.id.ll);
new MAsyncTask().execute("http://120.27.23.105/ad/getAd");
}
//网络解析数据
class MAsyncTask extends AsyncTask{@Override
protected String doInBackground(String... strings) {
try {URL url = new URL(strings[0]);
Log.e("skn666","url+++"+url);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
int responseCode = urlConnection.getResponseCode();
if (responseCode==200){
InputStream inputStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer buffer = new StringBuffer();
String string = "";
while((string=br.readLine())!=null){
buffer.append(string);
}
Log.e("skn666","bbb+++"+buffer.toString());
return buffer.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.e("skn666","sss++++"+s);
Gson gson = new Gson();
ImageBean bean = gson.fromJson(s, ImageBean.class);
list = bean.getData();
Log.e("skn666","lll+++"+list.get(0).getIcon());
initDot();
vp.setAdapter(new MyAdapter());
vp.setCurrentItem(10000*list.size());
//无线轮播加这行代码
handler.sendEmptyMessageDelayed(0,2000);
//handler实现自动轮播,需要这行代码开启
ViewPagerListener();
}
}
//点击事件
private void ViewPagerListener() {
vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}@Override
public void onPageSelected(int position) {
for(int i =0;
i
//3.Web
--------------------------------
package com.bwie.skn.customviewpagerdemo2;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class WebViewActivity extends AppCompatActivity {private WebView webview;
private ProgressBar progressBar2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
Intent intent = getIntent();
String url = intent.getStringExtra("url");
webview = (WebView)findViewById(R.id.webview);
progressBar2 = (ProgressBar)findViewById(R.id.progressbar);
WebSettings settings = webview.getSettings();
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setJavaScriptEnabled(true);
//加载
webview.loadUrl(url);
//WebViewClient主要帮助WebView处理各种通知、请求事件的
webview.setWebViewClient(new WebViewClient(){});
//WebChromeClient主要辅助WebView处理JavaScript的对话框、网站图标、网站title、加载进度等比如
webview.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
// TODO Auto-generated method stub
super.onProgressChanged(view, newProgress);
//显示
progressBar2.setVisibility(view.VISIBLE);
progressBar2.setProgress(newProgress);
if(newProgress==100){
//隐藏
progressBar2.setVisibility(View.INVISIBLE);
}
}
});
}
}
视图
webAdaoter