资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Androidviewpager自动轮播和小圆点联动效果

本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、网页空间、营销软件、网站建设、思南网站维护、网站推广。

首先来看一下我们要做成的而效果:

主页面要显示一个viewpager自动轮播+小圆点联动的效果 :

Android viewpager自动轮播和小圆点联动效果

废话不多说,直接上代码:

布局文件:

Android viewpager自动轮播和小圆点联动效果

activity_main.xml:



  

    
    

    
    
  

  
  

  
  

  
  



menu下的main.xml:



  



接下来看一下要用到的架包文件:

Android viewpager自动轮播和小圆点联动效果

最后是Java文件:

Android viewpager自动轮播和小圆点联动效果

首先是com.example.monthextwo包中的MainActivity.java:

public class MainActivity extends Activity {

  ViewPager vp;
  DataResult data;
  List list;
  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);
      }
      //创建适配器
      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 list;

  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: *这是个空文件*

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 advs = 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;
 }
}

以上就是全部的代码了,新手练习。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


网站标题:Androidviewpager自动轮播和小圆点联动效果
网站URL:http://cdkjz.cn/article/gjsieo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220