public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener {
private XListView mxListView;
private List<Data.DataBean> mList;
private int page = 1;
private MyAdapter myAdapter;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 1) {
if (page == 1)
mList.clear();
page++;
String s = msg.obj.toString();
Gson gson = new Gson();
Data data = gson.fromJson(s, Data.class);
mList.addAll(data.getData());
myAdapter.notifyDataSetChanged();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add();
loadDate();
}
private void loadDate() {
new Thread() {
@Override
public void run() {
String urlConnect = Net.getUrlConnect("http://ic.snssdk.com/2/article/v25/stream/?category=news_tech&count=20&bd_latitude=4.9E-324&bd_longitude=4.9E-324&bd_loc_time=1457695555&loc_mode=5&lac=4527&cid=28883&iid=3835029558&device_id=12211880440&ac=wifi&channel=baidu&aid=13&app_name=news_article&version_code=460&device_platform=android&device_type=iToolsAVM&os_");
if (urlConnect != null) {
Message message = Message.obtain();
message.obj = urlConnect;
handler.sendMessage(message);
}
}
}.start();
}
private void add() {
mList = new ArrayList<>();
mxListView = (XListView) findViewById(R.id.xlistv);
myAdapter = new MyAdapter();
mxListView.setPullLoadEnable(true);
mxListView.setXListViewListener(this);
mxListView.setAdapter(myAdapter);
}
@Override
public void onRefresh() {
page = 1;
loadDate();
stopLoad();
}
@Override
public void onLoadMore() {
loadDate();
stopLoad();
}
private void stopLoad() {
mxListView.stopRefresh();
mxListView.stopLoadMore();
mxListView.setRefreshTime("刚刚");
}
class MyAdapter extends BaseAdapter {
final int TYPE0 = 0;
final int TYPE1 = 1;
final int TYPE2 = 2;
ImageLoader imageLoader;
DisplayImageOptions options;
public MyAdapter() {
imageLoader = ImageLoader.getInstance();
ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(MainActivity.this);
imageLoader.init(configuration);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher).build();
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int i) {
return mList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public int getItemViewType(int position) {
switch (position) {
case 0:
return TYPE0;
case 1:
return TYPE1;
case 2:
return TYPE2;
default:
return TYPE0;
}
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Data.DataBean bean = mList.get(i);
List<Data.DataBean.ImageListBean> listimage = bean.getImage_list();
Log.e("Tag",listimage.toString());
int size = listimage != null ? bean.getImage_list().size() : 0;
int type = getItemViewType(size);
Log.e("sss",i+"/"+size+"/"+type);
ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
switch (type) {
case TYPE0:
view = View.inflate(MainActivity.this, R.layout.item1, null);
break;
case TYPE1:
view = View.inflate(MainActivity.this, R.layout.item2, null);
holder.imageView = (ImageView) view.findViewById(R.id.i1);
break;
case TYPE2:
view = View.inflate(MainActivity.this, R.layout.item3, null);
holder.imageView = (ImageView) view.findViewById(R.id.i1);
holder.imageView2 = (ImageView) view.findViewById(R.id.i2);
holder.imageView3 = (ImageView) view.findViewById(R.id.i3);
break;
}
holder.textView = (TextView) view.findViewById(R.id.t1);
holder.textView2 = (TextView) view.findViewById(R.id.t2);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.textView.setText(bean.getTitle() + "-" + size);
holder.textView2.setText(bean.getSource());
switch (type) {
case TYPE1:
imageLoader.displayImage(listimage.get(0).getUri(), holder.imageView, options);
break;
case TYPE2:
imageLoader.displayImage(listimage.get(0).getUri(), holder.imageView, options);
imageLoader.displayImage(listimage.get(1).getUri(), holder.imageView2, options);
imageLoader.displayImage(listimage.get(2).getUri(), holder.imageView3, options);
break;
}
return view;
}
class ViewHolder {
TextView textView, textView2;
ImageView imageView, imageView2, imageView3;
}
}
}
private XListView mxListView;
private List<Data.DataBean> mList;
private int page = 1;
private MyAdapter myAdapter;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 1) {
if (page == 1)
mList.clear();
page++;
String s = msg.obj.toString();
Gson gson = new Gson();
Data data = gson.fromJson(s, Data.class);
mList.addAll(data.getData());
myAdapter.notifyDataSetChanged();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add();
loadDate();
}
private void loadDate() {
new Thread() {
@Override
public void run() {
String urlConnect = Net.getUrlConnect("http://ic.snssdk.com/2/article/v25/stream/?category=news_tech&count=20&bd_latitude=4.9E-324&bd_longitude=4.9E-324&bd_loc_time=1457695555&loc_mode=5&lac=4527&cid=28883&iid=3835029558&device_id=12211880440&ac=wifi&channel=baidu&aid=13&app_name=news_article&version_code=460&device_platform=android&device_type=iToolsAVM&os_");
if (urlConnect != null) {
Message message = Message.obtain();
message.obj = urlConnect;
handler.sendMessage(message);
}
}
}.start();
}
private void add() {
mList = new ArrayList<>();
mxListView = (XListView) findViewById(R.id.xlistv);
myAdapter = new MyAdapter();
mxListView.setPullLoadEnable(true);
mxListView.setXListViewListener(this);
mxListView.setAdapter(myAdapter);
}
@Override
public void onRefresh() {
page = 1;
loadDate();
stopLoad();
}
@Override
public void onLoadMore() {
loadDate();
stopLoad();
}
private void stopLoad() {
mxListView.stopRefresh();
mxListView.stopLoadMore();
mxListView.setRefreshTime("刚刚");
}
class MyAdapter extends BaseAdapter {
final int TYPE0 = 0;
final int TYPE1 = 1;
final int TYPE2 = 2;
ImageLoader imageLoader;
DisplayImageOptions options;
public MyAdapter() {
imageLoader = ImageLoader.getInstance();
ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(MainActivity.this);
imageLoader.init(configuration);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher).build();
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int i) {
return mList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public int getItemViewType(int position) {
switch (position) {
case 0:
return TYPE0;
case 1:
return TYPE1;
case 2:
return TYPE2;
default:
return TYPE0;
}
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Data.DataBean bean = mList.get(i);
List<Data.DataBean.ImageListBean> listimage = bean.getImage_list();
Log.e("Tag",listimage.toString());
int size = listimage != null ? bean.getImage_list().size() : 0;
int type = getItemViewType(size);
Log.e("sss",i+"/"+size+"/"+type);
ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
switch (type) {
case TYPE0:
view = View.inflate(MainActivity.this, R.layout.item1, null);
break;
case TYPE1:
view = View.inflate(MainActivity.this, R.layout.item2, null);
holder.imageView = (ImageView) view.findViewById(R.id.i1);
break;
case TYPE2:
view = View.inflate(MainActivity.this, R.layout.item3, null);
holder.imageView = (ImageView) view.findViewById(R.id.i1);
holder.imageView2 = (ImageView) view.findViewById(R.id.i2);
holder.imageView3 = (ImageView) view.findViewById(R.id.i3);
break;
}
holder.textView = (TextView) view.findViewById(R.id.t1);
holder.textView2 = (TextView) view.findViewById(R.id.t2);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.textView.setText(bean.getTitle() + "-" + size);
holder.textView2.setText(bean.getSource());
switch (type) {
case TYPE1:
imageLoader.displayImage(listimage.get(0).getUri(), holder.imageView, options);
break;
case TYPE2:
imageLoader.displayImage(listimage.get(0).getUri(), holder.imageView, options);
imageLoader.displayImage(listimage.get(1).getUri(), holder.imageView2, options);
imageLoader.displayImage(listimage.get(2).getUri(), holder.imageView3, options);
break;
}
return view;
}
class ViewHolder {
TextView textView, textView2;
ImageView imageView, imageView2, imageView3;
}
}
}