private String url="http://172.17.8.100/mobile/exam/findCommodityList"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); new anyc().execute(); } class anyc extends AsyncTask<Integer,Integer,String>{ private ViewPager pager; private ImageView imageview; private GridView gridview; private ArrayList<ImageView> list2; private List<Bean.ResultBean.PicsBean> mlist; @Override protected String doInBackground(Integer... integers) { String message=""; try { URL urll = new URL(url); HttpURLConnection connection = (HttpURLConnection) urll.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.connect(); int code = connection.getResponseCode(); if (code==HttpURLConnection.HTTP_OK) { InputStream stream = connection.getInputStream(); ByteArrayOutputStream bout = new ByteArrayOutputStream(); int len = 1; byte[] bytes = new byte[512]; while ((len = stream.read(bytes)) != -1) { bout.write(bytes, 0, len); } message = bout.toString(); connection.disconnect(); stream.close(); } } catch (Exception e) { e.printStackTrace(); } return message; } @Override protected void onPostExecute(String s) { Gson gson = new Gson(); Bean bean = gson.fromJson(s, Bean.class); final List<Bean.ResultBean.ProductBean> list = bean.getResult().getProduct(); mlist = bean.getResult().getPics(); final ArrayList<ImageView> list2 = new ArrayList<ImageView>(); //轮播图 for (int i = 1; i< mlist.size(); i++){ ImageView imageView = new ImageView(Main2Activity.this); ImageLoader.getInstance().displayImage(mlist.get(i).getPic(),imageView); list2.add(imageView); }d handler.sendEmptyMessageDelayed(0,3000); pager.setAdapter(new PagerAdapter() { @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(@NonNull View arg0, @NonNull Object arg1) { return arg0==arg1; } @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { container.removeView((View) object); } @Override public Object instantiateItem(@NonNull ViewGroup container, final int position) { ImageView imageView = list2.get(position%list2.size()); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(Main2Activity.this, WebActivity.class); intent.putExtra("url",mlist.get(position).getUrl()); startActivity(intent); } }); container.addView(imageView); return imageView; } }); //gridview优化 gridview.setAdapter(new BaseAdapter() { @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder holder=new ViewHolder(); if (view==null){ view=View.inflate(Main2Activity.this,R.layout.grideview,null); holder.tv2=view.findViewById(R.id.textView2); holder.imageview=view.findViewById(R.id.image); view.setTag(holder); }else{ holder= (ViewHolder) view.getTag(); } // holder.tv1.setText(list.get(i).getImg()); holder.tv2.setText(list.get(i).getName()); ImageLoader.getInstance().displayImage(list.get(i).getImg(),holder.imageview); return view; } class ViewHolder{ TextView tv1,tv2; ImageView imageview; } }); } @Override protected void onPreExecute() { pager = findViewById(R.id.pager); gridview = findViewById(R.id.gridview); } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); } private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ case 0: int currentItem = pager.getCurrentItem(); currentItem+=1; pager.setCurrentItem(currentItem); handler.sendEmptyMessageDelayed(0,3000); } } }; }
08-15
08-15