public classM2ActivityextendsAppCompatActivityimplementsXListView.IXListViewListener {
privateBannerbanner;
privateListlist;
privateListimagelist;
privateXListViewxListView;
privateMyAdapteradapter;
private intpage=1;
// hand
privateHandlerhandler=newHandler() {
@Override
public voidhandleMessage(Message msg) {
String s = msg.obj.toString();
Gson gson =newGson();
PicLun picLun = gson.fromJson(s,PicLun.class);
list.addAll(picLun.getList());
adapter.notifyDataSetChanged();
List beee = picLun.getList();
imagelist=newArrayList<>();
for(PicLun.ListBean bean : beee) {
String pic = bean.getPic();
String[] split = pic.split("\\|");
imagelist.add(split[1]);
}
banner.setImages(imagelist);
banner.start();
stopXlist();
}
private voidstopXlist() {
xListView.stopLoadMore();
xListView.stopRefresh();
xListView.setRefreshTime("刚刚");
}
};
@Override
protected voidonCreate(@NullableBundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.m2activity);
add();
loadDate();
}
// 网络请求
private voidloadDate() {
newThread() {
@Override
public voidrun() {
String urlConnect = Net.getUrlConnect("http://qhb.2dyt.com/Bwei/news?type=5&postkey=1503d&page="+page);
Message message = Message.obtain();
message.obj= urlConnect;
handler.sendMessage(message);
}
}.start();
}
// 寻找控件ID
private voidadd() {
list=newArrayList<>();
xListView= (XListView) findViewById(R.id.xlist);
adapter=newMyAdapter();
xListView.setAdapter(adapter);
xListView.setPullLoadEnable(true);
xListView.setXListViewListener(this);
banner= (Banner) findViewById(R.id.banner);
banner.setImageLoader(newImageL(this));
banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
banner.setIndicatorGravity(BannerConfig.CENTER);
}
@Override
public voidonRefresh() {
page++;
list.clear();
loadDate();
}
@Override
public voidonLoadMore() {
page++;
loadDate();
}
// 适配器
classMyAdapterextendsBaseAdapter {
final intTYPE1=0;
final intTYPE2=1;
ImageLoaderimageLoader;
DisplayImageOptionsoptions;
publicMyAdapter() {
imageLoader= ImageLoader.getInstance();
// File file = new File(Environment.getExternalStorageDirectory(), "Bawei");
// if (!file.exists())
// file.mkdir();
ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(M2Activity.this);
imageLoader.init(configuration);
options=newDisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher).cacheOnDisk(true).build();
}
@Override
public intgetCount() {
returnlist.size();
}
@Override
publicObjectgetItem(inti) {
returnlist.get(i);
}
@Override
public longgetItemId(inti) {
returni;
}
@Override
public intgetItemViewType(intposition) {
returnlist.get(position).getType() ==1?TYPE1:TYPE2;
}
@Override
public intgetViewTypeCount() {
return2;
}
@Override
publicViewgetView(inti,View view,ViewGroup viewGroup) {
inttype = getItemViewType(i);
ViewHolder holder;
if(view ==null) {
holder =newViewHolder();
switch(type) {
caseTYPE1:
view = View.inflate(M2Activity.this,R.layout.item, null);
holder.textView= (TextView) view.findViewById(R.id.t1);
holder.image1= (ImageView) view.findViewById(R.id.i1);
holder.image2= (ImageView) view.findViewById(R.id.i2);
break;
caseTYPE2:
view = View.inflate(M2Activity.this,R.layout.item2, null);
holder.textView= (TextView) view.findViewById(R.id.t1);
holder.image3= (ImageView) view.findViewById(R.id.i3);
holder.image4= (ImageView) view.findViewById(R.id.i4);
holder.image5= (ImageView) view.findViewById(R.id.i5);
holder.image6= (ImageView) view.findViewById(R.id.i6);
break;
}
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
PicLun.ListBean bean =list.get(i);
holder.textView.setText(bean.getTitle());
switch(type) {
caseTYPE1:
String pic = bean.getPic();
String[] split = pic.split("\\|");
imageLoader.displayImage(split[0],holder.image1,options);
imageLoader.displayImage(split[1],holder.image2,options);
break;
caseTYPE2:
String pic2 = bean.getPic();
String[] split2 = pic2.split("\\|");
imageLoader.displayImage(split2[0],holder.image3,options);
imageLoader.displayImage(split2[1],holder.image4,options);
imageLoader.displayImage(split2[0],holder.image5,options);
imageLoader.displayImage(split2[1],holder.image6,options);
break;
}
returnview;
}
}
classViewHolder {
TextViewtextView;
ImageViewimage1,image2,image3,image4,image5,image6;
}