http://blog.csdn.net/libaineu2004/article/details/26582383
http://blog.csdn.net/android_my/article/details/51534853
http://www.jianshu.com/p/9c40c0968f4b
http://blog.csdn.net/afanbaby/article/details/55804850
自定义view包,将XListView,XListViewFooter,XListViewHeader
这三个类导入工程里
并加载布局文件:
xlistview_footer.xml,xlistview_header.xml
和图片:xlistview_arrow.png
以及strings.xml 配置
最后 改包名和R文件(Eclipse 里用 ctrl+shift+o)
MainActivity
package com.example.xlistview_test;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
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.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import bean.Max;
import xlistview_view.XListView;
public class MainActivity extends AppCompatActivity {
XListView xlv;
int currenti=1;
boolean flag=true;
List<Max.NewslistBean> list=new ArrayList<Max.NewslistBean>();
MyAdapter myAdapter;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageLoaderConfiguration config = ImageLoaderConfiguration.createDefault(this);
ImageLoader.getInstance().init(config);
xlv=(XListView) findViewById(R.id.xlv);
initData();
myAdapter = new MyAdapter();
xlv.setAdapter(myAdapter);
xlv.setPullRefreshEnable(true);
xlv.setPullLoadEnable(true);
xlv.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
flag=false;
currenti=1;
initData();
xlv.stopRefresh();
}
@Override
public void onLoadMore() {
flag=true;
currenti++;
initData();
xlv.stopLoadMore();
}
});
}
private void initData() {
new AsyncTask<String,String,String>(){
@Override
protected String doInBackground(String... strings) {
getData();
return null;
}
@Override
protected void onPostExecute(String s) {
myAdapter.notifyDataSetChanged();
super.onPostExecute(s);
}
}.execute();
}
private void getData() {
try {
URL url = new URL("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=" + currenti);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if(code==200){
Gson gson = new Gson();
Max m = gson.fromJson(new InputStreamReader(conn.getInputStream(), "UTF-8"), Max.class);
if(flag){
list.addAll(m.getNewslist());
}else{
list=m.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 i;
}
@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=view.findViewById(R.id.tv);
holder.iv=view.findViewById(R.id.iv);
view.setTag(holder);
}else{
holder=(ViewHolder) view.getTag();
}
holder.tv.setText(list.get(i).getTitle());
ImageLoader.getInstance().displayImage(list.get(i).getPicUrl(),holder.iv);
return view;
}
}
class ViewHolder{
TextView tv;
ImageView iv;
}
}
activity_main.xml 布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.xlistview_test.MainActivity">
<xlistview_view.XListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/xlv"
></xlistview_view.XListView>
</RelativeLayout>
item.xml 布局(xlistview 加载的布局)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:id="@+id/tv"
/>
</LinearLayout>