import android.media.Image;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ListView;
import com.google.gson.Gson;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ImageView img;
String urldata="http://v.juhe.cn/toutiao/index?type=top&key=444da40ec8ee43818073d7131c2ffa8f";
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = findViewById(R.id.lv);
mytask mytask = new mytask();
mytask.execute();
}
class mytask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
try {
URL url = new URL(urldata);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
if (urlConnection.getResponseCode()==200){
InputStream inputStream = urlConnection.getInputStream();
String s=statemstring(inputStream);
return s;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(s!=null){
Gson gson = new Gson();
Bean bean = gson.fromJson(s, Bean.class);
List<Bean.ResultBean.DataBean> data = bean.getResult().getData();
Myadapter myadapter = new Myadapter(MainActivity.this, data);
lv.setAdapter(myadapter);
}
}
}
public String statemstring(InputStream inputStream){
ByteArrayOutputStream outputStream=null;
outputStream = new ByteArrayOutputStream();
byte[]b=new byte[1024];
int len;
try {
while((len=inputStream.read(b))!=-1){
outputStream.write(b,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}
return outputStream.toString();
}
}
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
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.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import java.io.File;
import java.util.List;
/**
* Created by 联想 on 2018/1/3.
*/
public class Myadapter extends BaseAdapter {
private Context context;
private List<Bean.ResultBean.DataBean> data;
private DisplayImageOptions options;;
//定义三种类型
private final int ONE_IMG=0;
private final int TWO_IMG=1;
private final int THREE_IMG2=2;
public Myadapter(Context context, List<Bean.ResultBean.DataBean> data) {
this.context = context;
this.data = data;
options = new DisplayImageOptions.Builder()
.cacheInMemory(true)//使用内存缓存
.cacheOnDisk(true)//使用磁盘缓存
.showImageOnLoading(R.mipmap.ic_launcher)//设置正在下载的图片
.showImageForEmptyUri(R.mipmap.ic_launcher)//url为空或请求的资源不存在时
.showImageOnFail(R.mipmap.ic_launcher)//下载失败时显示的图片
.bitmapConfig(Bitmap.Config.RGB_565)//设置图片色彩模式 1px=2字节
.imageScaleType(ImageScaleType.EXACTLY)//设置图片的缩放模式
.displayer(new RoundedBitmapDisplayer(100))//设置圆角 30代表半径 自定义
.build();
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public int getItemViewType(int position) {
String pic_s = data.get(position).getThumbnail_pic_s();
String pic_s02 = data.get(position).getThumbnail_pic_s02();
String pic_s03 = data.get(position).getThumbnail_pic_s03();
if (pic_s!=null&&pic_s02==null&&pic_s03==null){
return ONE_IMG;
}else if(pic_s==null&&pic_s02!=null&&pic_s03==null){
return TWO_IMG;
}else{
return THREE_IMG2;
}
}
@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 type = getItemViewType(i);
if (type==ONE_IMG) {
Viewholder holder;
if (view==null){
view = View.inflate(context,R.layout.second1,null);
holder=new Viewholder();
holder.img=view.findViewById(R.id.img);
holder.tv=view.findViewById(R.id.tv);
view.setTag(holder);
}else{
holder= (Viewholder) view.getTag();
}
Log.d("--", "getView: "+data.get(i).getTitle());
ImageLoader.getInstance().displayImage(data.get(i).getThumbnail_pic_s(),holder.img,options);
holder.tv.setText(data.get(i).getTitle());
return view;
}else if (type==TWO_IMG){
Viewholder1 holder1;
if (view==null){
view = View.inflate(context,R.layout.view,null);
holder1=new Viewholder1();
holder1.img1=view.findViewById(R.id.img1);
holder1.img2=view.findViewById(R.id.img2);
holder1.tv01=view.findViewById(R.id.tv01);
view.setTag(holder1);
}else{
holder1= (Viewholder1) view.getTag();
ImageLoader.getInstance().displayImage(data.get(i).getThumbnail_pic_s02(),holder1.img1,options);
ImageLoader.getInstance().displayImage(data.get(i).getThumbnail_pic_s02(),holder1.img2,options);
}
// Log.d("--", "getView: "+data.get(i).getTitle());
holder1.tv01.setText(data.get(i).getTitle());
return view;
}else {
Viewholder2 holder2;
if (view==null){
view = View.inflate(context,R.layout.view3,null);
holder2=new Viewholder2();
holder2.imgv1=view.findViewById(R.id.imgv1);
holder2.imgv2=view.findViewById(R.id.imgv2);
holder2.imgv3=view.findViewById(R.id.imgv3);
holder2.tvv=view.findViewById(R.id.tvv);
view.setTag(holder2);
}else{
holder2= (Viewholder2) view.getTag();
ImageLoader.getInstance().displayImage(data.get(i).getThumbnail_pic_s03(),holder2.imgv1,options);
ImageLoader.getInstance().displayImage(data.get(i).getThumbnail_pic_s03(),holder2.imgv2,options);
ImageLoader.getInstance().displayImage(data.get(i).getThumbnail_pic_s03(),holder2.imgv3,options);
}
// Log.d("--", "getView: "+data.get(i).getTitle());
holder2.tvv.setText(data.get(i).getTitle());
return view;
}
}
private class Viewholder{
ImageView img;
TextView tv;
}
private class Viewholder1{
ImageView img1;
ImageView img2;
TextView tv01;
}
private class Viewholder2{
ImageView imgv1;
ImageView imgv2;
ImageView imgv3;
TextView tvv;
}
}
主布局文件:<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv"></ListView>
子布局文件为三个:1<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/ic_launcher"
android:id="@+id/img"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:text="asdasd"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/img"/>
2.<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="@+id/img1"/>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="@+id/img2"
android:layout_toRightOf="@id/img1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv01"
android:layout_toRightOf="@id/img2"/>
3.<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvv"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/tv">
<ImageView
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_marginTop="50dp"
android:id="@+id/imgv1"
android:layout_weight="1"/>
<ImageView
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_marginTop="50dp"
android:id="@+id/imgv2"
android:layout_weight="1"/>
<ImageView
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_marginTop="50dp"
android:id="@+id/imgv3"
android:layout_weight="1"/>
</LinearLayout>
注:在xml文件中配置权限