package com.example.lxb20170913; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RadioGroup; import android.widget.TextView; import com.google.gson.Gson; import com.nostra13.universalimageloader.core.ImageLoader; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private StringBuilder builder; private URL url; private ViewPager vp; List<Bean.NewslistBean> list = new ArrayList<Bean.NewslistBean>(); int[]i={R.id.b1,R.id.b2,R.id.b3,R.id.b4,R.id.b5,R.id.b6};//Radiobutoon的控件 Handler handler=new Handler(){//自动轮播 @Override public void handleMessage(Message msg) { super.handleMessage(msg); int i = vp.getCurrentItem()+1;//获取下一页的值 vp.setCurrentItem(i);//显示下一页 handler.sendEmptyMessageDelayed(0,2000);//延时操作 } }; private LinearLayout ll; private ArrayList<Object> ivlist; private ImageView imageView; private GridView gv; private mydata2 mydata2; private RadioGroup rg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rg =(RadioGroup)findViewById(R.id.rg); vp =(ViewPager)findViewById(R.id.vp); gv =(GridView)findViewById(R.id.gv); new Thread(){ @Override public void run() { super.run(); //读取数据 try { url = new URL("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=6&page=1"); URLConnection connection = url.openConnection(); InputStream stream = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); builder = new StringBuilder(); String tem=""; while((tem=reader.readLine())!=null){ builder.append(tem); } Log.e("====读取到的数据========",builder.toString()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } runOnUiThread(new Runnable() { @Override public void run() { //解析数据 Gson gson = new Gson(); Bean json = gson.fromJson(builder.toString(), Bean.class); list = json.getNewslist(); for (int i=0;i<list.size();i++){ String picUrl = list.get(i).getPicUrl(); } vp.setAdapter(new mydata()); vp.setCurrentItem(10000*list.size());// handler.sendEmptyMessageDelayed(0,2000);//开启handler vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { rg.check(i[position%list.size()]);// } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { } }); //GridView mydata2 = new mydata2(); gv.setAdapter(mydata2); } }); } }.start(); } class mydata2 extends BaseAdapter{ @Override public int getCount() { return Integer.MAX_VALUE; } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder vh; if(view==null){ vh=new ViewHolder(); view=View.inflate(MainActivity.this,R.layout.textview,null); vh.img=(ImageView) view.findViewById(R.id.img); vh.name=(TextView) view.findViewById(R.id.name); view.setTag(vh); }else{ vh=(ViewHolder)view.getTag(); } ImageLoader.getInstance().displayImage(list.get(i%list.size()).getPicUrl(),vh.img); vh.name.setText(list.get(i%list.size()).getTitle()); return view; } class ViewHolder{ ImageView img; TextView name; } } class mydata extends PagerAdapter { @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) { View view = View.inflate(MainActivity.this, R.layout.textview, null); ImageView img=(ImageView)view.findViewById(R.id.img); TextView name=(TextView)view.findViewById(R.id.name); ImageLoader.getInstance().displayImage(list.get(position%list.size()).getPicUrl(),img); name.setText(list.get(position%list.size()).getTitle()); container.addView(view); return view; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } } }
ViewPager无限轮播加数据展示
最新推荐文章于 2024-03-21 10:04:17 发布