<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.wz.yuekao0528.MainActivity"> <me.maxwin.view.XListView android:id="@+id/xlistview" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" /> <TextView android:id="@+id/source" android:layout_width="match_parent" android:layout_height="20dp" android:textSize="12sp" /> </LinearLayout><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="60dp" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp"/> <TextView android:id="@+id/source" android:layout_width="match_parent" android:layout_height="20dp" android:textSize="12sp"/> </LinearLayout> <ImageView android:id="@+id/image1" android:layout_width="80dp" android:layout_height="60dp"/> </LinearLayout><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:orientation="horizontal"> <ImageView android:id="@+id/image1" android:layout_width="0dp" android:layout_height="60dp" android:layout_weight="1" /> <ImageView android:id="@+id/image2" android:layout_width="0dp" android:layout_height="60dp" android:layout_weight="1" /> <ImageView android:id="@+id/image3" android:layout_width="0dp" android:layout_height="60dp" android:layout_weight="1" /> </LinearLayout> <TextView android:id="@+id/source" android:layout_width="match_parent" android:layout_height="20dp" android:textSize="12sp" /> </LinearLayout>package com.example.wz.imageloade; import android.os.Bundle; import android.os.Handler; import android.os.Message; 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.ImageView; import android.widget.TextView; import com.google.gson.Gson; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import com.socks.library.KLog; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; import me.maxwin.view.XListView; public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener { private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 1) { if (page == 1) mList.clear(); page++; Data data = (Data) msg.obj; mList.addAll(data.getData()); mAdapter.notifyDataSetChanged(); stopLoad(); } } }; private List<Data.DataBean> mList; private XListView mXListView; private MyAdapter mAdapter; private int page = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); loadData(); } private void initView() { mList = new ArrayList<>(); mXListView = (XListView) findViewById(R.id.xlistview); mXListView.setPullLoadEnable(true); mXListView.setXListViewListener(this); mAdapter = new MyAdapter(); mXListView.setAdapter(mAdapter); } /** * 网络请求 */ private void loadData() { new Thread() { @Override public void run() { String urlPath = "http://ic.snssdk.com/2/article/v25/stream/?category=news_tech&count=20&bd_latitude=4.9E-324&bd_longitude=4.9E-324&bd_loc_time=1457695555&loc_mode=5&lac=4527&cid=28883&iid=3835029558&device_id=12211880440&ac=wifi&channel=baidu&aid=13&app_name=news_article&version_code=460&device_platform=android&device_type=iToolsAVM&os_api=" + page; try { URL url = new URL(urlPath); //利用HttpURLConnection对象从网络中获取网页数据 // 打开连接 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.connect();//建立连接 //判断 if (urlConnection.getResponseCode() == 200) { // 调用HttpURLConnection连接对象的getInputStream()函数, // // 将内存缓冲区中封装好的完整的HTTP请求电文发送到服务端。 //得到输入流 InputStream in = urlConnection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(in)); StringBuffer sb = new StringBuffer(); String line = null; while ((line = br.readLine()) != null) { sb.append(line); } Log.e("loadData", "loadData:" + sb.toString()); KLog.json(sb.toString()); Gson gson = new Gson(); Data data = gson.fromJson(sb.toString(), Data.class); Message msg = Message.obtain(); msg.what = 1; msg.obj = data; mHandler.sendMessage(msg); } else { Log.e("loadData", "loadData:网络异常---" + urlConnection.getResponseCode()); } } catch (Exception e) { e.printStackTrace(); } } }.start(); } private void stopLoad() { mXListView.stopRefresh();//停止刷新,重置header view mXListView.stopLoadMore();//停止加载更多,重置footer view mXListView.setRefreshTime("刚刚");//设置上次刷新的时间 } @Override public void onRefresh() { page = 1; loadData(); } @Override public void onLoadMore() { loadData(); } class MyAdapter extends BaseAdapter { final int TYPE0 = 0; final int TYPE1 = 1; final int TYPE3 = 2; ImageLoader imageLoader; DisplayImageOptions options; public MyAdapter() { imageLoader = ImageLoader.getInstance(); ImageLoaderConfiguration config = ImageLoaderConfiguration.createDefault(MainActivity.this); imageLoader.init(config); options = new DisplayImageOptions.Builder() .showImageOnLoading(R.mipmap.ic_launcher) .build(); } @Override public int getCount() { return mList.size(); } @Override public Object getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return position; } @Override public int getItemViewType(int size) { switch (size) { case 0: return TYPE0; case 1: return TYPE1; case 3: return TYPE3; default: return TYPE0; } } @Override public int getViewTypeCount() { return 3; } @Override public View getView(int position, View convertView, ViewGroup parent) { Data.DataBean bean = mList.get(position); List<Data.DataBean.ImageListBean> listImages = bean.getImage_list(); int size = listImages != null ? bean.getImage_list().size() : 0; int type = getItemViewType(size); KLog.e(position + "/" + size + "/" + type); ViewHolder holder = null; convertView = null; if (convertView == null) { holder = new ViewHolder(); switch (type) { case TYPE0: convertView = View.inflate(MainActivity.this, R.layout.item0, null); break; case TYPE1: convertView = View.inflate(MainActivity.this, R.layout.item1, null); holder.image1 = (ImageView) convertView.findViewById(R.id.image1); break; case TYPE3: convertView = View.inflate(MainActivity.this, R.layout.item3, null); holder.image1 = (ImageView) convertView.findViewById(R.id.image1); holder.image2 = (ImageView) convertView.findViewById(R.id.image2); holder.image3 = (ImageView) convertView.findViewById(R.id.image3); break; } holder.title = (TextView) convertView.findViewById(R.id.title); holder.source = (TextView) convertView.findViewById(R.id.source); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.title.setText(bean.getTitle() + "-" + size); holder.source.setText(bean.getSource()); switch (type) { case TYPE1: imageLoader.displayImage(listImages.get(0).getUrl(), holder.image1, options); break; case TYPE3: imageLoader.displayImage(listImages.get(0).getUrl(), holder.image1, options); imageLoader.displayImage(listImages.get(1).getUrl(), holder.image2, options); imageLoader.displayImage(listImages.get(2).getUrl(), holder.image3, options); break; } return convertView; } } class ViewHolder { TextView title, source; ImageView image1, image2, image3; } }
网络请求+ImageLoader
最新推荐文章于 2020-12-29 00:07:45 发布