主Activity
public class MainActivity extends AppCompatActivity { ListView lv; //ListView beans bs; //bean包 util ut; //获取网络数据的包 List<beans.ResultBean.ListBean> list; //bean包的集合用于传值 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); ListGson(); //ListView 的监听事件 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { //传值过去...url路径 Intent intent = new Intent(MainActivity.this,WebActivity.class); intent.putExtra("bs",list.get(i).getUrl()); startActivity(intent); Toast.makeText(MainActivity.this,list.get(i).getTitle(),Toast.LENGTH_SHORT).show(); } }); } //ListView的解析 public void ListGson(){ new AsyncTask<String,String,String>(){ @Override protected String doInBackground(String... strings) { String read = util.getNetDataStr(); // 使用Gson把该字符输入流转换成JavaBean Gson Lvgson = new Gson(); bs = Lvgson.fromJson(read, beans.class); list = bs .getResult().getList(); return null; } @Override protected void onPostExecute(String result) { MyApdateF myApdateF =new MyApdateF(); lv.setAdapter(myApdateF); myApdateF.notifyDataSetChanged(); super.onPostExecute(result); } }.execute(); } //ListView适配器 class MyApdateF extends BaseAdapter { @Override public int getCount() { return bs == null?0:bs.getResult().getList().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; if (view == null){ view = View.inflate(MainActivity.this,R.layout.list_x,null); //初始化holder holder = new ViewHolder(); holder.iv = view.findViewById(R.id.iv); holder.tv1 = view.findViewById(R.id.tv1); holder.tv2=view.findViewById(R.id.tv2); //赋值 view.setTag(holder); }else{ //取值 holder = (ViewHolder) view.getTag(); } holder.tv1.setText(bs.getResult().getList().get(i).getSource()); holder.tv2.setText(bs.getResult().getList().get(i).getTitle()); ImageLoader.getInstance().displayImage(bs.getResult().getList().get(i).getFirstImg(),holder.iv); return view; } } class ViewHolder{ TextView tv1, tv2; ImageView iv; } }
WebView的主方法
public class WebActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_web); WebView webView = (WebView) findViewById(R.id.Wv); Intent intent = getIntent(); String url = intent.getStringExtra("bs"); //加载 webView.loadUrl(url); //设置 webView.setWebViewClient(new WebViewClient()); WebSettings settings = webView.getSettings(); settings.setJavaScriptEnabled(true); settings.setJavaScriptCanOpenWindowsAutomatically(true); } }
URL 获取网络数据
public class util { //ListView public static String getNetDataStr() { String json = null; try { URL url = new URL( "http://v.juhe.cn/weixin/query?key=88f7bbc507e3ecacfaeab2b47dd8936f"); 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; } }
轮播图的数据解析
//轮播图的解析 public void getBeanByGson() { // 使用该方法得到了一个 Json的字符输入流 new AsyncTask<String,String,String>(){ @Override protected String doInBackground(String... strings) { Reader reader = NetuS.getNetData(); // 使用Gson把该字符输入流转换成JavaBean Gson gson = new Gson(); Ib = gson.fromJson(reader, Imagebean.class); return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); list=new ArrayList<ImageView>(); for (int i = 0; i <Ib.getBanner().size() ; i++) { ImageView imageView = new ImageView(getActivity()); imageView.setScaleType(ImageView.ScaleType.FIT_XY); ImageLoader.getInstance().displayImage(Ib.getBanner().get(i).getImage_url(),imageView); list.add(imageView); } MyPagerAdapter myPagerAdapter = new MyPagerAdapter(); VpF.setAdapter(myPagerAdapter); handler.sendEmptyMessageDelayed(1,1000); } }.execute(); }
ImageLoad所需要继承的类
public class Myapps extends Application { //ImageLoader所需要继承的类 @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this) .threadPoolSize(5) .build(); ImageLoader.getInstance().init(configuration); } }
Handler用于实现无限轮播
//用于实现无限轮播 private Handler handler = new Handler(){ public void handleMessage(Message msg) { //获取当前条目 int index = VpF.getCurrentItem(); //向右轮播 VpF.setCurrentItem(index+=1); //间隔2秒 handler.sendEmptyMessageDelayed(2,2000); }; };