import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
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.example.weekend_03.utils.ImageLoaderUtils;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader; import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List; /**
* Created by lenovo on 2017/12/19.
*/ class MyAdapter extends BaseAdapter{
private List<Result.DataBean>list;
private Context context;
public MyAdapter(List<Result.DataBean> list, Context context) {
this.list = list;
this.context = context;
}
//定义了两个常量,注意定义的常量值一定要小于 类型的总数 (2)
private final int HAS_IMAGE=0;//有图
private final int NO_IMAGE=1;//无图 //得到item条目的类型总数
@Override
public int getViewTypeCount() {
return 2;
} //根据条目的下标,返回此条目对应的类型,返回值是int类型
//position:item的索引值,下标
@Override
public int getItemViewType(int position) {
if(list.get(position).getIMAGEURL()==null){//偶数,返回无图类型
return NO_IMAGE;
}else{
return HAS_IMAGE;
}
} @Override
public int getCount() {
return list.size();
} @Override
public Object getItem(int i) {
return list.get(i);
} @Override
public long getItemId(int i) {
return i;
} @Override
public View getView(int i, View view, ViewGroup viewGroup) { if (getItemViewType(i)==HAS_IMAGE){
view = View.inflate(context,R.layout.itme,null);
TextView title = view.findViewById(R.id.title);
ImageView img = view.findViewById(R.id.img); title.setText(list.get(i).getTITLE());
Log.d("zzz",list.get(i).getIMAGEURL());
ImageLoader.getInstance().displayImage(list.get(i).getIMAGEURL(),img, ImageLoaderUtils.getImageOptions(context)); }else{
view = View.inflate(context,R.layout.itme_1,null);
TextView text1 = view.findViewById(R.id.text1);
text1.setText(list.get(i).getTITLE()); } return view;
} } ========================================================================================
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView; import com.example.weekend_03.utils.ImageLoaderUtils;
import com.nostra13.universalimageloader.core.ImageLoader; import java.util.List; /**
* Created by lenovo on 2017/12/19.
*/ class MyPagerAdapter extends PagerAdapter {
private List<String>urls;
private Context context; public MyPagerAdapter(List<String> urls, Context context) {
this.urls = urls;
this.context = context;
} @Override
public int getCount() {
return Integer.MAX_VALUE;
} @Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
} @Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView=new ImageView(context);
//加载显示图片
ImageLoader.getInstance().displayImage(urls.get(position%urls.size()),imageView, ImageLoaderUtils.getImageOptions(context));
//添加进去
container.addView(imageView); //添加点击事件
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =new Intent(context,Main2Activity.class);
intent.putExtra("key","http://www.baidu.com");
context.startActivity(intent);
}
});
return imageView;
} @Override
public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object);
}
}
05-04 07:23