主页面
package com.example.app2;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import com.bwie.xlistview.XListView;
import com.example.app2.Adapter.BasesAdapter;
import com.example.app2.Bean.News;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private XListView xListView;
int currentPage = 1;
private BasesAdapter basesAdapter;
private List<News.DataBeanX.DataBean> list;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xListView = findViewById(R.id.x_lv);
list = new ArrayList<>();
basesAdapter = new BasesAdapter(this,list);
xListView.setAdapter(basesAdapter);
xListView.setPullLoadEnable(true);
xListView.setPullRefreshEnable(true);
xListView.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
currentPage = 1;
getDate(currentPage,false);
}
@Override
public void onLoadMore() {
currentPage ++;
getDate(currentPage,true);
}
});
getDate(1,false);
}
private void getDate(int i, final boolean b) {
new AsyncTask<String, Integer, String>() {
@Override
protected String doInBackground(String... strings) {
String result = null;
try {
result = HttpUtils.getHttpFrom(strings[0]);
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (!TextUtils.isEmpty(s)){
Gson gson = new Gson();
News news = gson.fromJson(s,News.class);
if (news.getData() != null){
if (!b){
list.clear();
}
list.addAll(news.getData().getData());
basesAdapter.notifyDataSetChanged();
}
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (b){
xListView.stopLoadMore();;
}else{
xListView.stopRefresh();
xListView.setRefreshTime("刚刚");
}
}
},2000);
}
}
}.execute("http://365jia.cn/news/api3/365jia/news/headline?page="+currentPage);
}
}
图片加载
package com.example.app2;
import android.app.Application;
import android.graphics.Bitmap;
import android.os.Environment;
import android.view.Display;
import com.nostra13.universalimageloader.cache.disc.DiskCache;
import com.nostra13.universalimageloader.cache.disc.impl.ext.LruDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import java.io.File;
import java.io.IOException;
/**
* Created by 红鼻子小黑 on 2018/9/8.
*/
public class BaseApption extends Application{
private File file;
private DiskCache diskCache;
@Override
public void onCreate() {
super.onCreate();
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File rootSD = Environment.getExternalStorageDirectory();
file = new File(rootSD,"pic");
if (!file.exists()){
file.mkdirs();
}
}
try {
diskCache = new LruDiskCache(file,new Md5FileNameGenerator(),50*1024*1024);
} catch (IOException e) {
e.printStackTrace();
}
BitmapDisplayer displayer = new RoundedBitmapDisplayer(10);
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheOnDisk(true)
.cacheInMemory(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.displayer(displayer)
.showImageOnFail(R.mipmap.ic_launcher)
.showImageOnLoading(R.mipmap.ic_launcher)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.build();
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
.diskCache(diskCache)
.memoryCache(new LruMemoryCache(12*1024*1024))
.threadPoolSize(3)
.defaultDisplayImageOptions(options)
.build();
ImageLoader .getInstance().init(configuration);
}
}
package com.example.app2;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by 红鼻子小黑 on 2018/9/8.
*/
public class HttpUtils {
public static String getHttpFrom(String urlstring) throws IOException {
String result = "";
URL url = new URL(urlstring);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setUseCaches(false);
connection.setConnectTimeout(10*1000);
connection.setRequestMethod("GET");
int code = connection.getResponseCode();
if (code == 200){
InputStream is = connection.getInputStream();
result = getHttpFromInputStream(is);
}
connection.connect();
return result;
}
private static String getHttpFromInputStream(InputStream is) throws IOException {
String result = "";
int length = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
while((length = is.read(buffer,0,buffer.length))!=-1){
baos.write(buffer,0,length);
baos.flush();
}
result = baos.toString();
baos.close();
is.close();
return result;
}
}
package com.example.app2.Adapter;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.app2.Bean.News;
import com.example.app2.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
/**
* Created by 红鼻子小黑 on 2018/9/8.
*/
public class BasesAdapter extends BaseAdapter{
private Context context;
private List<News.DataBeanX.DataBean> list;
private static final int TYPE_ONE_PIC = 0;
private static final int TYPE_TWO_PIC = 1;
public BasesAdapter(Context context, List<News.DataBeanX.DataBean> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int posotion) {
return list.get(posotion);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
News.DataBeanX.DataBean dataBean = list.get(position);
if (dataBean.getType().equals("images")){
return TYPE_TWO_PIC;
}else{
return TYPE_ONE_PIC;
}
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder1 holder1 = null;
ViewHolder2 holder2 = null;
int type = getItemViewType(i);
switch (type){
case TYPE_ONE_PIC:
if (view == null){
holder1 = new ViewHolder1();
view = View.inflate(context, R.layout.item_list1,null);
holder1.Title = view.findViewById(R.id.txt_title);
holder1.pl = view.findViewById(R.id.txt_pl);
holder1.yd = view.findViewById( R.id.txt_yd);
holder1.Imag = view.findViewById(R.id.img);
view.setTag(holder1);
}else{
holder1 = (ViewHolder1) view.getTag();
}
holder1.Title.setText(list.get(i).getTitle());
ImageLoader.getInstance().displayImage("http://365jia.cn/uploads/"+list.get(i).getPics().get(0),holder1.Imag);
break;
case TYPE_TWO_PIC:
if (view == null){
holder2 = new ViewHolder2();
view = View.inflate(context,R.layout.item_list2,null);
holder2.Title = view.findViewById(R.id.txt_title);
holder2.pl = view.findViewById(R.id.txt_pl);
holder2.yd = view.findViewById( R.id.txt_yd);
holder2.Imag1=view.findViewById(R.id.img1);
holder2.Imag2=view.findViewById(R.id.img2);
holder2.Imag3=view.findViewById(R.id.img3);
view.setTag(holder2);
}else{
holder2 = (ViewHolder2) view.getTag();
}
holder2.Title.setText(list.get(i).getTitle());
ImageLoader.getInstance().displayImage("http://365jia.cn/uploads/"+list.get(i).getPics().get(0),holder2.Imag1);
ImageLoader.getInstance().displayImage("http://365jia.cn/uploads/"+list.get(i).getPics().get(1),holder2.Imag2);
ImageLoader.getInstance().displayImage("http://365jia.cn/uploads/"+list.get(i).getPics().get(2),holder2.Imag3);
break;
}
return view;
}
class ViewHolder1{
TextView Title;
TextView yd;
TextView pl;
ImageView Imag;
}
class ViewHolder2{
TextView Title;
TextView yd;
TextView pl;
ImageView Imag1;
ImageView Imag2;
ImageView Imag3;
}
}