多条目 imageloader
activity
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="zhanghaijiao.bawei.com.week2_kaoshi.MainActivity"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lv"></ListView> </LinearLayout>
item
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="80dp" android:layout_height="80dp" android:id="@+id/img"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv" android:layout_toRightOf="@+id/img"/> </RelativeLayout>
item1
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv" /> <ImageView android:layout_width="match_parent" android:layout_height="80dp" android:id="@+id/img" android:layout_below="@+id/tv"/> </RelativeLayout>
main_activity
package zhanghaijiao.bawei.com.week2_kaoshi; import android.content.BroadcastReceiver; import android.content.Intent; import android.content.IntentFilter; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ListView; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Type; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.List; import javax.xml.transform.Result; public class MainActivity extends AppCompatActivity { private Handler myHandler=new Handler(){ @Override public void handleMessage(Message msg) { List<ResultData.ItemBean> data=( List<ResultData.ItemBean>)msg.obj; //设置适配器,进行显示 MyAdapter adapter=new MyAdapter(MainActivity.this,data); listView.setAdapter(adapter); } }; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = findViewById(R.id.lv); //进行网络请求 new Thread(){ @Override public void run() { refquestNetData(); } }.start(); } private void refquestNetData() { try { URL url=new URL("http://api.iclient.ifeng.com/ClientNews?id=YL53,FOCUSYL53&action=default&pullNum=1&uid=864678036850608&devid=864678036850608&proid=ifengnews&vt=5&publishid=6103&screen=1080x1920&df=androidphone&os=android_22&nw=wifi"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setConnectTimeout(5000); urlConnection.setReadTimeout(5000); int code=urlConnection.getResponseCode(); if(code==200){ //获取数据 InputStream inputStream = urlConnection.getInputStream(); String jsonData=inputstreamToString(inputStream); List<ResultData> datas=new ArrayList<ResultData>(); //得到对应的数据类型type对象 Type mty=new TypeToken< List<ResultData>>(){}.getType(); //进行解析 Gson gson=new Gson(); datas=gson.fromJson(jsonData,mty); //获取列表要显示的数据 List<ResultData.ItemBean> item = datas.get(0).getItem(); //给主线程发送消息 Message msg=Message.obtain(); msg.obj=item; myHandler.sendMessage(msg); } } catch (Exception e) { e.printStackTrace(); } } public String inputstreamToString(InputStream stream){ StringBuilder builder=new StringBuilder(); try { BufferedReader reader=new BufferedReader(new InputStreamReader(stream)); String str; while ((str=reader.readLine())!=null){ builder.append(str); } } catch (IOException e) { e.printStackTrace(); } return builder.toString(); } }
Myapp
package zhanghaijiao.bawei.com.week2_kaoshi; import android.app.Application; import android.os.Environment; import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache; import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import java.io.File; /** * Created by jane on 2018/2/23. */ public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); File cachefile=new File(Environment.getExternalStorageDirectory().getPath()+"/image"); ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this) .memoryCacheExtraOptions(480, 800)//缓存图片最大的长和宽 .threadPoolSize(2)//线程池的数量 .threadPriority(4) .memoryCacheSize(2*1024*1024)//设置内存缓存区大小 .diskCacheSize(20*1024*1024)//设置sd卡缓存区大小 .diskCache(new UnlimitedDiscCache(cachefile))//自定义缓存目录 .writeDebugLogs()//打印日志内容 .diskCacheFileNameGenerator(new Md5FileNameGenerator())//给缓存的文件名进行md5加密处理 .build(); ImageLoader.getInstance().init(configuration); } }
Myadapter
package zhanghaijiao.bawei.com.week2_kaoshi; import android.content.Context; import android.graphics.Bitmap; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.assist.ImageScaleType; import java.util.List; /** * Created by jane on 2018/2/23. */ public class MyAdapter extends BaseAdapter{ private Context context; private List<ResultData.ItemBean> data; private DisplayImageOptions options; /** * 图片在下面 */ private final int ONE_ITEM=0; /** * 图片在左边 */ private final int TWO_ITEM=1; public MyAdapter(Context context, List<ResultData.ItemBean> data) { this.context = context; this.data = data; options=new DisplayImageOptions.Builder() .cacheInMemory(true)//使用内存缓存 .cacheOnDisk(true)//使用磁盘缓存 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片色彩模式 .imageScaleType(ImageScaleType.EXACTLY)//设置图片的缩放模式 .build(); } @Override public int getViewTypeCount() { return 2; } @Override public int getItemViewType(int position) { String type=data.get(position).getType(); if("slide".equals(type)){ return ONE_ITEM; }else { return TWO_ITEM; } } @Override public int getCount() { return data.size(); } @Override public Object getItem(int i) { return data.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { int viewType=getItemViewType(i); if(viewType==ONE_ITEM){ ViewHolder holder; if(view==null){ view=View.inflate(context,R.layout.item2,null); holder=new ViewHolder(); holder.img=view.findViewById(R.id.img); holder.textView=view.findViewById(R.id.tv); view.setTag(holder); }else { holder= (ViewHolder) view.getTag(); } holder.textView.setText(data.get(i).getTitle()); if(data.get(i).getThumbnail()!=null){ ImageLoader.getInstance().displayImage(data.get(i).getThumbnail(),holder.img,options); } return view; }else { ViewHolder holder; if(view==null){ view=View.inflate(context,R.layout.item1,null); holder=new ViewHolder(); holder.img=view.findViewById(R.id.img); holder.textView=view.findViewById(R.id.tv); view.setTag(holder); }else { holder= (ViewHolder) view.getTag(); } holder.textView.setText(data.get(i).getTitle()); if(data.get(i).getThumbnail()!=null){ ImageLoader.getInstance().displayImage(data.get(i).getThumbnail(),holder.img,options); } return view; } } class ViewHolder{ ImageView img; TextView textView; } }
data
...........