详情页的tab是由ViewPager来实现切换的
布局news_menu_details.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" >
<android.support.v4.view.ViewPager
android:id="@+id/vp_menu_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
NewsMenuDetailPager.java逻辑页面
package com.ldw.news.base.menudetails;
import java.util.ArrayList;
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 com.ldw.news.R;
import com.ldw.news.base.BaseMenuDetailPager;
import com.ldw.news.base.TabDetailPager;
import com.ldw.news.domain.NewsData.NewsTabData;
/*
* 菜单详情页-新闻
*/
public class NewsMenuDetailPager extends BaseMenuDetailPager {
private ViewPager vp_menu_detail;
private ArrayList<TabDetailPager> mPagerList;
private ArrayList<NewsTabData> mNewsTabData;//页签的网络数据
public NewsMenuDetailPager(Activity activity, ArrayList<NewsTabData> children) {
super(activity);
//接受传过来的children数据,有11个
mNewsTabData = children;
}
@Override
public View initView() {
View view = View.inflate(mActivity, R.layout.news_menu_details, null);
vp_menu_detail = (ViewPager) view.findViewById(R.id.vp_menu_detail);
return view;
}
@Override
public void initData(){
//数据是通过从服务器中获取大数据来初始化
mPagerList = new ArrayList<TabDetailPager>();
for(int i = 0; i < mNewsTabData.size(); i++){
TabDetailPager tabDetailPager = new TabDetailPager(mActivity, mNewsTabData.get(i));
//添加到链表
mPagerList.add(tabDetailPager);
}
vp_menu_detail.setAdapter(new MenuDetailAdapter());
}
class MenuDetailAdapter extends PagerAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return mPagerList.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) {
//初始化页面
TabDetailPager tabDetailPager = new TabDetailPager(mActivity, mNewsTabData.get(position));
//添加view
container.addView(tabDetailPager.mRebootView);
//初始化数据
tabDetailPager.initData();
//显示界面
return tabDetailPager.mRebootView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}
BaseMenuDetailPager.java是NewsMenuDetailPager的基类
package com.ldw.news.base;
import android.app.Activity;
import android.view.View;
/*
* 点击左侧菜单的时候,右侧显示的详细内容
*/
public abstract class BaseMenuDetailPager {
public Activity mActivity;
public View mRebootView;//根局部
public BaseMenuDetailPager(Activity activity){
mActivity = activity;
mRebootView = initView();
}
/*
* 初始化界面,设置成虚函数,必须实现
*/
public abstract View initView();
/*
* 初始化数据
*/
public void initData(){
}
}
NewsCenterPager.java解析网络数据并往详情页传递数据
package com.ldw.news.base.impl;
import java.util.ArrayList;
import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.ldw.news.MainActivity;
import com.ldw.news.base.BaseMenuDetailPager;
import com.ldw.news.base.BasePager;
import com.ldw.news.base.menudetails.InteractMenuDetailPager;
import com.ldw.news.base.menudetails.NewsMenuDetailPager;
import com.ldw.news.base.menudetails.PhotoMenuDetailPager;
import com.ldw.news.base.menudetails.TopicMenuDetailPager;
import com.ldw.news.domain.NewsData;
import com.ldw.news.domain.NewsData.NewsMenuData;
import com.ldw.news.fragment.LeftMenuFragment;
import com.ldw.news.global.ClobalContants;
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;
/**
* 新闻中心
*
* @author Kevin
*
*/
public class NewsCenterPager extends BasePager {
// 4个菜单详情页的集合,填充左侧菜单栏对应的ViewPager中的详情
private ArrayList<BaseMenuDetailPager> mPagers;
private NewsData mNewsData;
public NewsCenterPager(Activity activity) {
super(activity);
}
@Override
public void initData() {
System.out.println("初始化设置数据....");
tvTitle.setText("news");
//btnMenu.setVisibility(View.GONE);// 隐藏菜单按钮
setSlidingMenuEnable(true);// 关闭侧边栏
/*
TextView text = new TextView(mActivity);
text.setText("设置");
text.setTextColor(Color.RED);
text.setTextSize(25);
text.setGravity(Gravity.CENTER);
// 向FrameLayout中动态添加布局
flContent.addView(text);
*/
//从服务器中获取数据
getDataFromServer();
}
/*
* 从服务器中获取数据,使用xUtils实现
*/
private void getDataFromServer(){
HttpUtils utils = new HttpUtils();
//参数是String获取到的是String
utils.send(HttpMethod.GET, ClobalContants.CATEGORIES_URL, new RequestCallBack<String>(){
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
//获取到返回的结果
String result = (String) responseInfo.result;
System.out.println("返回结果:" + 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();
//第一个参数是数据,第二个参数是要转化成什么对象
mNewsData = gson.fromJson(result, NewsData.class);
System.out.println("解析结果:" + mNewsData);
// 刷新测边栏的数据,利用MainActivity获取到左侧边栏的数据
MainActivity mainUi = (MainActivity) mActivity;
//获取到左侧边栏的对象
LeftMenuFragment leftMenuFragment = mainUi.getLeftMenuFragment();
//设置网络数据
leftMenuFragment.setMenuData(mNewsData);
//设置4个菜单详情页,LeftMenuFragment和内容的交互,需要调用这里的方法,来初始化侧边栏对应的内容
mPagers = new ArrayList<BaseMenuDetailPager>();
//获取到data条目下面,news对于页面的children数据,初始化详情页的构造函数
mPagers.add(new NewsMenuDetailPager(mActivity, mNewsData.data.get(0).children));
mPagers.add(new TopicMenuDetailPager(mActivity));
mPagers.add(new PhotoMenuDetailPager(mActivity));
mPagers.add(new InteractMenuDetailPager(mActivity));
// 设置菜单详情页-新闻为默认当前页
setCurrentMenuDetailPager(0);
}
//设置当前的菜单详情页,显示哪一个详情页,LeftMenuFragment和内容的交互
public void setCurrentMenuDetailPager(int position){
//获取到某一个位置的pager
BaseMenuDetailPager pager = mPagers.get(position);
// 清除之前的布局,fragment布局的内容会叠加
flContent.removeAllViews();
//填充内容,mRebootView就是初始化的某一个子页面,设置给frameLayout
flContent.addView(pager.mRebootView);
// 设置当前页的标题
NewsMenuData menuData = mNewsData.data.get(position);
tvTitle.setText(menuData.title);
// 初始化当前页面的数据
pager.initData();
}
}
详情页的页签TabDetailPager.java
package com.ldw.news.base;
import com.ldw.news.domain.NewsData.NewsTabData;
import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
/*
* 左边菜单栏右边详情页,最上面对应的tab,页签
*/
public class TabDetailPager extends BaseMenuDetailPager {
//携带数据详情的数据
private NewsTabData mTabData;
private TextView text;
public TabDetailPager(Activity activity, NewsTabData newsTabData) {
super(activity);
mTabData = newsTabData;
}
@Override
public View initView() {
text = new TextView(mActivity);
text.setText("页签详情");
text.setTextColor(Color.RED);
text.setTextSize(25);
text.setGravity(Gravity.CENTER);
// 向FrameLayout中动态添加布局
return text;
}
@Override
public void initData(){
text.setText(mTabData.title);
}
}