public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{ private XListView xlv; private int page=1; private boolean flag; private MyBaseadapter adapter; private Listlist; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportActionBar().hide();//隐藏应用名 xlv = (XListView) findViewById(R.id.xlv); xlv.setPullLoadEnable(true); xlv.setXListViewListener(this);//xlistview所需的方法 data(); } public void data(){ //url网址 jiexi("http://api.kkmh.com/v1/daily/comic_lists/1487606400?since=0&gender=0&sa_event=eyJwcm9qZWN0Ijoia3VhaWthbl9hcHAiLCJ0aW1lIjoxNDg3ODI5MDIzNDgxLCJwcm9wZXJ0aWVzIjp7IkhvbWVwYWdlVGFiTmFtZSI6IueDremXqCIsIlZDb21tdW5pdHlUYWJOYW1lIjoi54Ot6ZeoIiwiJG9%22%20+%22zX3ZlcnNpb24iOiI0LjIuMiIsIkdlbmRlclR5cGUiOiLlpbPniYgiLCJGcm9tSG9tZXBhZ2VUYWJOYW1lIjoi54Ot6ZeoIiwiJGxpYl92ZXJzaW9uIjoiMS42LjM0IiwiJG5ldHdvcmtfdHlwZSI6IldJRkkiLCIkd2lmaSI6dHJ1ZSwiJG1hbnVmYWN0dXJlciI6InNhbXN1bmciLCJGcm9tSG9tZXBhZ2VVcGRhdGVEYXRlIjoyLCIkc2NyZWVuX2hlaWdodCI6NTc2LCJIb21lcGFnZVVwZGF0ZURhdGUiOjEsIlByb3BlcnR5RXZlbnQiOiJSZWFkSG9tZVBhZ2UiLCJGaW5kVGFiTmFtZSI6IuaOqOiNkCIsImFidGVzdF9ncm91cCI6NDYsIiRzY3JlZW5fd2lkdGgiOjEwMjQsIiRvcyI6IkFuZHJvaWQiLCJUcmlnZ2VyUGFnZSI6IkhvbWVQYWdlIiwiJGNhcnJpZXIiOiJDTUNDIiwiJG1vZGVsIjoiR1QtUDUyMTAiLCIkYXBwX3ZlcnNpb24iOiIzLjguMSJ9LCJ0eXBlIjoidHJhY2siLCJkaXN0aW5jdF9pZCI6IkE6OTA1MTA0Mjc2Mzc1NTEwOSIsIm9yaWdpbmFsX2lkIjoiQTo5MDUxMDQyNzYzNzU1MTA5IiwiZXZlbnQiOiJSZWFkSG9tZVBhZ2UifQ%3D%3D"); } public void jiexi(String path){ //解析网址显示数据 new AsyncTask (){ //使用异步的方法 @Override protected void onPostExecute(String s) { if (s!=null){ Gson gson=new Gson(); Bean bean = gson.fromJson(s, Bean.class); list = bean.getData().getComics(); if (adapter==null) { adapter = new MyBaseadapter(MainActivity.this, list); xlv.setAdapter(adapter); }else{ adapter.sss(list,flag); } xlv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView adapterView, View view, int i, long l) { Intent intent=new Intent(MainActivity.this,Main2.class); intent.putExtra("url",list.get(i).getUrl());//传值 startActivity(intent); } }); } } @Override protected String doInBackground(String... params) { try { URL url=new URL(params[0]); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(3000); connection.setConnectTimeout(3000); int code = connection.getResponseCode(); if (code==200){ InputStream is = connection.getInputStream(); return info.readFromNetWork(is); } } catch (Exception e) { e.printStackTrace(); } return null; } }.execute(path); } @Override public void onRefresh() { //上啦刷新 flag=false; ++page; data(); xlv.stopRefresh(true); } @Override public void onLoadMore() { //下拉加载 flag=true; ++page; data(); xlv.stopLoadMore(); }}
public class MyBaseadapter extends BaseAdapter { private Listlist; private Context context; public MyBaseadapter(Context context, List list) { this.context=context; this.list=list; } public void sss(List data,boolean flag){ for (Bean.DataBean.ComicsBean s: data) { if (flag){ list.add(s); }else{ list.add(0,s); } } notifyDataSetChanged(); } @Override public int getCount() { return list!=null?list.size():0; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHoder hoder=null; if (convertView==null){ hoder=new ViewHoder(); convertView=View.inflate(context,R.layout.item,null); hoder.manhuaming= (TextView) convertView.findViewById(manhuaming); hoder.zuozhe= (TextView) convertView.findViewById(zuozhe); hoder.zhuangjie= (TextView) convertView.findViewById(zhuangjie); hoder.dianzan= (TextView) convertView.findViewById(dianzan); hoder.pinglun= (TextView) convertView.findViewById(pinglun); hoder.image= (ImageView) convertView.findViewById(R.id.image); convertView.setTag(hoder); }else{ hoder= (ViewHoder) convertView.getTag(); } hoder.manhuaming.setText(" "+list.get(position).getTopic().getTitle()); hoder.zuozhe.setText("作者:"+list.get(position).getTopic().getUser().getNickname()); hoder.zhuangjie.setText("最新章节:"+list.get(position).getTitle()); hoder.dianzan.setText("点赞: "+list.get(position).getLikes_count()); hoder.pinglun.setText("评论: "+list.get(position).getComments_count()); ImageLoader.getInstance().displayImage(list.get(position).getCover_image_url(),hoder.image,appliction.getImageOptions()); return convertView; }//listview优化 class ViewHoder{ private TextView manhuaming,zuozhe,zhuangjie,dianzan,pinglun; private ImageView image; }}
public class Main2 extends AppCompatActivity { private WebView web; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity2); getSupportActionBar().hide(); web = (WebView) findViewById(R.id.web); Intent url=getIntent(); String s = url.getStringExtra("url");//接收传过来的值 web.loadUrl(s);//使用webview显示 }