本文實(shí)例為大家分享了Android九宮格圖片展示的具體代碼,供大家參考,具體內(nèi)容如下
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、網(wǎng)頁空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、思南網(wǎng)站維護(hù)、網(wǎng)站推廣。
首先來看一下我們要做成的而效果:
主頁面要顯示一個(gè)viewpager自動(dòng)輪播+小圓點(diǎn)聯(lián)動(dòng)的效果 :
廢話不多說,直接上代碼:
布局文件:
activity_main.xml:
menu下的main.xml:
接下來看一下要用到的架包文件:
最后是Java文件:
首先是com.example.monthextwo包中的MainActivity.java:
public class MainActivity extends Activity { ViewPager vp; DataResult data; Listlist; List 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 (); 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 (){ @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 (); 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); } //創(chuàng)建適配器 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{ Listlist; public MyPagerAdatper(List 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: *這是個(gè)空文件*
public class Category { }
Category.java: *這是個(gè)空文件*
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: *這是個(gè)空文件*
public class Nation { }
Result.java:
public class Result { Listadvs = new ArrayList (); List brands = new ArrayList (); List categorys = new ArrayList (); List nations = new ArrayList (); public List getAdvs() { return advs; } public void setAdvs(List advs) { this.advs = advs; } public List getBrands() { return brands; } public void setBrands(List brands) { this.brands = brands; } public List getCategorys() { return categorys; } public void setCategorys(List categorys) { this.categorys = categorys; } public List getNations() { return nations; } public void setNations(List 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; } }
以上就是全部的代碼了,新手練習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。