public classMainActivityextendsAppCompatActivityimplementsXListView.IXListViewListener {
privateBannerbanner;
privateListlist;
privateListimagelist;
privateXListViewxListView;
private intpage=1;
privateListbeanList;
privateMyAdaptermyAdapter;
privateHandlerhandler=newHandler() {
@Override
public voidhandleMessage(Message msg) {
String s = msg.obj.toString();
Gson gson =newGson();
Pic pic = gson.fromJson(s,Pic.class);
List been = pic.getData();
imagelist=newArrayList<>();
for(Pic.DataBean bean : been) {
imagelist.add(bean.getImgSrc());
}
banner.setImages(imagelist);
banner.start();
}
};
privateHandlerxhandle=newHandler() {
@Override
public voidhandleMessage(Message msg) {
String s = msg.obj.toString();
Gson gson =newGson();
News news = gson.fromJson(s,News.class);
beanList.addAll(news.getData().getArticleList());
myAdapter.notifyDataSetChanged();
stopXlist();
}
private voidstopXlist() {
xListView.stopLoadMore();
xListView.stopRefresh();
xListView.setRefreshTime("刚刚");
}
};
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add();
loadDate();
banner.setOnBannerListener(newOnBannerListener() {
@Override
public voidOnBannerClick(intposition) {
Intent intent =newIntent(MainActivity.this,Main2Activity.class);
intent.putExtra("image",imagelist.get(position %imagelist.size()));
intent.cloneFilter();
startActivity(intent);
}
});
newThread() {
@Override
public voidrun() {
String urlConnect = Net.getUrlConnect("http://zkread.com/htnewsroom/articles/tops");
Message message = Message.obtain();
message.obj= urlConnect;
handler.sendMessage(message);
}
}.start();
}
private voidloadDate() {
newThread() {
@Override
public voidrun() {
String urlConnect = Net.getUrlConnect("http://zkread.com/htnewsroom/v2.0/mobileapp/user-0/categories/articles?itemType=recommendation&itemName=推荐&itemId=-1&size=20&firstId=1339380&page="+page);
Message message = Message.obtain();
message.obj= urlConnect;
xhandle.sendMessage(message);
}
}.start();
}
private voidadd() {
xListView= (XListView) findViewById(R.id.xlist);
beanList=newArrayList<>();
list=newArrayList<>();
xListView.setPullLoadEnable(true);
xListView.setXListViewListener(this);
myAdapter=newMyAdapter();
xListView.setAdapter(myAdapter);
banner= (Banner) findViewById(R.id.ban);
banner.setImageLoader(newImagemap(this));
banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
banner.setIndicatorGravity(BannerConfig.RIGHT);
}
@Override
public voidonRefresh() {
page++;
beanList.clear();
loadDate();
}
@Override
public voidonLoadMore() {
page++;
loadDate();
}
classMyAdapterextendsBaseAdapter {
final intTYPE1=0;
final intTYPE2=1;
@Override
public intgetCount() {
returnbeanList.size();
}
@Override
publicObjectgetItem(inti) {
returnbeanList.get(i);
}
@Override
public longgetItemId(inti) {
returni;
}
@Override
public intgetItemViewType(intposition) {
returnbeanList.get(position).getImgSrc().startsWith("http") ?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(MainActivity.this,R.layout.item, null);
holder.textView= (TextView) view.findViewById(R.id.t1);
holder.imageView= (ImageView) view.findViewById(R.id.i1);
break;
caseTYPE2:
view = View.inflate(MainActivity.this,R.layout.item2, null);
holder.textView= (TextView) view.findViewById(R.id.t1);
holder.textView2= (TextView) view.findViewById(R.id.t2);
holder.textView3= (TextView) view.findViewById(R.id.t3);
break;
}
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
News.DataBean.ArticleListBean bean=beanList.get(i);
holder.textView.setText(beanList.get(i).getTitle());
switch(type) {
caseTYPE1:
Glide.with(MainActivity.this).load(beanList.get(i).getImgSrc()).into(holder.imageView);
break;
caseTYPE2:
holder.textView2.setText(beanList.get(i).getDescription());
holder.textView3.setText(longData(bean.getCreateTime()));
break;
}
returnview;
}
}
classViewHolder {
TextViewtextView,textView2,textView3;
ImageViewimageView;
}
publicStringlongData(longtime) {
Date date=newDate(time);
SimpleDateFormat dateFormat =newSimpleDateFormat("HH:mm");
returndateFormat.format(date);
}
}
网友评论