package com.qianqian.xlistview;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.qianqian.xlistview.bean.Data;
import com.qianqian.xlistview.view.xlist.XListView;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
//声明变量
XListView xlv;
List<Data.NewslistBean> list=new ArrayList<Data.NewslistBean>();;
int current=1;
MyAdapter adapter;
boolean flag=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
xlv= (XListView) findViewById(R.id.xlv);
//初始化数据
initData();
//设置适配器
adapter = new MyAdapter();
//绑定
xlv.setAdapter(adapter);
//刷新
xlv.setPullRefreshEnable(true);
//加载
xlv.setPullLoadEnable(true);
//时间
xlv.setRefreshTime("2017-10-18");
//监听
xlv.setXListViewListener(new XListView.IXListViewListener() {
//刷新
@Override
public void onRefresh() {
flag=false;
current=1;
initData();
xlv.stopRefresh();
}
//加载
@Override
public void onLoadMore() {
flag=true;
current++;
initData();
xlv.stopLoadMore();
}
});
}
//初始化数据
private void initData() {
new AsyncTask<String, String, String>() {
@Override
protected String doInBackground(String... parmas) {
//解析
getData();
return null;
}
@Override
protected void onPostExecute(String result) {
//刷新
adapter.notifyDataSetChanged();
super.onPostExecute(result);
}
}.execute();
}
//解析的方法
private void getData() {
//路径
String path="https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page="+current;
try {
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
int code = conn.getResponseCode();
if (code==200) {
//gson解析
Gson gson=new Gson();
Data data = gson.fromJson(new InputStreamReader(conn.getInputStream()), Data.class);
// list=data.getNewslist();
if (flag) {
list.addAll(data.getNewslist());
} else {
list=data.getNewslist();
}
// list.addAll(data.getNewslist());
}
} catch (Exception e) {
e.printStackTrace();
}
}
//适配器的方法
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
if (list!=null){
return list.size();
}
return 0;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
//优化
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view==null) {
//加载布局
view=View.inflate(MainActivity.this, R.layout.item, null);
holder=new ViewHolder();
//扎控件
holder.tv_title =(TextView) view.findViewById(R.id.tv_title);
holder.tv_desc =(TextView) view.findViewById(R.id.tv_desc);
holder.tv_time =(TextView) view.findViewById(R.id.tv_time);
holder.iv =(ImageView) view.findViewById(R.id.iv);
view.setTag(holder);
}else {
holder=(ViewHolder) view.getTag();
}
//赋值
holder.tv_title.setText(list.get(i).getTitle());
holder.tv_desc.setText(list.get(i).getDescription());
holder.tv_time.setText(list.get(i).getCtime());
ImageLoader.getInstance().displayImage(list.get(i).getPicUrl(), holder.iv);
return view;
}
}
static class ViewHolder{
TextView tv_title,tv_desc,tv_time;
ImageView iv;
}
}
//图片
package com.qianqian.xlistview.bean;
import android.app.Application;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.qianqian.xlistview.R;
public class Myapction extends Application{
@Override
public void onCreate() {
//初始化一个默认图片的配置
DisplayImageOptions options=new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher)
//渐显
.displayer(new FadeInBitmapDisplayer(3000)).cacheOnDisk(true)
.cacheInMemory(true)
.build();
ImageLoaderConfiguration config=new ImageLoaderConfiguration.Builder(this)
.threadPoolSize(4)
.defaultDisplayImageOptions(options)
.build();
//初始化
ImageLoader.getInstance().init(config);
super.onCreate();
}
}
//xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/iv"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="标题" />
<TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="2"
android:text="描述" />
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:singleLine="true"
android:text="时间" />
</LinearLayout>
<ImageView
android:id="@+id/iv"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true" />
</RelativeLayout>