第一步
1)main的布局
<LinearLayout 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"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#33000000"
android:layout_alignParentBottom="true"
android:textColor="#fff"
android:padding="2dp"/>
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
</RadioGroup>
</RelativeLayout>
<com.bwie.yuekaotwo.view.XListView
android:id="@+id/xlistview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
2)item1.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical" >
<ImageView
android:id="@+id/item1_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/item1_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题" />
<TextView
android:id="@+id/item1_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="内容" />
</LinearLayout>
</LinearLayout>
3)item2.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/item2_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/item2_image1"
android:layout_width="100dp"
android:layout_height="100dp"
/>
<ImageView
android:id="@+id/item2_image2"
android:layout_width="100dp"
android:layout_height="100dp"
/>
</LinearLayout>
</LinearLayout>
4)list_item.xml布局
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/list_item_textview"
android:textSize="16sp"
android:textColor="#000"
android:padding="5dp">
</TextView>
5)xlistview_footer.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/xlistview_footer_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp" >
<ProgressBar
android:id="@+id/xlistview_footer_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="invisible" />
<TextView
android:id="@+id/xlistview_footer_hint_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/xlistview_footer_hint_normal" />
</RelativeLayout>
</LinearLayout>
6)xlistview_header.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom" >
<RelativeLayout
android:id="@+id/xlistview_header_content"
android:layout_width="fill_parent"
android:layout_height="60dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="vertical"
android:id="@+id/xlistview_header_text">
<TextView
android:id="@+id/xlistview_header_hint_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/xlistview_header_hint_normal" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/xlistview_header_last_time"
android:textSize="12sp" />
<TextView
android:id="@+id/xlistview_header_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/xlistview_header_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/xlistview_header_text"
android:layout_centerVertical="true"
android:layout_marginLeft="-35dp"
android:src="@drawable/xlistview_arrow" />
<ProgressBar
android:id="@+id/xlistview_header_progressbar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignLeft="@id/xlistview_header_text"
android:layout_centerVertical="true"
android:layout_marginLeft="-40dp"
android:visibility="invisible" />
</RelativeLayout>
</LinearLayout>
7)动画布局,创建一个anim文件
anim_item布局
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="100.0%p"
android:toXDelta="0"
android:fromYDelta="0"
android:toYDelta="0"
android:duration="2000">
</translate>
8)图片轮播所用到的小圆点
radio_button.xml布局
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true" android:drawable="@drawable/radio_checked"></item>
<item android:state_enabled="false" android:drawable="@drawable/radio_unchecked"></item>
</selector>
</translate>
9)radio_cheched.xml布局
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size android:width="5dp" android:height="5dp"/>
<solid android:color="#f00"/>
</shape>
10)radio_unchecked.xml布局
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size android:width="5dp" android:height="5dp"/>
<solid android:color="#33000000"/>
</shape>
11)string.xml布局进行复制
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">YueKaoTwo</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="xlistview_header_hint_normal">下拉刷新</string>
<string name="xlistview_header_hint_ready">松开刷新数据</string>
<string name="xlistview_header_hint_loading">正在加载...</string>
<string name="xlistview_header_last_time">上次更新时间:</string>
<string name="xlistview_footer_hint_normal">查看更多</string>
<string name="xlistview_footer_hint_ready">松开载入更多</string>
</resources>
第二步.导入jar包
1)xUtils包,gson包,imageLoader包
第三步.MAinActivity
package com.bwie.yuekaotwo;
import java.io.IOException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.bwie.yuekaotwo.adapter.MyListAdapter;
import com.bwie.yuekaotwo.adapter.VpAdapter;
import com.bwie.yuekaotwo.bean.Ads;
import com.bwie.yuekaotwo.bean.Ads.Ads2;
import com.bwie.yuekaotwo.bean.Datas;
import com.bwie.yuekaotwo.view.XListView;
import com.bwie.yuekaotwo.view.XListView.IXListViewListener;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.Color;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity implements IXListViewListener {
private ViewPager mVp;
private XListView mXl;
private RadioGroup mRg;
private int lastPostion = 0;
private TextView mTvTitle;
private List<Ads> data;
VpAdapter adapter2 ;
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 0:
@SuppressWarnings("unchecked")
List<Ads> list = (List<Ads>) msg.obj;
Ads ads = list.get(0);
List<Ads2> list2 = ads.getAds();
if (adapter2==null) {
adapter2 = new VpAdapter(MainActivity.this, list2);
mVp.setAdapter(adapter2);
}
setRadioButtonCircle(list2);
initEvent(list2);
mTvTitle.setText(list2.get(lastPostion).getTitle());
// 发送延迟消息
sendMessageDelaye();
// 设置listview显示的数据
setListViewData(list);
break;
case 1:
sendMessageDelaye();
break;
}
}
};
private MyListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到控件的id
initView();
// 获取数据
getData();
//设置listview刷新和加载的监听
mXl.setXListViewListener(MainActivity.this);
}
/**
* 设置listview显示的数据
*
* @param list
*/
private void setListViewData(List<Ads> list) {
//设置适配器
adapter = new MyListAdapter(MainActivity.this, list);
//设置动画
Animation loadAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim_item);
//创建LayoutAnimationController对象
LayoutAnimationController controller=new LayoutAnimationController(loadAnimation);
mXl.setLayoutAnimation(controller);
//设置适配器
mXl.setAdapter(adapter);
}
/**
* 设置数据
* 显示小圆点的方法
* @param list2
*/
private void setRadioButtonCircle(List<Ads2> list2) {
for (int i = 0; i < list2.size(); i++) {
// 创建radiobutton对象
RadioButton rb = new RadioButton(MainActivity.this);
rb.setId(i);
if (i == 0) {
rb.setEnabled(true);
} else {
rb.setEnabled(false);
}
int wrapContent = RelativeLayout.LayoutParams.WRAP_CONTENT;
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
wrapContent, wrapContent);
params.rightMargin = 15;
rb.setLayoutParams(params);
rb.setButtonDrawable(R.drawable.radio_button);
mRg.addView(rb);
mRg.check(0);
}
};
/**
* 设置触发的监听
*
* @param list
*/
private void initEvent(final List<Ads2> list) {
mVp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
int i = arg0 % list.size();
//当前选中的小圆点
mRg.getChildAt(i).setEnabled(true);
//未选中的小圆点
mRg.getChildAt(lastPostion).setEnabled(false);
lastPostion = i;
//设置title标题
mTvTitle.setText(list.get(i).getTitle());
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
/**
* 获取数据的方法
*/
private void getData() {
final String path = "http://c.m.163.com/nc/article/headline/T1348647909107/0-20.html";
new Thread() {
public void run() {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(path);
try {
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity();
String string = EntityUtils.toString(httpEntity);
Datas datas = jsonObj(string);
List<Ads> list = datas.getT1348647909107();
Message message = Message.obtain();
data = list;
message.obj = data;
message.what = 0;
handler.sendMessage(message);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}
/**
* 解析json数据的方法
*
* @param string
*/
private Datas jsonObj(String string) {
Gson gson = new Gson();
Datas datas = gson.fromJson(string, Datas.class);
return datas;
};
/**
* 初始化控件的视图
*/
private void initView() {
mVp = (ViewPager) findViewById(R.id.viewpager);
mXl = (XListView) findViewById(R.id.xlistview);
mRg = (RadioGroup) findViewById(R.id.radiogroup);
mTvTitle = (TextView) findViewById(R.id.tv_title);
mXl.setPullLoadEnable(true);
mXl.setPullRefreshEnable(true);
}
/**
* 实现无限轮博
*/
protected void sendMessageDelaye() {
int currentItem = mVp.getCurrentItem();
currentItem++;
mVp.setCurrentItem(currentItem);
// 延时两秒去发送一个消息
handler.sendEmptyMessageDelayed(1, 2000);
}
/**
* 刷新数据的方法
*/
@Override
public void onRefresh() {
new Thread() {
public void run() {
// 获取数据
getData();
};
}.start();
close();
}
/**
* 关闭的方法
*/
private void close() {
mXl.stopLoadMore();
mXl.stopRefresh();
}
/**
* 加载数据的方法
*/
@Override
public void onLoadMore() {
//加载数据的方法
getDataFlush();
//进行适配器刷新的方法
adapter.notifyDataSetChanged();
//关闭
close();
}
/**
* 上拉加载时请求的数据
*/
private void getDataFlush() {
final String path = "http://c.m.163.com/nc/article/headline/T1348647909107/10-20.html";
new Thread() {
public void run() {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(path);
try {
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity();
String string = EntityUtils.toString(httpEntity);
Datas datas = jsonObj(string);
List<Ads> list = datas.getT1348647909107();
data.addAll(list);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}
}
第四步.适配器
1)MyListAdapter适配器
package com.bwie.yuekaotwo.adapter;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bwie.yuekaotwo.R;
import com.bwie.yuekaotwo.bean.Ads;
import com.bwie.yuekaotwo.bean.Imgextra;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
public class MyListAdapter extends BaseAdapter {
Context context;
List<Ads> list;
public MyListAdapter(Context context, List<Ads> list) {
this.context=context;
this.list=list;
}
@Override
public int getCount() {
return list!=null?list.size():0;
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getItemViewType(int position) {
if (list!=null) {
Ads ads = list.get(position);
if (ads.getImgextra()!=null) {
return 0;
}
else if(ads.getImgsrc()!=null){
return 1;
}
}
return 3;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
View view = null;
ViewHolder1 holder1;
ViewHolder2 holder2;
Ads ads = list.get(position);
if (null==convertView) {
switch (type) {
case 0:
view = View.inflate(context, R.layout.item2, null);
holder1=new ViewHolder1();
holder1.title=(TextView) view.findViewById(R.id.item2_title);
holder1.image1=(ImageView) view.findViewById(R.id.item2_image1);
holder1.image2=(ImageView) view.findViewById(R.id.item2_image2);
holder1.title.setText(ads.getTitle());
Imgextra imgextra = ads.getImgextra().get(0);
Imgextra imgextra1 = ads.getImgextra().get(1);
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
.cacheOnDisk(true).build();
ImageLoader.getInstance().displayImage(imgextra.getImgsrc(), holder1.image1, options);
ImageLoader.getInstance().displayImage(imgextra1.getImgsrc(), holder1.image2, options);
view.setTag(holder1);
break;
case 1:
view=View.inflate(context, R.layout.item1, null);
holder2=new ViewHolder2();
holder2.image1=(ImageView) view.findViewById(R.id.item1_image);
holder2.title=(TextView) view.findViewById(R.id.item1_title);
holder2.content=(TextView) view.findViewById(R.id.item1_content);
DisplayImageOptions options1 = new DisplayImageOptions.Builder()
.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
.cacheOnDisk(true).build();
ImageLoader.getInstance().displayImage(ads.getImgsrc(), holder2.image1, options1);
holder2.title.setText(ads.getTitle());
holder2.content.setText(ads.getDigest());
view.setTag(holder2);
break;
}
}
else{
view=convertView;
switch (type) {
case 0:
holder1=(ViewHolder1) view.getTag();
holder1.title.setText(ads.getTitle());
Imgextra imgextra = ads.getImgextra().get(0);
Imgextra imgextra1 = ads.getImgextra().get(1);
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
.cacheOnDisk(true).build();
ImageLoader.getInstance().displayImage(imgextra.getImgsrc(), holder1.image1, options);
ImageLoader.getInstance().displayImage(imgextra1.getImgsrc(), holder1.image2, options);
break;
case 1:
holder2=(ViewHolder2) view.getTag();
DisplayImageOptions options1 = new DisplayImageOptions.Builder()
.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
.cacheOnDisk(true).build();
ImageLoader.getInstance().displayImage(ads.getImgsrc(), holder2.image1, options1);
holder2.title.setText(ads.getTitle());
holder2.content.setText(ads.getDigest());
break;
}
}
return view;
}
class ViewHolder1{
TextView title;
ImageView image1;
ImageView image2;
}
class ViewHolder2{
TextView title;
ImageView image1;
TextView content;
}
}
---------------------------------------------------------------------------------------------
2)VpAdapter适配器
package com.bwie.yuekaotwo.adapter;
import java.util.List;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import com.bwie.yuekaotwo.R;
import com.bwie.yuekaotwo.bean.Ads.Ads2;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
public class VpAdapter extends PagerAdapter {
List<Ads2> list;
Context context;
public VpAdapter(Context context, List<Ads2> list) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int index = position % list.size();
Ads2 ads2 = list.get(index);
Log.e("tag", "==="+index+"====");
// DisplayImageOptions options = new DisplayImageOptions.Builder()
// .showImageOnLoading() //加载图片时的图片
// .showImageForEmptyUri(R.drawable.ic_empty) //没有图片资源时的默认图片
// .showImageOnFail(R.drawable.ic_error) //加载失败时的图片
// .cacheInMemory(true) //启用内存缓存
// .cacheOnDisk(true) //启用外存缓存
// .considerExifParams(true) //启用EXIF和JPEG图像格式
// .displayer(new RoundedBitmapDisplayer(20)) //设置显示风格这里是圆角矩形
// .build();
imageView.setScaleType(ScaleType.FIT_XY);
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
.cacheOnDisk(true).build();
ImageLoader.getInstance().displayImage(ads2.getImgsrc(), imageView,
options);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
-------------------------------------------------------------------------------
第五步.创建MyApplication类,继承Application
package com.bwie.yuekaotwo.application;
import java.io.File;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;
import android.app.Application;
public class MyApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
File cacheDir =StorageUtils.getOwnCacheDirectory(this, "imageloader/Cache");
ImageLoaderConfiguration configuration= new ImageLoaderConfiguration
.Builder(this)
.memoryCacheExtraOptions(480, 800) // maxwidth, max height,即保存的每个缓存文件的最大长宽
.threadPoolSize(3)//线程池内加载的数量
.threadPriority(Thread.NORM_PRIORITY -2)
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new UsingFreqLimitedMemoryCache(2* 1024 * 1024)) // You can pass your own memory
cache implementation/你可以通过自己的内存缓存实现
.memoryCacheSize(2 * 1024 * 1024)
.discCacheSize(50 * 1024 * 1024)
.tasksProcessingOrder(QueueProcessingType.LIFO)
.discCacheFileCount(100) //缓存的文件数量
.discCache(new UnlimitedDiscCache(cacheDir))//自定义缓存路径
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.imageDownloader(new BaseImageDownloader(this,5 * 1000, 30 * 1000)) // connectTimeout (5 s),
readTimeout (30 s)超时时间
.writeDebugLogs() // Remove for releaseapp
.build();//开始构建
ImageLoader.getInstance().init(configuration);
}
}
----------------------------------------------------------
第六步.创建一个bean包,进行gson解析使用
1)Ads类
package com.bwie.yuekaotwo.bean;
import java.util.List;
public class Ads {
private List<Ads2> ads;
private String digest;
private String title;
private List<Imgextra> imgextra;
private String imgsrc;
public List<Imgextra> getImgextra() {
return imgextra;
}
public void setImgextra(List<Imgextra> imgextra) {
this.imgextra = imgextra;
}
@Override
public String toString() {
return "Ads [ads=" + ads + ", digest=" + digest + ", title=" + title
+ ", imgextra=" + imgextra + ", imgsrc=" + imgsrc + "]";
}
public String getImgsrc() {
return imgsrc;
}
public void setImgsrc(String imgsrc) {
this.imgsrc = imgsrc;
}
public List<Ads2> getAds() {
return ads;
}
public void setAds(List<Ads2> ads) {
this.ads = ads;
}
public String getDigest() {
return digest;
}
public void setDigest(String digest) {
this.digest = digest;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public class Ads2{
private String imgsrc;
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImgsrc() {
return imgsrc;
}
public void setImgsrc(String imgsrc) {
this.imgsrc = imgsrc;
}
@Override
public String toString() {
return "Ads2 [imgsrc=" + imgsrc + ", title=" + title + "]";
}
}
}
2)Datas类
package com.bwie.yuekaotwo.bean;
import java.util.List;
public class Datas {
private List<Ads> T1348647909107;
public List<Ads> getT1348647909107() {
return T1348647909107;
}
public void setT1348647909107(List<Ads> t1348647909107) {
T1348647909107 = t1348647909107;
}
@Override
public String toString() {
return "Datas [T1348647909107=" + T1348647909107 + "]";
}
}
3)Imgextra类
package com.bwie.yuekaotwo.bean;
public class Imgextra {
private String imgsrc;
public String getImgsrc() {
return imgsrc;
}
public void setImgsrc(String imgsrc) {
this.imgsrc = imgsrc;
}
@Override
public String toString() {
return "Imgextra [imgsrc=" + imgsrc + "]";
}
}
----------------------------------------------------------------------------------------------
第七步.复制xlistview的代码
1)XListView
/**
* @file XListView.java
* @package me.maxwin.view
* @create Mar 18, 2012 6:28:41 PM
* @author Maxwin
* @description An ListView support (a) Pull down to refresh, (b) Pull up to load more.
* Implement IXListViewListener, and see stopRefresh() / stopLoadMore().
*/
package com.bwie.yuekaotwo.view;
import com.bwie.yuekaotwo.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.DecelerateInterpolator;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Scroller;
import android.widget.TextView;
public class XListView extends ListView implements OnScrollListener {
private float mLastY = -1; // save event y
private Scroller mScroller; // used for scroll back
private OnScrollListener mScrollListener; // user's scroll listener
// the interface to trigger refresh and load more.
private IXListViewListener mListViewListener;
// -- header view
private XListViewHeader mHeaderView;
// header view content, use it to calculate the Header's height. And hide it
// when disable pull refresh.
private RelativeLayout mHeaderViewContent;
private TextView mHeaderTimeView;
private int mHeaderViewHeight; // header view's height
private boolean mEnablePullRefresh = true;
private boolean mPullRefreshing = false; // is refreashing.
// -- footer view
private XListViewFooter mFooterView;
private boolean mEnablePullLoad;
private boolean mPullLoading;
private boolean mIsFooterReady = false;
// total list items, used to detect is at the bottom of listview.
private int mTotalItemCount;
// for mScroller, scroll back from header or footer.
private int mScrollBack;
private final static int SCROLLBACK_HEADER = 0;
private final static int SCROLLBACK_FOOTER = 1;
private final static int SCROLL_DURATION = 400; // scroll back duration
private final static int PULL_LOAD_MORE_DELTA = 50; // when pull up >= 50px
// at bottom, trigger
// load more.
private final static float OFFSET_RADIO = 1.8f; // support iOS like pull
// feature.
/**
* @param context
*/
public XListView(Context context) {
super(context);
initWithContext(context);
}
public XListView(Context context, AttributeSet attrs) {
super(context, attrs);
initWithContext(context);
}
public XListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initWithContext(context);
}
private void initWithContext(Context context) {
mScroller = new Scroller(context, new DecelerateInterpolator());
// XListView need the scroll event, and it will dispatch the event to
// user's listener (as a proxy).
super.setOnScrollListener(this);
// init header view
mHeaderView = new XListViewHeader(context);
mHeaderViewContent = (RelativeLayout) mHeaderView.findViewById(R.id.xlistview_header_content);
mHeaderTimeView = (TextView) mHeaderView.findViewById(R.id.xlistview_header_time);
addHeaderView(mHeaderView);
// init footer view
mFooterView = new XListViewFooter(context);
// init header height
mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mHeaderViewHeight = mHeaderViewContent.getHeight();
getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}
@Override
public void setAdapter(ListAdapter adapter) {
// make sure XListViewFooter is the last footer view, and only add once.
if (mIsFooterReady == false) {
mIsFooterReady = true;
addFooterView(mFooterView);
}
super.setAdapter(adapter);
}
/**
* enable or disable pull down refresh feature.
*
* @param enable
*/
public void setPullRefreshEnable(boolean enable) {
mEnablePullRefresh = enable;
if (!mEnablePullRefresh) { // disable, hide the content
mHeaderViewContent.setVisibility(View.INVISIBLE);
} else {
mHeaderViewContent.setVisibility(View.VISIBLE);
}
}
/**
* enable or disable pull up load more feature.
*
* @param enable
*/
public void setPullLoadEnable(boolean enable) {
mEnablePullLoad = enable;
if (!mEnablePullLoad) {
mFooterView.hide();
mFooterView.setOnClickListener(null);
} else {
mPullLoading = false;
mFooterView.show();
mFooterView.setState(XListViewFooter.STATE_NORMAL);
// both "pull up" and "click" will invoke load more.
mFooterView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startLoadMore();
}
});
}
}
/**
* stop refresh, reset header view.
*/
public void stopRefresh() {
if (mPullRefreshing == true) {
mPullRefreshing = false;
resetHeaderHeight();
}
}
/**
* stop load more, reset footer view.
*/
public void stopLoadMore() {
if (mPullLoading == true) {
mPullLoading = false;
mFooterView.setState(XListViewFooter.STATE_NORMAL);
}
}
/**
* set last refresh time
*
* @param time
*/
public void setRefreshTime(String time) {
mHeaderTimeView.setText(time);
}
private void invokeOnScrolling() {
if (mScrollListener instanceof OnXScrollListener) {
OnXScrollListener l = (OnXScrollListener) mScrollListener;
l.onXScrolling(this);
}
}
private void updateHeaderHeight(float delta) {
mHeaderView.setVisiableHeight((int) delta + mHeaderView.getVisiableHeight());
if (mEnablePullRefresh && !mPullRefreshing) { // 未处于刷新状态,更新箭头
if (mHeaderView.getVisiableHeight() > mHeaderViewHeight) {
mHeaderView.setState(XListViewHeader.STATE_READY);
} else {
mHeaderView.setState(XListViewHeader.STATE_NORMAL);
}
}
setSelection(0); // scroll to top each time
}
/**
* reset header view's height.
*/
private void resetHeaderHeight() {
int height = mHeaderView.getVisiableHeight();
if (height == 0) // not visible.
return;
// refreshing and header isn't shown fully. do nothing.
if (mPullRefreshing && height <= mHeaderViewHeight) {
return;
}
int finalHeight = 0; // default: scroll back to dismiss header.
// is refreshing, just scroll back to show all the header.
if (mPullRefreshing && height > mHeaderViewHeight) {
finalHeight = mHeaderViewHeight;
}
mScrollBack = SCROLLBACK_HEADER;
mScroller.startScroll(0, height, 0, finalHeight - height, SCROLL_DURATION);
// trigger computeScroll
invalidate();
}
private void updateFooterHeight(float delta) {
int height = mFooterView.getBottomMargin() + (int) delta;
if (mEnablePullLoad && !mPullLoading) {
if (height > PULL_LOAD_MORE_DELTA) { // height enough to invoke load
// more.
mFooterView.setState(XListViewFooter.STATE_READY);
} else {
mFooterView.setState(XListViewFooter.STATE_NORMAL);
}
}
mFooterView.setBottomMargin(height);
// setSelection(mTotalItemCount - 1); // scroll to bottom
}
private void resetFooterHeight() {
int bottomMargin = mFooterView.getBottomMargin();
if (bottomMargin > 0) {
mScrollBack = SCROLLBACK_FOOTER;
mScroller.startScroll(0, bottomMargin, 0, -bottomMargin, SCROLL_DURATION);
invalidate();
}
}
private void startLoadMore() {
mPullLoading = true;
mFooterView.setState(XListViewFooter.STATE_LOADING);
if (mListViewListener != null) {
mListViewListener.onLoadMore();
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (mLastY == -1) {
mLastY = ev.getRawY();
}
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastY = ev.getRawY();
break;
case MotionEvent.ACTION_MOVE:
final float deltaY = ev.getRawY() - mLastY;
mLastY = ev.getRawY();
if (getFirstVisiblePosition() == 0 && (mHeaderView.getVisiableHeight() > 0 || deltaY > 0)) {
// the first item is showing, header has shown or pull down.
updateHeaderHeight(deltaY / OFFSET_RADIO);
invokeOnScrolling();
} else if (getLastVisiblePosition() == mTotalItemCount - 1 && (mFooterView.getBottomMargin() > 0 || deltaY <
0)) {
// last item, already pulled up or want to pull up.
updateFooterHeight(-deltaY / OFFSET_RADIO);
}
break;
default:
mLastY = -1; // reset
if (getFirstVisiblePosition() == 0) {
// invoke refresh
if (mEnablePullRefresh && mHeaderView.getVisiableHeight() > mHeaderViewHeight) {
mPullRefreshing = true;
mHeaderView.setState(XListViewHeader.STATE_REFRESHING);
if (mListViewListener != null) {
mListViewListener.onRefresh();
}
}
resetHeaderHeight();
} else if (getLastVisiblePosition() == mTotalItemCount - 1) {
// invoke load more.
if (mEnablePullLoad && mFooterView.getBottomMargin() > PULL_LOAD_MORE_DELTA) {
startLoadMore();
}
resetFooterHeight();
}
break;
}
return super.onTouchEvent(ev);
}
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
if (mScrollBack == SCROLLBACK_HEADER) {
mHeaderView.setVisiableHeight(mScroller.getCurrY());
} else {
mFooterView.setBottomMargin(mScroller.getCurrY());
}
postInvalidate();
invokeOnScrolling();
}
super.computeScroll();
}
@Override
public void setOnScrollListener(OnScrollListener l) {
mScrollListener = l;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (mScrollListener != null) {
mScrollListener.onScrollStateChanged(view, scrollState);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// send to user's listener
mTotalItemCount = totalItemCount;
if (mScrollListener != null) {
mScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
}
}
public void setXListViewListener(IXListViewListener l) {
mListViewListener = l;
}
/**
* you can listen ListView.OnScrollListener or this one. it will invoke
* onXScrolling when header/footer scroll back.
*/
public interface OnXScrollListener extends OnScrollListener {
public void onXScrolling(View view);
}
/**
* implements this interface to get refresh/load more event.
*/
public interface IXListViewListener {
public void onRefresh();
public void onLoadMore();
}
}
2)XListViewFooter
/**
* @file XFooterView.java
* @create Mar 31, 2012 9:33:43 PM
* @author Maxwin
* @description XListView's footer
*/
package com.bwie.yuekaotwo.view;
import com.bwie.yuekaotwo.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class XListViewFooter extends LinearLayout {
public final static int STATE_NORMAL = 0;
public final static int STATE_READY = 1;
public final static int STATE_LOADING = 2;
private Context mContext;
private View mContentView;
private View mProgressBar;
private TextView mHintView;
public XListViewFooter(Context context) {
super(context);
initView(context);
}
public XListViewFooter(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public void setState(int state) {
mHintView.setVisibility(View.INVISIBLE);
mProgressBar.setVisibility(View.INVISIBLE);
mHintView.setVisibility(View.INVISIBLE);
if (state == STATE_READY) {
mHintView.setVisibility(View.VISIBLE);
mHintView.setText(R.string.xlistview_footer_hint_ready);
} else if (state == STATE_LOADING) {
mProgressBar.setVisibility(View.VISIBLE);
} else {
mHintView.setVisibility(View.VISIBLE);
mHintView.setText(R.string.xlistview_footer_hint_normal);
}
}
public void setBottomMargin(int height) {
if (height < 0) return ;
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)mContentView.getLayoutParams();
lp.bottomMargin = height;
mContentView.setLayoutParams(lp);
}
public int getBottomMargin() {
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)mContentView.getLayoutParams();
return lp.bottomMargin;
}
/**
* normal status
*/
public void normal() {
mHintView.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.GONE);
}
/**
* loading status
*/
public void loading() {
mHintView.setVisibility(View.GONE);
mProgressBar.setVisibility(View.VISIBLE);
}
/**
* hide footer when disable pull load more
*/
public void hide() {
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)mContentView.getLayoutParams();
lp.height = 0;
mContentView.setLayoutParams(lp);
}
/**
* show footer
*/
public void show() {
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)mContentView.getLayoutParams();
lp.height = LayoutParams.WRAP_CONTENT;
mContentView.setLayoutParams(lp);
}
private void initView(Context context) {
mContext = context;
LinearLayout moreView = (LinearLayout)LayoutInflater.from(mContext).inflate(R.layout.xlistview_footer,
null);
addView(moreView);
moreView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
mContentView = moreView.findViewById(R.id.xlistview_footer_content);
mProgressBar = moreView.findViewById(R.id.xlistview_footer_progressbar);
mHintView = (TextView)moreView.findViewById(R.id.xlistview_footer_hint_textview);
}
}
3)XListViewHeader
/**
* @file XListViewHeader.java
* @create Apr 18, 2012 5:22:27 PM
* @author Maxwin
* @description XListView's header
*/
package com.bwie.yuekaotwo.view;
import com.bwie.yuekaotwo.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
public class XListViewHeader extends LinearLayout {
private LinearLayout mContainer;
private ImageView mArrowImageView;
private ProgressBar mProgressBar;
private TextView mHintTextView;
private int mState = STATE_NORMAL;
private Animation mRotateUpAnim;
private Animation mRotateDownAnim;
private final int ROTATE_ANIM_DURATION = 180;
public final static int STATE_NORMAL = 0;
public final static int STATE_READY = 1;
public final static int STATE_REFRESHING = 2;
public XListViewHeader(Context context) {
super(context);
initView(context);
}
/**
* @param context
* @param attrs
*/
public XListViewHeader(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
private void initView(Context context) {
// 鍒濆鎯呭喌锛岃缃笅鎷夊埛鏂皏iew楂樺害涓�0
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, 0);
mContainer = (LinearLayout) LayoutInflater.from(context).inflate(
R.layout.xlistview_header, null);
addView(mContainer, lp);
setGravity(Gravity.BOTTOM);
mArrowImageView = (ImageView)findViewById(R.id.xlistview_header_arrow);
mHintTextView = (TextView)findViewById(R.id.xlistview_header_hint_textview);
mProgressBar = (ProgressBar)findViewById(R.id.xlistview_header_progressbar);
mRotateUpAnim = new RotateAnimation(0.0f, -180.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION);
mRotateUpAnim.setFillAfter(true);
mRotateDownAnim = new RotateAnimation(-180.0f, 0.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION);
mRotateDownAnim.setFillAfter(true);
}
public void setState(int state) {
if (state == mState) return ;
if (state == STATE_REFRESHING) { // 鏄剧ず杩涘害
mArrowImageView.clearAnimation();
mArrowImageView.setVisibility(View.INVISIBLE);
mProgressBar.setVisibility(View.VISIBLE);
} else { // 鏄剧ず绠ご鍥剧墖
mArrowImageView.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.INVISIBLE);
}
switch(state){
case STATE_NORMAL:
if (mState == STATE_READY) {
mArrowImageView.startAnimation(mRotateDownAnim);
}
if (mState == STATE_REFRESHING) {
mArrowImageView.clearAnimation();
}
mHintTextView.setText(R.string.xlistview_header_hint_normal);
break;
case STATE_READY:
if (mState != STATE_READY) {
mArrowImageView.clearAnimation();
mArrowImageView.startAnimation(mRotateUpAnim);
mHintTextView.setText(R.string.xlistview_header_hint_ready);
}
break;
case STATE_REFRESHING:
mHintTextView.setText(R.string.xlistview_header_hint_loading);
break;
default:
}
mState = state;
}
public void setVisiableHeight(int height) {
if (height < 0)
height = 0;
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mContainer
.getLayoutParams();
lp.height = height;
mContainer.setLayoutParams(lp);
}
public int getVisiableHeight() {
return mContainer.getHeight();
}
}
第八步.清单文件的权利
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bwie.yuekaotwo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:name="com.bwie.yuekaotwo.application.MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>