详情页的banner图同样是使用ViewPager操作的,因为父类也有ViewPager因此需要对让父类的不进行拦截,在布局中继承父类的对象,关于数据的读取是从服务器中读取的,加载资源数据,获取数据来填充ViewPager
tab详情页布局tab_detail_pager.xml.上面的部分是banner图,下面的是listView新闻列表
<?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" >
<!-- banner图,继承处理以后的ViewPager,防止父类拦截 -->
<com.ldw.news.view.TopNewsViewPager
android:id="@+id/vp_news"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
<!-- 下面的新闻条目 -->
<ListView
android:id="@+id/lv_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
></ListView>
</LinearLayout>
TabData.java新闻数据的对象bean,需要从服务器读取的数据的类型的初始化
package com.ldw.news.domain;
import java.util.ArrayList;
import com.ldw.news.domain.NewsData.NewsMenuData;
/*
* tab详情数据
*/
public class TabData {
public int retcode;//返回值
public TabDetail data;//字段数据
// tab页的详情数据
public class TabDetail {
public String title;
public String more;
public ArrayList<TabNewsData> news;
public ArrayList<TopNewsData> topnews;
@Override
public String toString() {
return "TabDetail [title=" + title + ", more=" + more + ", news="
+ news + ", topnews=" + topnews + "]";
}
}
//新闻列表对象
public class TabNewsData{
public String id;
public String listimage;
public String pupdate;
public String title;
public String type;
public String url;
@Override
public String toString() {
return "TabNewsData [id=" + id + ", listimage=" + listimage
+ ", pupdate=" + pupdate + ", title=" + title + ", type="
+ type + ", url=" + url + "]";
}
}
//头条新闻
public class TopNewsData{
public String id;
public String topimage;
public String pupdate;
public String title;
public String type;
public String url;
@Override
public String toString() {
return "TopNewData [id=" + id + ", topimage=" + topimage
+ ", pupdate=" + pupdate + ", title=" + title + ", type="
+ type + ", url=" + url + "]";
}
}
}
TabDetailPager.java新闻详情页面的处理,涉及到加载网络数据,创建ViewPager继承PagerAdapter,然后初始化banner数据
package com.ldw.news.base;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.ldw.news.R;
import com.ldw.news.domain.NewsData.NewsTabData;
import com.ldw.news.domain.TabData;
import com.ldw.news.domain.TabData.TopNewsData;
import com.ldw.news.global.ClobalContants;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
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 com.lidroid.xutils.view.annotation.ViewInject;
/*
* 左边菜单栏右边详情页,最上面对应的tab,页签
*/
public class TabDetailPager extends BaseMenuDetailPager {
//携带数据详情的数据
private NewsTabData mTabData;
private TextView text;
private String mUrl;
private TabData mTabDetailData;
@ViewInject(R.id.vp_news)
private ViewPager mViewPager;
public TabDetailPager(Activity activity, NewsTabData newsTabData) {
super(activity);
mTabData = newsTabData;
//图片的地址
mUrl = ClobalContants.SERVER_URL + mTabData.url;
}
@Override
public View initView() {
View view = View.inflate(mActivity, R.layout.tab_detail_pager, null);
ViewUtils.inject(this, view);
// 向FrameLayout中动态添加布局
return view;
}
@Override
public void initData(){
getDataFromServer();
}
/*
* 获取网络数据
*/
private void getDataFromServer() {
//从服务器中获取数据
HttpUtils utils = new HttpUtils();
utils.send(HttpMethod.GET, mUrl, new RequestCallBack<String>(){
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
//获取到返回的结果
String result = (String) responseInfo.result;
System.out.println("tab详情返回结果:" + result);
parseData(result);
}
@Override
public void onFailure(HttpException error, String msg) {
Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT)
.show();
//打印错误信息
error.printStackTrace();
}
});
}
/*
* 解析网络数据
*/
protected void parseData(String result) {
//使用Gson解析
Gson gson = new Gson();
mTabDetailData = gson.fromJson(result, TabData.class);
System.out.println("解析结果:" + mTabDetailData);
mViewPager.setAdapter(new TopNewsAdapter());
}
/*
* 头条新闻,banner
*/
class TopNewsAdapter extends PagerAdapter{
private BitmapUtils utils;
//构造函数
public TopNewsAdapter(){
utils = new BitmapUtils(mActivity);
//加载数据过程中,设置默认加载的图片
utils.configDefaultLoadingImage(R.drawable.topnews_item_default);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mTabDetailData.data.topnews.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
//初始化banner图
ImageView image = new ImageView(mActivity);
//填充布局对象
//image.setImageResource(R.drawable.topnews_item_default);
//填充平铺布局,基于控件的大小
image.setScaleType(ScaleType.FIT_XY);
//获取到图片资源的数据的地址
TopNewsData topNewsData = mTabDetailData.data.topnews.get(position);
utils.display(image, topNewsData.topimage);
//添加image数据
container.addView(image);
return image;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}