Android viewpager自动轮播和小圆点联动效果
程序员文章站
2023-12-17 14:11:40
本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下
首先来看一下我们要做成的而效果:
主页面要显示一个viewpager自动轮播+小圆...
本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下
首先来看一下我们要做成的而效果:
主页面要显示一个viewpager自动轮播+小圆点联动的效果 :
废话不多说,直接上代码:
布局文件:
activity_main.xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <relativelayout android:layout_width="match_parent" android:layout_height="200dp" > <android.support.v4.view.viewpager android:id="@+id/main_vp" android:layout_width="match_parent" android:layout_height="200dp" > </android.support.v4.view.viewpager> <linearlayout android:id="@+id/ll_doc" android:layout_width="match_parent" android:layout_height="40dp" android:layout_alignparentbottom="true" android:gravity="center" android:orientation="horizontal" > </linearlayout> </relativelayout> <linearlayout android:layout_width="match_parent" android:layout_height="100dp" > </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="80dp" > </linearlayout> <gridview android:id="@+id/main_gv" android:layout_width="match_parent" android:layout_height="match_parent" android:numcolumns="2" > </gridview> </linearlayout>
menu下的main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context="com.example.monthextwo.mainactivity" > <item android:id="@+id/action_settings" android:orderincategory="100" android:showasaction="never" android:title="@string/action_settings"/> </menu>
接下来看一下要用到的架包文件:
最后是java文件:
首先是com.example.monthextwo包中的mainactivity.java:
public class mainactivity extends activity { viewpager vp; dataresult data; list<imageview> list; list<imageview> list_doc; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initview(); initdata(); } private void initview() { vp = (viewpager) findviewbyid(r.id.main_vp); } public void initdoc(){ linearlayout ll_dco = (linearlayout) findviewbyid(r.id.ll_doc); list_doc = new arraylist<imageview>(); if(data != null && data.getresult().getadvs().size() > 0){ for (int i = 0; i < data.getresult().getadvs().size(); i++) { imageview iv = new imageview(this); layoutparams params = new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content); params.rightmargin = 10; iv.setlayoutparams(params); if(i == 0){ iv.setbackgroundresource(r.drawable.doc_check); }else{ iv.setbackgroundresource(r.drawable.doc_uncheck); } ll_dco.addview(iv); list_doc.add(iv); } } } private void initdata() { new asynctask<string, string, string>(){ @override protected string doinbackground(string... params) { string json = netutils.getnetdata("http://www.babybuy100.com/api/getshopoverview.ashx"); gson gson = new gson(); data = gson.fromjson(json, dataresult.class); log.i("=================", data.tostring()); return null; } @override protected void onpostexecute(string result) { initvp(); super.onpostexecute(result); } }.execute(); } protected void initvp() { list = new arraylist<imageview>(); if(data != null && data.getresult().getadvs().size() > 0){ for (int i = 0; i < data.getresult().getadvs().size(); i++) { imageview iv = new imageview(this); iv.setscaletype(scaletype.fit_xy); imageloader.getinstance().displayimage(data.getresult().getadvs().get(i).getpic(), iv); list.add(iv); } //创建适配器 mypageradatper mypageradatper = new mypageradatper(list); vp.setadapter(mypageradatper); initdoc(); vp.addonpagechangelistener(new onpagechangelistener() { @override public void onpageselected(int arg0) { int index = arg0 % list_doc.size(); for (int i = 0; i < list_doc.size(); i++) { list_doc.get(i).setbackgroundresource(r.drawable.doc_uncheck); } list_doc.get(index).setbackgroundresource(r.drawable.doc_check); } @override public void onpagescrolled(int arg0, float arg1, int arg2) { // todo auto-generated method stub } @override public void onpagescrollstatechanged(int arg0) { // todo auto-generated method stub } }); } } }
myapp.xml:
public class myapp extends application{ @override public void oncreate() { // todo auto-generated method stub super.oncreate(); imageloaderconfiguration configuration = imageloaderconfiguration.createdefault(this); imageloader.getinstance().init(configuration); } }
com.example.monthextwo.adapter包下的mypageradatper.java:
public class mypageradatper extends pageradapter{ list<imageview> list; public mypageradatper(list<imageview> list) { super(); this.list = list; } @override public int getcount() { if(list != null){ return integer.max_value; } return 0; } @override public boolean isviewfromobject(view arg0, object arg1) { // todo auto-generated method stub return arg0 == arg1; } @override public void destroyitem(viewgroup container, int position, object object) { // todo auto-generated method stub container.removeview((view) object); } @override public object instantiateitem(viewgroup container, int position) { int index = position % list.size(); container.addview(list.get(index)); return list.get(index); } }
com.example.monthextwo.bean包下的adv.java:
public class adv { string pic; public string getpic() { return pic; } public void setpic(string pic) { this.pic = pic; } @override public string tostring() { return "avd [pic=" + pic + "]"; } }
brand.java: *这是个空文件*
public class category { }
category.java: *这是个空文件*
public class category { }
dataresult.java:
public class dataresult { result result; public result getresult() { return result; } public void setresult(result result) { this.result = result; } @override public string tostring() { return "dataresult [result=" + result + "]"; } }
nation.java: *这是个空文件*
public class nation { }
result.java:
public class result { list<adv> advs = new arraylist<adv>(); list<brand> brands = new arraylist<brand>(); list<category> categorys = new arraylist<category>(); list<nation> nations = new arraylist<nation>(); public list<adv> getadvs() { return advs; } public void setadvs(list<adv> advs) { this.advs = advs; } public list<brand> getbrands() { return brands; } public void setbrands(list<brand> brands) { this.brands = brands; } public list<category> getcategorys() { return categorys; } public void setcategorys(list<category> categorys) { this.categorys = categorys; } public list<nation> getnations() { return nations; } public void setnations(list<nation> nations) { this.nations = nations; } @override public string tostring() { return "result [avds=" + advs + ", brands=" + brands + ", categorys=" + categorys + ", nations=" + nations + "]"; } }
com.example.monthextwo.net包下的netutils.java:
public class netutils { public static string getnetdata(string path){ string json = ""; try { url url = new url(path); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setrequestmethod("get"); int code = conn.getresponsecode(); if(code == 200){ inputstream is = conn.getinputstream(); bytearrayoutputstream bos = new bytearrayoutputstream(); int len = -1; byte[] buff = new byte[1024]; while((len = is.read(buff)) != -1){ bos.write(buff, 0, len); } json = new string(bos.tobytearray()); } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } return json; } }
以上就是全部的代码了,新手练习。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。