将网络数据传递给侧边栏
运行效果:
使用XUtils开源项目,完成网络请求;使用GSON开源项目完成JSON格式数据的解析。
Gson(又称Google Gson)是Google公司发布的一个开放源代码的Java库,主要用途为序列化Java对象为JSON字符串,或反序列化JSON字符串成Java对象。
GlobalVariables.java
package com.xbmu.wisdombj.global;
/**
* 定义全局的变量
* Created by Administrator on 2016/2/6 0006.
*/
public class GlobalVariables {
public static final String BASE_URL = "http://10.0.2.2:8080/zhbj/";//基本的访问地址
public static final String NEWS_URL = BASE_URL + "categories.json";//新闻中心地址
}
NewsData.java
package com.xbmu.wisdombj.bean;
import java.util.ArrayList;
/**
* 网络分类信息的封装
* 字段名字必须和服务器返回的字段名一致,方便GSON解析
* Created by Administrator on 2016/2/6 0006.
*/
public class NewsData {
public int retcode;
public ArrayList<NewsMenuData> data;
//侧滑菜单中的数据
public class NewsMenuData{
public String id;
public String title;
public int type;
public ArrayList<NewsTabData> children;
@Override
public String toString() {
return "NewsMenuData{" +
"id='" + id + '\'' +
", title='" + title + '\'' +
", type=" + type +
'}';
}
}
//新闻页面下11个子页面标签的数据
public class NewsTabData{
public String id;
public String title;
public int type;
public String url;
@Override
public String toString() {
return "NewsTabData{" +
"id='" + id + '\'' +
", title='" + title + '\'' +
", type=" + type +
", url='" + url + '\'' +
'}';
}
}
@Override
public String toString() {
return "NewsData{" +
"retcode=" + retcode +
'}';
}
}
NewsCenterPager.java
package com.xbmu.wisdombj.base.impl;
import android.app.Activity;
import android.widget.Toast;
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 com.xbmu.wisdombj.MainActivity;
import com.xbmu.wisdombj.base.BaseNewsDetailPager;
import com.xbmu.wisdombj.base.BasePager;
import com.xbmu.wisdombj.base.menudetail.NewsDetailPager;
import com.xbmu.wisdombj.base.menudetail.NewsInteractDetailPager;
import com.xbmu.wisdombj.base.menudetail.NewsPhotoDetailPager;
import com.xbmu.wisdombj.base.menudetail.NewsSubjectDetailPager;
import com.xbmu.wisdombj.bean.NewsData;
import com.xbmu.wisdombj.fragment.LeftMenuFragment;
import com.xbmu.wisdombj.global.GlobalVariables;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2016/2/5 0005.
*/
public class NewsCenterPager extends BasePager {
private NewsData newsData;
public NewsCenterPager(Activity activity) {
super(activity);
}
@Override
public void initData() {
setSlidingMenuEnable(true);
//从服务器获取数据
getDataFromServer();
}
/**
* 从服务器获取数据
*
* @return
*/
public void getDataFromServer() {
HttpUtils httpUtils = new HttpUtils();
httpUtils.send(HttpMethod.GET, GlobalVariables.NEWS_URL, new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
String result = responseInfo.result;
System.out.println("从网络获取的数据:" + result);
//解析数据
paserData(result);
}
@Override
public void onFailure(HttpException e, String msg) {
Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
});
}
/**
* 解析数据
* @param result
*/
private void paserData(String result) {
//使用第三方开源的框架GSON解析数据
Gson gson = new Gson();
newsData = gson.fromJson(result, NewsData.class);
//获取侧滑菜单并设置侧滑菜单数据
MainActivity mainActivity = (MainActivity) mActivity;
LeftMenuFragment leftMenuFragment =mainActivity.getLeftMenuFragment();
leftMenuFragment.setMenuData(newsData);
}
}
MainActivity.java
package com.xbmu.wisdombj;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.xbmu.wisdombj.fragment.ContentFragment;
import com.xbmu.wisdombj.fragment.LeftMenuFragment;
/**
* 主页面
*/
public class MainActivity extends SlidingFragmentActivity {
private static final String LEFT_MENU_FRAGMENT = "leftMenuFragment";
private static final String CONTENT_FRAGMENT = "contentFragment";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setBehindContentView(R.layout.left_menu);//设置侧边栏
SlidingMenu slidingMenu = getSlidingMenu();//获取侧边栏对象
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);//设置全屏触摸
slidingMenu.setBehindOffset(200);//设置预留屏幕的宽度
initFragment();
}
/**
* 初始化Fragment
*/
private void initFragment() {
FragmentManager fm = getSupportFragmentManager();
//开启事务
FragmentTransaction ft = fm.beginTransaction();
//替换帧布局
ft.replace(R.id.fl_left_menu, new LeftMenuFragment(), LEFT_MENU_FRAGMENT);
ft.replace(R.id.fl_content, new ContentFragment(), CONTENT_FRAGMENT);
//提交事务
ft.commit();
}
/**
* 获取侧边栏对象
* @return
*/
public LeftMenuFragment getLeftMenuFragment() {
FragmentManager fm = getSupportFragmentManager();
LeftMenuFragment leftMenuFragment = (LeftMenuFragment) fm.findFragmentByTag(LEFT_MENU_FRAGMENT);
return leftMenuFragment;
}
/**
* 获取主页面对象
* @return
*/
public ContentFragment getContentFragment() {
FragmentManager fm = getSupportFragmentManager();
ContentFragment contentFragment = (ContentFragment) fm.findFragmentByTag(CONTENT_FRAGMENT);
return contentFragment;
}
}
LeftMenuFragment.java
package com.xbmu.wisdombj.fragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xbmu.wisdombj.MainActivity;
import com.xbmu.wisdombj.R;
import com.xbmu.wisdombj.base.impl.NewsCenterPager;
import com.xbmu.wisdombj.bean.NewsData.NewsMenuData;
import com.xbmu.wisdombj.bean.NewsData;
import java.util.ArrayList;
/**
* 左侧菜单的Fragment
* Created by Administrator on 2016/2/4 0004.
*/
public class LeftMenuFragment extends BaseFragment{
@ViewInject(R.id.lv_list)
private ListView lvList;
private ArrayList<NewsMenuData> newsMenuList;
private LeftMenuAdapter leftMenuAdapter;
@Override
public View initView() {
View view = View.inflate(mActivity, R.layout.fragment_left_menu, null);
ViewUtils.inject(this, view);
return view;
}
@Override
public void initData() {
super.initData();
}
public void setMenuData(NewsData newsData) {
newsMenuList = newsData.data;
leftMenuAdapter = new LeftMenuAdapter();
lvList.setAdapter(leftMenuAdapter);
}
private class LeftMenuAdapter extends BaseAdapter{
@Override
public int getCount() {
return newsMenuList.size();
}
@Override
public NewsMenuData getItem(int position) {
return newsMenuList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(mActivity,R.layout.left_menu_item,null);
TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
tvTitle.setText(newsMenuList.get(position).title);
return view;
}
}
}
点击侧边栏切换页面
运行效果:
根据效果图,分析界面的设计
由上面的图可以观察到:当切换到“新闻中心”页面时,打开侧边栏,有"新闻","专题","组图","互动"四个条目,当我们点击任意其中一个条目时,会切换到各自的详情页面,
这里就有四个详情页面的设计。我们就需要一个类对象去创建者四个页面。
BaseNewsDetailPager.java
package com.xbmu.wisdombj.base;
import android.app.Activity;
import android.view.View;
/**
* 新闻详情页面的基类(是侧边栏:新闻,专题,互动,组图页面的基类)
* Created by Administrator on 2016/2/6 0006.
*/
public abstract class BaseNewsDetailPager {
public Activity mActivity;
public View rootView;
public BaseNewsDetailPager(Activity mActivity) {
this.mActivity = mActivity;
rootView = initView();
}
/**
* 初始化布局
* @return
*/
public abstract View initView() ;
/**
* 初始化数据
*/
public void initData(){
}
}
NewsDetailPager.java
package com.xbmu.wisdombj.base.menudetail;
import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import com.xbmu.wisdombj.base.BaseNewsDetailPager;
/**
* 新闻页面-详情
* Created by Administrator on 2016/2/6 0006.
*/
public class NewsDetailPager extends BaseNewsDetailPager {
public NewsDetailPager(Activity mActiviy) {
super(mActiviy);
}
@Override
public View initView() {
TextView tvContent = new TextView(mActivity);
tvContent.setTextSize(20);
tvContent.setText("新闻页面-详情");
tvContent.setTextColor(Color.RED);
tvContent.setGravity(Gravity.CENTER);
return tvContent;
}
}
NewsInteractDetailPager.java
package com.xbmu.wisdombj.base.menudetail;
import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import com.xbmu.wisdombj.base.BaseNewsDetailPager;
/**
* 新闻页面-互动
* Created by Administrator on 2016/2/6 0006.
*/
public class NewsInteractDetailPager extends BaseNewsDetailPager {
public NewsInteractDetailPager(Activity mActiviy) {
super(mActiviy);
}
@Override
public View initView() {
TextView tvContent = new TextView(mActivity);
tvContent.setTextSize(20);
tvContent.setText("新闻页面-互动");
tvContent.setTextColor(Color.RED);
tvContent.setGravity(Gravity.CENTER);
return tvContent;
}
}
NewsPhotoDetailPager.java
package com.xbmu.wisdombj.base.menudetail;
import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import com.xbmu.wisdombj.base.BaseNewsDetailPager;
/**
* 新闻页面-组图
* Created by Administrator on 2016/2/6 0006.
*/
public class NewsPhotoDetailPager extends BaseNewsDetailPager {
public NewsPhotoDetailPager(Activity mActiviy) {
super(mActiviy);
}
@Override
public View initView() {
TextView tvContent = new TextView(mActivity);
tvContent.setTextSize(20);
tvContent.setText("新闻页面-组图");
tvContent.setTextColor(Color.RED);
tvContent.setGravity(Gravity.CENTER);
return tvContent;
}
}
NewsSubjectDetailPager.java
package com.xbmu.wisdombj.base.menudetail;
import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import com.xbmu.wisdombj.base.BaseNewsDetailPager;
/**
* 新闻页面-专题
* Created by Administrator on 2016/2/6 0006.
*/
public class NewsSubjectDetailPager extends BaseNewsDetailPager {
public NewsSubjectDetailPager(Activity mActiviy) {
super(mActiviy);
}
@Override
public View initView() {
TextView tvContent = new TextView(mActivity);
tvContent.setTextSize(20);
tvContent.setText("新闻页面-专题");
tvContent.setTextColor(Color.RED);
tvContent.setGravity(Gravity.CENTER);
return tvContent;
}
}
上面,我们创建了四个详情页面的类。然后添加到"新闻中心"页面中去。
NewsCenterPager.java
package com.xbmu.wisdombj.base.impl;
import android.app.Activity;
import android.widget.Toast;
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 com.xbmu.wisdombj.MainActivity;
import com.xbmu.wisdombj.base.BaseNewsDetailPager;
import com.xbmu.wisdombj.base.BasePager;
import com.xbmu.wisdombj.base.menudetail.NewsDetailPager;
import com.xbmu.wisdombj.base.menudetail.NewsInteractDetailPager;
import com.xbmu.wisdombj.base.menudetail.NewsPhotoDetailPager;
import com.xbmu.wisdombj.base.menudetail.NewsSubjectDetailPager;
import com.xbmu.wisdombj.bean.NewsData;
import com.xbmu.wisdombj.fragment.LeftMenuFragment;
import com.xbmu.wisdombj.global.GlobalVariables;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2016/2/5 0005.
*/
public class NewsCenterPager extends BasePager {
private NewsData newsData;
private List<BaseNewsDetailPager> newsDetailPagerList;//封装侧滑菜单中4个详情页面
public NewsCenterPager(Activity activity) {
super(activity);
}
@Override
public void initData() {
setSlidingMenuEnable(true);
//从服务器获取数据
getDataFromServer();
}
/**
* 从服务器获取数据
*
* @return
*/
public void getDataFromServer() {
HttpUtils httpUtils = new HttpUtils();
httpUtils.send(HttpMethod.GET, GlobalVariables.NEWS_URL, new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
String result = responseInfo.result;
System.out.println("从网络获取的数据:" + result);
//解析数据
paserData(result);
}
@Override
public void onFailure(HttpException e, String msg) {
Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
});
}
/**
* 解析数据
* @param result
*/
private void paserData(String result) {
//使用第三方开源的框架GSON解析数据
Gson gson = new Gson();
newsData = gson.fromJson(result, NewsData.class);
//获取侧滑菜单并设置侧滑菜单数据
MainActivity mainActivity = (MainActivity) mActivity;
LeftMenuFragment leftMenuFragment =mainActivity.getLeftMenuFragment();
leftMenuFragment.setMenuData(newsData);
//准备4个菜单的详情页面
newsDetailPagerList = new ArrayList<>();
newsDetailPagerList.add(new NewsDetailPager(mActivity));
newsDetailPagerList.add(new NewsSubjectDetailPager(mActivity));
newsDetailPagerList.add(new NewsPhotoDetailPager(mActivity));
newsDetailPagerList.add(new NewsInteractDetailPager(mActivity));
//设置默认新闻详情页面
setCurrentNewDetailPager(0);
}
/**
* 设置当前新闻详情页面
*/
public void setCurrentNewDetailPager(int position) {
//获取当前新闻详情页面
BaseNewsDetailPager newsCurrentDetailPager = newsDetailPagerList.get(position);
flContent.removeAllViews();//清除之前所有的布局
//添加到FrameLayout中
flContent.addView(newsCurrentDetailPager.rootView);
//设置当前页的标题
NewsData.NewsMenuData newsMenuData = newsData.data.get(position);
tvTitle.setText(newsMenuData.title);
}
}
LeftMenuFragment.java
package com.xbmu.wisdombj.fragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xbmu.wisdombj.MainActivity;
import com.xbmu.wisdombj.R;
import com.xbmu.wisdombj.base.impl.NewsCenterPager;
import com.xbmu.wisdombj.bean.NewsData.NewsMenuData;
import com.xbmu.wisdombj.bean.NewsData;
import java.util.ArrayList;
/**
* 左侧菜单的Fragment
* Created by Administrator on 2016/2/4 0004.
*/
public class LeftMenuFragment extends BaseFragment{
@ViewInject(R.id.lv_list)
private ListView lvList;
private ArrayList<NewsMenuData> newsMenuList;
private int mCurrentPosition;//侧滑菜单当前选中的位置
private LeftMenuAdapter leftMenuAdapter;
@Override
public View initView() {
View view = View.inflate(mActivity, R.layout.fragment_left_menu, null);
ViewUtils.inject(this, view);
return view;
}
@Override
public void initData() {
super.initData();
lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mCurrentPosition = position;
//刷新ListView界面
leftMenuAdapter.notifyDataSetChanged();
//点击了侧边栏的新闻条目,显示相应的新闻详情页面
setCurrentNewsDetailPager(position);
//侧边栏切换开关
toggleSlidingMenu();
}
});
}
/**
* 侧滑菜单的显示和隐藏
*/
private void toggleSlidingMenu() {
MainActivity mainActivity = (MainActivity) mActivity;
SlidingMenu slidingMenu = mainActivity.getSlidingMenu();
slidingMenu.toggle();
}
private void setCurrentNewsDetailPager(int position) {
MainActivity mainActivity = (MainActivity) mActivity;
ContentFragment contentFragment = mainActivity.getContentFragment();//获取主页面Fragment
NewsCenterPager newsCenterPager = contentFragment.getNewsCenterPager();//获取新闻中心页面
newsCenterPager.setCurrentNewDetailPager(position);//设置当前新闻菜单详情页
}
public void setMenuData(NewsData newsData) {
newsMenuList = newsData.data;
leftMenuAdapter = new LeftMenuAdapter();
lvList.setAdapter(leftMenuAdapter);
}
private class LeftMenuAdapter extends BaseAdapter{
@Override
public int getCount() {
return newsMenuList.size();
}
@Override
public NewsMenuData getItem(int position) {
return newsMenuList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(mActivity,R.layout.left_menu_item,null);
TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
tvTitle.setText(newsMenuList.get(position).title);
if(mCurrentPosition == position){//被选中了
tvTitle.setEnabled(true);//显示红色
}else{
tvTitle.setEnabled(false);
}
return view;
}
}
}
侧边栏切换页面细节优化
BasePager.java
package com.xbmu.wisdombj.base;
import android.app.Activity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.xbmu.wisdombj.MainActivity;
import com.xbmu.wisdombj.R;
/**
* 主页下5个子页面的基类
* Created by Administrator on 2016/2/5 0005.
*/
public class BasePager {
public Activity mActivity;
public View rootView;
public TextView tvTitle;
public FrameLayout flContent;
public ImageButton ibMenu;
public BasePager(Activity activity) {
mActivity = activity;
initView();
}
/**
* 初始化布局
*/
public void initView(){
rootView = View.inflate(mActivity, R.layout.base_pager, null);
tvTitle = (TextView) rootView.findViewById(R.id.tv_title);
flContent = (FrameLayout) rootView.findViewById(R.id.fl_content);
ibMenu = (ImageButton) rootView.findViewById(R.id.ib_menu);
ibMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleSlidingMenu();
}
});
}
/**
* 侧滑菜单的显示和隐藏
*/
private void toggleSlidingMenu() {
MainActivity mainActivity = (MainActivity) mActivity;
SlidingMenu slidingMenu = mainActivity.getSlidingMenu();
slidingMenu.toggle();
}
/**
* 初始化数据
*/
public void initData(){
}
/**
* 设置侧边栏的打开或者关闭
* @param enable
*/
public void setSlidingMenuEnable(boolean enable){
MainActivity mainActivity = (MainActivity) mActivity;
SlidingMenu slidingMenu = mainActivity.getSlidingMenu();
if(enable){
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
}else{
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
}
}
}
fragment_left_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#000"
android:layout_height="match_parent">
<ListView
android:layout_marginTop="50dp"
android:listSelector="@android:color/transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lv_list"></ListView>
</RelativeLayout>
left_menu_item.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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新闻"
android:textColor="@drawable/left_menu_item_text_selector"
android:id="@+id/tv_title"
android:textSize="25sp"
android:padding="10dp"
android:drawableLeft="@drawable/left_menu_item_selector"
android:drawablePadding="10dp"
android:layout_gravity="center"
/>
</LinearLayout>
left_menu_item_selector.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/menu_arr_select"/>
<item android:drawable="@drawable/menu_arr_normal"/>
</selector>
left_menu_item_text_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:color="#f00"/>
<item android:color="#fff"/>
</selector>
页签详情页实现:
"新闻中心",侧边栏中的新闻有12个字页签的实现,运行效果:
TabDetailPager.java
package com.xbmu.wisdombj.base;
import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import com.xbmu.wisdombj.bean.NewsData;
/**
* 页签详情页,表示每一个页签的对象
* Created by Administrator on 2016/2/7 0007.
*/
public class TabDetailPager extends BaseNewsDetailPager {
private TextView tvContent;
private NewsData.NewsTabData mTabData;
public TabDetailPager(Activity mActivity, NewsData.NewsTabData newsTabDatas) {
super(mActivity);
this.mTabData =newsTabDatas;
}
@Override
public View initView() {
tvContent = new TextView(mActivity);
tvContent.setTextSize(20);
tvContent.setText("详情页");
tvContent.setTextColor(Color.RED);
tvContent.setGravity(Gravity.CENTER);
return tvContent;
}
@Override
public void initData() {
super.initData();
tvContent.setText(mTabData.title);
}
}
NewsMenuDetailPager.java
package com.xbmu.wisdombj.base.menudetail;
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.xbmu.wisdombj.R;
import com.xbmu.wisdombj.base.BaseNewsDetailPager;
import com.xbmu.wisdombj.base.TabDetailPager;
import com.xbmu.wisdombj.bean.NewsData;
import java.util.ArrayList;
import java.util.List;
/**
* 新闻页面-详情
* Created by Administrator on 2016/2/6 0006.
*/
public class NewsMenuDetailPager extends BaseNewsDetailPager {
private ArrayList<NewsData.NewsTabData> mTabDatas;
private ViewPager vpMenuDetail;
private List<TabDetailPager> tabDetailPagersList = new ArrayList<>();
public NewsMenuDetailPager(Activity mActiviy, ArrayList<NewsData.NewsTabData> newsTabDatas) {
super(mActiviy);
this.mTabDatas = newsTabDatas;
}
@Override
public View initView() {
View view = View.inflate(mActivity, R.layout.news_menu_detail, null);
vpMenuDetail = (ViewPager) view.findViewById(R.id.vp_menu_detail);
return view;
}
@Override
public void initData() {
super.initData();
System.out.println("子标签页的个数:" + mTabDatas.size());
for (int i = 0; i < mTabDatas.size(); i++) {
tabDetailPagersList.add(new TabDetailPager(mActivity,mTabDatas.get(i)));
}
vpMenuDetail.setAdapter(new NewsMenuDetailAdapter());
}
private class NewsMenuDetailAdapter extends PagerAdapter {
@Override
public int getCount() {
return mTabDatas.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
TabDetailPager tabDetailPager = tabDetailPagersList.get(position);
container.addView(tabDetailPager.rootView);
tabDetailPager.initData();
return tabDetailPager.rootView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
}
}
NewsCenterPager.java
package com.xbmu.wisdombj.base.impl;
import android.app.Activity;
import android.widget.Toast;
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 com.xbmu.wisdombj.MainActivity;
import com.xbmu.wisdombj.base.BaseNewsDetailPager;
import com.xbmu.wisdombj.base.BasePager;
import com.xbmu.wisdombj.base.menudetail.NewsMenuDetailPager;
import com.xbmu.wisdombj.base.menudetail.NewsInteractDetailPager;
import com.xbmu.wisdombj.base.menudetail.NewsPhotoDetailPager;
import com.xbmu.wisdombj.base.menudetail.NewsSubjectDetailPager;
import com.xbmu.wisdombj.bean.NewsData;
import com.xbmu.wisdombj.fragment.LeftMenuFragment;
import com.xbmu.wisdombj.global.GlobalVariables;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2016/2/5 0005.
*/
public class NewsCenterPager extends BasePager {
private NewsData newsData;
private List<BaseNewsDetailPager> newsDetailPagerList;//封装侧滑菜单中4个详情页面
public NewsCenterPager(Activity activity) {
super(activity);
}
@Override
public void initData() {
setSlidingMenuEnable(true);
//从服务器获取数据
getDataFromServer();
}
/**
* 从服务器获取数据
*
* @return
*/
public void getDataFromServer() {
HttpUtils httpUtils = new HttpUtils();
httpUtils.send(HttpMethod.GET, GlobalVariables.NEWS_URL, new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
String result = responseInfo.result;
System.out.println("从网络获取的数据:" + result);
//解析数据
paserData(result);
}
@Override
public void onFailure(HttpException e, String msg) {
Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
});
}
/**
* 解析数据
* @param result
*/
private void paserData(String result) {
//使用第三方开源的框架GSON解析数据
Gson gson = new Gson();
newsData = gson.fromJson(result, NewsData.class);
//获取侧滑菜单并设置侧滑菜单数据
MainActivity mainActivity = (MainActivity) mActivity;
LeftMenuFragment leftMenuFragment =mainActivity.getLeftMenuFragment();
leftMenuFragment.setMenuData(newsData);
//准备4个菜单的详情页面
newsDetailPagerList = new ArrayList<>();
newsDetailPagerList.add(new NewsMenuDetailPager(mActivity,newsData.data.get(0).children));
newsDetailPagerList.add(new NewsSubjectDetailPager(mActivity));
newsDetailPagerList.add(new NewsPhotoDetailPager(mActivity));
newsDetailPagerList.add(new NewsInteractDetailPager(mActivity));
//设置默认新闻详情页面
setCurrentNewDetailPager(0);
}
/**
* 设置当前新闻详情页面
*/
public void setCurrentNewDetailPager(int position) {
//获取当前新闻详情页面
BaseNewsDetailPager newsCurrentDetailPager = newsDetailPagerList.get(position);
flContent.removeAllViews();//清除之前所有的布局
//添加到FrameLayout中
flContent.addView(newsCurrentDetailPager.rootView);
//设置当前页的标题
NewsData.NewsMenuData newsMenuData = newsData.data.get(position);
tvTitle.setText(newsMenuData.title);
newsCurrentDetailPager.initData();
}
}
NoScrollViewPager.java
package com.xbmu.wisdombj.view;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
/**
* 不能左右滑的ViewPager
* Created by Administrator on 2016/2/5 0005.
*/
public class NoScrollViewPager extends ViewPager{
public NoScrollViewPager(Context context) {
super(context);
}
public NoScrollViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 表示事件是否拦截,false不拦截
* @param ev
* @return
*/
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return false;
}
/**
* 重写onTouchEvent事件,什么都不用做
* @param ev
* @return
*/
@Override
public boolean onTouchEvent(MotionEvent ev) {
return false;
}
}
到目前为止智慧北京的app基本框架已经搭建完成。
全部代码: http://download.csdn.net/detail/btt2013/9428862
遇到的问题: