轮播图的点击跳转
//主要逻辑处理的布局(mybanner_layout
<?xml version="1.0" encoding="utf-8"?> <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:layout_width="match_parent" android:layout_height="200dp" android:id="@+id/viewpager"></android.support.v4.view.ViewPager> <LinearLayout android:orientation="horizontal" android:gravity="center" android:id="@+id/ll_points" android:layout_width="match_parent" android:layout_height="30dp" android:layout_alignParentBottom="true"></LinearLayout> </RelativeLayout>
//主要的逻辑处理
package com.example.zhoukao_moni; 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.util.Log; 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 java.util.ArrayList; import java.util.List; /** * Created by lenovo on 2018/4/13. */ public class MyBanner extends RelativeLayout{ private LinearLayout linearLayout; private ViewPager viewPager; private MyHandler myHandler=new MyHandler(); private List<ImageView> points; private List<Bean.DataBean> lists; private String url; private static final String TAG = "MyBanner"; public MyBanner(Context context) { this(context,null); } public MyBanner(Context context, AttributeSet attrs) { this(context, attrs,0); } public MyBanner(final Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); View view = View.inflate(context, R.layout.mybanner_layout, this); //找空间 viewPager = (ViewPager)view.findViewById(R.id.viewpager); linearLayout = (LinearLayout) view.findViewById(R.id.ll_points); //设置滑动事件 viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { viewPager.setOnTouchListener(new View.OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event) { Intent in=new Intent(context,DetailActivity.class); //String url = lists.get(position).getUrl(); in.putExtra("url",url); //Log.i("点击了",url); Log.d(TAG, "onTouch:=--------------------- "+url); // if(ss==1) { // in.putExtra("url",url); context.startActivity(in); // }else if(ss==0){ // // } return false; } }); position = position% points.size(); for (int i = 0;i<points.size();i++){ if(i==position){ points.get(i).setSelected(true); }else{ points.get(i).setSelected(false); } } } @Override public void onPageScrollStateChanged(int state) { } }); } //设置适配器 public void setMyAdapter(Context context, List<ImageView> list, String url) { this.url = url; Log.d(TAG, "setMyAdapter: =============="+url); MyAdapter myAdapter = new MyAdapter(context, list); viewPager.setAdapter(myAdapter); //设置小圆点 points = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { ImageView point = new ImageView(context); point.setImageResource(R.drawable.circle_selector); linearLayout.addView(point); points.add(point); } //默认第一个选中 points.get(0).setSelected(true); } //开启自动轮播 public void startPlay() { myHandler.sendEmptyMessageDelayed(0, 1000); } //关闭自动轮播 public void stopPlay() { myHandler.removeCallbacksAndMessages(null); } class MyAdapter extends PagerAdapter{ private Context context; private List<ImageView> list; public MyAdapter(Context context, List<ImageView> 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 = list.get(position); container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((ImageView) object); } } class MyHandler extends Handler{ @Override public void handleMessage(Message msg) { int currentItem = viewPager.getCurrentItem(); currentItem++; viewPager.setCurrentItem(currentItem); myHandler.sendEmptyMessageDelayed(0,1000); } } }
//外部取地址的方法


package com.example.zhoukao_moni; /** * Created by lenovo on 2018/4/13. */ public class HttpConfig { public static String url = "http://120.27.23.105/ad/getAd"; }

//主方法的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    android:layout_height="match_parent" tools:context="com.example.zhoukao_moni.MainActivity">

    <com.example.zhoukao_moni.MyBanner
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/banner"></com.example.zhoukao_moni.MyBanner>

</LinearLayout>

//主方法
package com.example.zhoukao_moni; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.ImageView; import com.bumptech.glide.Glide; import com.google.gson.Gson; import java.io.IOException; import java.util.ArrayList; import java.util.List; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private MyBanner myBanner; private MyHandler myHandler=new MyHandler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); initDatas(); } private void initDatas() { OkHttpClient okHttpClient = new OkHttpClient(); Request request=new Request.Builder().url(HttpConfig.url).build(); Call call = okHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String json = response.body().string(); Message message = myHandler.obtainMessage(); message.obj = json; myHandler.sendMessage(message); } }); } private void initViews() { myBanner = (MyBanner)findViewById(R.id.banner); } class MyHandler extends Handler{ private String url; @Override public void handleMessage(Message msg) { String json = (String) msg.obj; Gson gson =new Gson(); Bean bean = gson.fromJson(json, Bean.class); List<Bean.DataBean> list= bean.getData(); List<ImageView> images = new ArrayList<>(); for(int i = 0;i <list.size();i++){ int type = list.get(i).getType(); if(type==0){ url = list.get(i).getUrl(); } Log.d(TAG, "handleMessage:-------------------******** "+ url); String icon = list.get(i).getIcon(); ImageView imageView=new ImageView(MainActivity.this); Glide.with(MainActivity.this).load(icon).into(imageView); images.add(imageView); } //Log.d(TAG, "handleMessage:------******-------------******** "+urlStr); myBanner.setMyAdapter(MainActivity.this,images,url); myBanner.startPlay(); } } }
//跳转的页面布局(activity_detail

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <WebView android:id="@+id/wv" android:layout_width="match_parent" android:layout_height="match_parent"></WebView> </LinearLayout>
//跳转的页面

package com.example.zhoukao_moni; import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.webkit.WebView; /** * Created by lenovo on 2018/4/13. */ public class DetailActivity extends AppCompatActivity{ private static final String TAG = "DetailActivity"; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); Intent intent = getIntent(); String url = intent.getStringExtra("url"); Log.d(TAG, "onCreate:========= "+url); /*List<Bean.DataBean> list=new ArrayList<>(); for (int i = 0 ; i<list.size();i++) { String url1 = list.get(i).getUrl(); //Toast.makeText(DetailActivity.this,url1, Toast.LENGTH_LONG).show(); // Log.d() }*/ WebView wv = (WebView) findViewById(R.id.wv); wv.getSettings().setJavaScriptEnabled(true); wv.loadUrl(url); } }
//圆点等的布局
//circle_selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/circle_press" android:state_selected="true" />
    <item android:drawable="@drawable/circle_normal" android:state_selected="false" />

</selector>

//circle_press

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <size
        android:width="20dp"
        android:height="20dp" />
    <solid android:color="#00ff00" />

</shape>

//circle_normal
<?xml version="1.0" encoding="utf-8"?>
<shape
    android:shape="oval"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <size android:width="20dp" android:height="20dp"/>
    <solid android:color="#55999999"/>

</shape>

//权限配置

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.zhoukao_moni"> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".DetailActivity"></activity> </application> </manifest>
//需要添加的依赖

compile 'com.google.code.gson:gson:2.6.2'

compile 'com.squareup.okhttp3:okhttp:3.3.0'

compile 'com.github.bumptech.glide:glide:3.7.0'


阅读更多
上一篇二级联动
下一篇搜索框
想对作者说点什么? 我来说一句

程序员职场规划之转型

-

学院

1970年01月01日 08:00

没有更多推荐了,返回首页

不良信息举报

轮播图的点击跳转

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭