android122 zhihuibeijing 新闻中心NewsCenterPager加载网络数据实现
新闻中心NewsCenterPager.java
package com.itheima.zhbj52.base.impl; import java.util.ArrayList; import android.app.Activity; import android.graphics.Color; import android.view.Gravity; import android.view.View; import android.widget.FrameLayout; import android.widget.ImageButton; import android.widget.TextView; import android.widget.Toast; import com.google.gson.Gson; import com.itheima.zhbj52.MainActivity; import com.itheima.zhbj52.R; import com.itheima.zhbj52.base.BaseMenuDetailPager; import com.itheima.zhbj52.base.BasePager; import com.itheima.zhbj52.base.menudetail.InteractMenuDetailPager; import com.itheima.zhbj52.base.menudetail.NewsMenuDetailPager; import com.itheima.zhbj52.base.menudetail.PhotoMenuDetailPager; import com.itheima.zhbj52.base.menudetail.TopicMenuDetailPager; import com.itheima.zhbj52.domain.NewsData; import com.itheima.zhbj52.domain.NewsData.NewsMenuData; import com.itheima.zhbj52.fragment.LeftMenuFragment; import com.itheima.zhbj52.global.GlobalContants; 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; /** * 新闻中心(前面可知,5个RadioButton所控制的ViewPager的xml类是NewsCenterPager的属性mRootView, * 这个类是用来给ViewPager的xml来填充xml数据的) */ public class NewsCenterPager extends BasePager { /*从BasePager继承的属性:(这个类的作用就是修改tvTitle,flContent,btnMenu的值) public View mRootView = View.inflate(mActivity, R.layout.base_pager, null);//这个mRootView是5个RadioButton所控制的ViewPager的xml类。 public TextView tvTitle = (TextView) mRootView.findViewById(R.id.tv_title);// 标题对象 public FrameLayout flContent = (FrameLayout) mRootView.findViewById(R.id.fl_content);// 下面的空布局 public ImageButton btnMenu = (ImageButton) mRootView.findViewById(R.id.btn_menu);// 三条红色的横线 */ private ArrayList<BaseMenuDetailPager> mPagers;// 4个菜单详情页的集合 private NewsData mNewsData; public NewsCenterPager(Activity activity) { super(activity); } @Override public void initData() { System.out.println("初始化新闻中心数据...."); tvTitle.setText("新闻"); setSlidingMenuEnable(true);// 打开侧边栏可以滑动出来。 getDataFromServer(); } /** * 从服务器获取数据 */ private void getDataFromServer() { HttpUtils utils = new HttpUtils(); // 使用xutils发送请求 utils.send(HttpMethod.GET, GlobalContants.CATEGORIES_URL, new RequestCallBack<String>() {//这里返回的是json字符串所以传String,如果是下载文件就要传File, // 访问成功 @Override public void onSuccess(ResponseInfo 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(); } }); } /** * 解析网络数据 ## Json ## {"name":"zhangsan", "age":18, "books":[ {"name":"wwww","price":10}, {"name":"wwww","price":20} ] } ## Gson ## google + json 谷歌提供的开源的解析json的框架 Gson gson = new Gson(); NewsData data = gson.fromJson(result, NewsData.class); */ protected void parseData(String result) { Gson gson = new Gson(); mNewsData = gson.fromJson(result, NewsData.class);//Gson将json解析成NewsData对象,Gson也可以将对象转成json, System.out.println("解析结果:" + mNewsData); // 刷新测边栏的数据(新闻中心RadioButton的侧边栏“新闻,专题,组图,互动”),侧边栏是根据主页面的不同而不同的。 MainActivity mainUi = (MainActivity) mActivity;//MainActivity LeftMenuFragment leftMenuFragment = mainUi.getLeftMenuFragment();//getLeftMenuFragment()是MainActivity的方法, leftMenuFragment.setMenuData(mNewsData); // 准备4个菜单详情页(下面其中之一"新闻中心"RadioButton点进去后的左侧栏的"新闻,专题,组图,互动页面"的右侧详情页面。) mPagers = new ArrayList<BaseMenuDetailPager>(); mPagers.add(new NewsMenuDetailPager(mActivity, mNewsData.data.get(0).children));//新闻中心-新闻 mPagers.add(new TopicMenuDetailPager(mActivity));//新闻中心-专题 mPagers.add(new PhotoMenuDetailPager(mActivity));//新闻中心-组图 /* BaseMenuDetailPager.java public abstract class BaseMenuDetailPager { public Activity mActivity; public View mRootView;// 根布局对象 public BaseMenuDetailPager(Activity activity) { mActivity = activity; mRootView = initViews(); } public abstract View initViews(); public void initData() { } } PhotoMenuDetailPager.java public class PhotoMenuDetailPager extends BaseMenuDetailPager { public PhotoMenuDetailPager(Activity activity) { super(activity); } @Override public View initViews() { TextView text = new TextView(mActivity); text.setText("菜单详情页-组图"); text.setTextColor(Color.RED); text.setTextSize(25); text.setGravity(Gravity.CENTER); return text; } }*/ mPagers.add(new InteractMenuDetailPager(mActivity));//新闻中心-互动 setCurrentMenuDetailPager(0);// 设置菜单详情页-新闻为默认当前页 } /** * 设置当前菜单详情页(修改5个RadioButton对应的ViewPager的xml数据) */ public void setCurrentMenuDetailPager(int position) { BaseMenuDetailPager pager = mPagers.get(position);// 获取当前要显示的菜单详情页 flContent.removeAllViews();// 清除之前的布局,flContent是BasePager的FrameLayout flContent属性,也是ViewPager的其中之一xml的FrameLayout(ViewPager的xml布局不一定要一样)。 flContent.addView(pager.mRootView);// 将菜单详情页的布局设置给帧布局,返回的是包含ViewPager控件的xml对应的View对象。View mRootView=View.inflate(mActivity, R.layout.news_menu_detail, null),news_menu_detail.xml是一个包含ViewPager的xml。或者仅仅返回一个View对象就可以。flContent是一个FrameLayout,里面加载的xml是新闻中心类的一个View属性,对该View属性的控制是在该类中完成的。 // 设置当前页的标题 NewsMenuData menuData = mNewsData.data.get(position); tvTitle.setText(menuData.title); pager.initData();// 初始化当前页面的数据 } }
网络数据类NewsData.java
package com.itheima.zhbj52.domain; import java.util.ArrayList; /** * 网络分类信息的封装 */ public class NewsData { //gson解析的时候retcode,data字段名字必须和服务器返回的json字段名一致, public int retcode; public ArrayList<NewsMenuData> data; // 侧边栏数据对象 public class NewsMenuData { //gson解析的时候id,title,type,url字段名字必须和服务器返回的字段名一致, public String id; public String title; public int type; public String url; public ArrayList<NewsTabData> children; @Override public String toString() { return "NewsMenuData [title=" + title + ", children=" + children + "]"; } } // 新闻页面下11个子页签的数据对象 public class NewsTabData { //gson解析的时候id,title,type,url字段名字必须和服务器返回的json字段名一致, public String id; public String title; public int type; public String url; @Override public String toString() { return "NewsTabData [title=" + title + "]"; } } @Override public String toString() { return "NewsData [data=" + data + "]"; } }
侧边栏(新闻中心RadioButton的侧边栏“新闻,专题,组图,互动”) LeftMenuFragment.java
package com.itheima.zhbj52.fragment; import java.util.ArrayList; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import com.itheima.zhbj52.MainActivity; import com.itheima.zhbj52.R; import com.itheima.zhbj52.base.impl.NewsCenterPager; import com.itheima.zhbj52.domain.NewsData; import com.itheima.zhbj52.domain.NewsData.NewsMenuData; import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu; import com.lidroid.xutils.ViewUtils; import com.lidroid.xutils.view.annotation.ViewInject; /** * 侧边栏(新闻中心RadioButton的侧边栏“新闻,专题,组图,互动”) */ public class LeftMenuFragment extends BaseFragment { @ViewInject(R.id.lv_list) private ListView lvList; private ArrayList<NewsMenuData> mMenuList; private int mCurrentPos;// 当前被点击的菜单项 private MenuAdapter mAdapter; @Override public View initViews() { View view = View.inflate(mActivity, R.layout.fragment_left_menu, null); //fragment_left_menu.xml /*<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" android:background="#000" > 侧边栏是一个List <ListView android:id="@+id/lv_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:listSelector="@android:color/transparent" android:divider="@android:color/transparent" listView的分割线为透明 android:layout_marginTop="40dp" /> </RelativeLayout>*/ ViewUtils.inject(this, view);//写这一句就支持注解了。 return view; } @Override public void initData() {//BaseFragment里面调用 lvList.setOnItemClickListener(new OnItemClickListener() {//ItemClick事件里面可以获取当前点击哪一个。 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mCurrentPos = position; mAdapter.notifyDataSetChanged();//调用Adapter的getView()方法,这时getView()方法执行的次数为listView的个数,从而设置是否显示红色和白色。 //getView()方法不仅仅是在初始化和滑动的时候调用,在其他需要的地方也可以调用。 setCurrentMenuDetailPager(position); toggleSlidingMenu();// 隐藏 } }); } /** * 切换SlidingMenu的状态,侧边栏隐藏和显示。 */ protected void toggleSlidingMenu() { MainActivity mainUi = (MainActivity) mActivity; SlidingMenu slidingMenu = mainUi.getSlidingMenu(); slidingMenu.toggle();// 切换状态, 显示时隐藏, 隐藏时显示 } /** * 设置当前菜单详情页,onItemClick触发。 */ protected void setCurrentMenuDetailPager(int position) { MainActivity mainUi = (MainActivity) mActivity; ContentFragment fragment = mainUi.getContentFragment();// 获取主页面fragment NewsCenterPager pager = fragment.getNewsCenterPager();// 获取新闻中心页面 pager.setCurrentMenuDetailPager(position);// 设置当前菜单详情页,新闻中心左边侧边栏点击后右边主页面显示不同的分类。 } // 设置网络数据 public void setMenuData(NewsData data) { // System.out.println("侧边栏拿到数据啦:" + data); mMenuList = data.data; mAdapter = new MenuAdapter(); lvList.setAdapter(mAdapter); } /** * 侧边栏数据适配器 */ class MenuAdapter extends BaseAdapter { @Override public int getCount() { return mMenuList.size(); } @Override public NewsMenuData getItem(int position) { return mMenuList.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.list_menu_item, null); //list_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="wrap_content" android:orientation="vertical" android:padding="10dp" > <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:drawableLeft="@drawable/btn_menu_selector" 新闻左边的小三角形图片选择器 android:drawablePadding="5dp" android:enabled="false" 默认不可使用, android:text="新闻" android:textColor="@drawable/text_menu_selector" 文字的颜色选择器 android:textSize="25sp" /> </LinearLayout> btn_menu_selector选择器: <?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> */ TextView tvTitle = (TextView) view.findViewById(R.id.tv_title); NewsMenuData newsMenuData = getItem(position); tvTitle.setText(newsMenuData.title); if (mCurrentPos == position) {// 判断当前绘制的view是否被选中 // 显示红色 tvTitle.setEnabled(true);//可以使用,然后根据颜色选择器判断显示的颜色。 } else { // 显示白色 tvTitle.setEnabled(false); } return view; } } }
侧边栏“新闻” NewsMenuDetailPager.java
package com.itheima.zhbj52.base.menudetail; 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 android.widget.TextView; import com.itheima.zhbj52.R; import com.itheima.zhbj52.base.BaseMenuDetailPager; import com.itheima.zhbj52.base.TabDetailPager; import com.itheima.zhbj52.domain.NewsData.NewsTabData; /** * 菜单详情页-新闻 */ public class NewsMenuDetailPager extends BaseMenuDetailPager { //BaseMenuDetailPager.java /*public abstract class BaseMenuDetailPager { public Activity mActivity; public View mRootView;// 根布局对象 public BaseMenuDetailPager(Activity activity) { mActivity = activity; mRootView = initViews(); } public abstract View initViews(); public void initData() { } }*/ /*继承的属性: public View mRootView = initViews(); */ private ViewPager mViewPager; private ArrayList<TabDetailPager> mPagerList; private ArrayList<NewsTabData> mNewsTabData;// 页签网络数据,北京,中国,国际,体育等11个页面 public NewsMenuDetailPager(Activity activity,ArrayList<NewsTabData> children) { super(activity); mNewsTabData = children; } @Override public View initViews() { View view = View.inflate(mActivity, R.layout.news_menu_detail, null); //news_menu_detail.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> */ mViewPager = (ViewPager) view.findViewById(R.id.vp_menu_detail);//修改view这个xml对象的属性 return view; } @Override public void initData() { mPagerList = new ArrayList<TabDetailPager>(); // 初始化页签数据(11个TabDetailPager是 北京,中国,国际,体育等页面) for (int i = 0; i < mNewsTabData.size(); i++) { TabDetailPager pager = new TabDetailPager(mActivity, mNewsTabData.get(i)); mPagerList.add(pager); } mViewPager.setAdapter(new MenuDetailAdapter()); } class MenuDetailAdapter extends PagerAdapter { @Override public int getCount() { return mPagerList.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public Object instantiateItem(ViewGroup container, int position) { TabDetailPager pager = mPagerList.get(position);//页签数据11个TabDetailPager container.addView(pager.mRootView);//mRootView=new TextView(mActivity),这里返回一个XMl的View对象。 pager.initData();//这里对XML的View对象进行数据的加载。 return pager.mRootView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } } }
页签详情页 TabDetailPager.java
package com.itheima.zhbj52.base; import com.itheima.zhbj52.domain.NewsData.NewsTabData; import android.app.Activity; import android.graphics.Color; import android.view.Gravity; import android.view.View; import android.widget.TextView; /** * 页签详情页 */ public class TabDetailPager extends BaseMenuDetailPager { /*public abstract class BaseMenuDetailPager { public Activity mActivity; public View mRootView;// 根布局对象,返回给上层ViewPager的xml的View对象。 public BaseMenuDetailPager(Activity activity) { mActivity = activity; mRootView = initViews(); } public abstract View initViews(); public void initData() { } }*/ NewsTabData mTabData; private TextView tvText; public TabDetailPager(Activity activity, NewsTabData newsTabData) { super(activity); mTabData = newsTabData; } @Override public View initViews() { tvText = new TextView(mActivity); tvText.setText("页签详情页"); tvText.setTextColor(Color.RED); tvText.setTextSize(25); tvText.setGravity(Gravity.CENTER); return tvText;//返回给上层的XML的View对象 } @Override public void initData() {//对返回给上层的XML的VIew对象的数据的加载 tvText.setText(mTabData.title); } }