智慧北京第二天

整体框架



1.ContentFragment 布局 (一个ViewPager 和RadioButton)  style风格自定义包括宽高颜色大小

<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" >

    <com.example.newbj.view.NoScrollViewPager
        android:id="@+id/vp_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
//必须设置id不然不会显示按下状态和没有按下状态
    <RadioGroup
        android:id="@+id/rg_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bottom_tab_bg"
        android:orientation="horizontal" >
        <RadioButton
            android:id="@+id/rb_home"
            style="@style/BottomTabStyle"
            android:drawableTop="@drawable/btn_tab_home_selector"
            android:text="首页" />
        <RadioButton
            android:id="@+id/rb_news"
            style="@style/BottomTabStyle"
            android:drawableTop="@drawable/btn_tab_news_selector"
            android:text="新闻中心" />
        <RadioButton
            android:id="@+id/rb_smart"//
            style="@style/BottomTabStyle"
            android:drawableTop="@drawable/btn_tab_smart_selector"
            android:text="智慧服务" />
        <RadioButton
            android:id="@+id/rb_gov"
            style="@style/BottomTabStyle"
            android:drawableTop="@drawable/btn_tab_gov_selector"
            android:text="政务" />
        <RadioButton
            android:id="@+id/rb_setting"
            style="@style/BottomTabStyle"
            android:drawableTop="@drawable/btn_tab_setting_selector"
            android:text="设置" />
    </RadioGroup>

</LinearLayout>








2.VIewPager里面设置一个Realative 和一个 FrameLayout 




3.5个子页面的基类设置 BasePager

public class BasePager {

	public Activity mActivity;//上下文MainActivity
	public View mRootview;//视图

	public TextView tvTitle;// 标题对象

	public FrameLayout flContent;// 内容

	public ImageButton btnMenu;// 菜单按钮
	private MainActivity mainUi;

	public BasePager(Activity activity) {
		mActivity = activity;
		initView();
	}

	public View initView() {
		mRootview = View.inflate(mActivity, R.layout.base_pager, null);

		tvTitle = (TextView) mRootview.findViewById(R.id.tv_title);
		flContent = (FrameLayout) mRootview.findViewById(R.id.fl_content);
		btnMenu = (ImageButton) mRootview.findViewById(R.id.btn_menu);
		btnMenu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				toggleSlideMenu();//开关侧边栏
			}
		});
		return mRootview;
	}

	public void initData() {

	};
	
	/**
	 * 设置侧边栏是否可以被打开.首页和设置不能打开
	 * 由子类调用
	 * 
	 * @param enable
	 */
	public void setSlidingMenuEnable(boolean enable) {
		mainUi = (MainActivity) mActivity;

		SlidingMenu slidingMenu = mainUi.getSlidingMenu();

		if (enable) {
			slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		} else {
			slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
		}
	}
	
	/**是否隐藏左侧菜单页*/
	protected void toggleSlideMenu() {
		SlidingMenu slidingMenu = mainUi.getSlidingMenu();
		slidingMenu.toggle();//显示时隐藏 .隐藏时显示
	}
}

4.BasePager 子类设置 HomePager .Govaffair. SmartService.SettingPager  以及最主要的NewCenterPager页面


public class HomePager extends BasePager{
	public HomePager(Activity activity) {
		super(activity);
	}
	public void initData() {
		System.out.println("初始化首页数据....");
		
		tvTitle.setText("智慧北京");// 修改标题
		btnMenu.setVisibility(View.GONE);// 隐藏菜单按钮
		setSlidingMenuEnable(false);//关闭侧边栏

		TextView text = new TextView(mActivity);
		text.setText("首页");
		text.setTextColor(Color.RED);
		text.setTextSize(25);
		text.setGravity(Gravity.CENTER);

		// 向FrameLayout中动态添加布局
		flContent.addView(text);
	};
	
	

}



5.ContentFragment对5个页面 进行设置


public class ContentFragment extends BaseFragment {

	@ViewInject(R.id.rg_group)
	private RadioGroup rgGroup;

	@ViewInject(R.id.vp_content)
	private ViewPager mViewPager;

	private List<BasePager> pageList;

	@Override
	public View initView() {
		View view = View.inflate(mActivity, R.layout.fragment_content, null);
		ViewUtils.inject(this, view);// 注入view和事件
		return view;
	}

	@Override
	public void initData() {
		rgGroup.check(R.id.rb_home);// 默认勾选首页

		pageList = new ArrayList<BasePager>();
		// for (int i = 0; i < 5; i++) {
		// pageList.add(new BasePager(mActivity));
		// }
		//5个子页面
		pageList.add(new HomePager(mActivity));
		pageList.add(new NewCenterPager(mActivity));
		pageList.add(new SmartService(mActivity));
		pageList.add(new Govaffair(mActivity));
		pageList.add(new SettingPager(mActivity));

		ContentAdapter contentAdapter = new ContentAdapter();
		mViewPager.setAdapter(contentAdapter);
		//当viewpager页面切换时加载数据
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			 
			@Override
			public void onPageSelected(int position) {
				//选择页面加载数据
				pageList.get(position).initData();
			}
			
			@Override
			public void onPageScrolled(int position, float positionOffset,
					int positionOffsetPixels) {
				
			}
			
			@Override
			public void onPageScrollStateChanged(int state) {
				
			}
		});
		
		pageList.get(0).initData();//默认加载第一页的数据

		//点击radiobutton切换viewPager
		rgGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rb_home:
					// mViewPager.setCurrentItem(0);// 设置当前页面
					mViewPager.setCurrentItem(0, false);// 去掉切换页面的动画
					break;
				case R.id.rb_news:
					mViewPager.setCurrentItem(1, false);// 设置当前页面
					break;
				case R.id.rb_smart:
					mViewPager.setCurrentItem(2, false);// 设置当前页面
					break;
				case R.id.rb_gov:
					mViewPager.setCurrentItem(3, false);// 设置当前页面
					break;
				case R.id.rb_setting:
					mViewPager.setCurrentItem(4, false);// 设置当前页面
					break;

				default:
					break;
				}
			}
		});
	}

	class ContentAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return pageList.size();
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			BasePager basePager = pageList.get(position);
			container.addView(basePager.mRootview);
			//basePager.initData();会预加载下一个页面,设置监听。当用户点击的时候才调用数据
			return basePager.mRootview;
		}

	}
	
	//返回NewCenterPager对象
	public NewCenterPager getNewsCenterPager() {
		NewCenterPager newCenterPager = (NewCenterPager) pageList.get(1);
		return newCenterPager;
	}
}


6.利用google的Gson对json进行解析



  定义gson的javabean 类

public class NewsData {
	//定义的名字必须和服务器返回的名字一致
	public int retcode;
	public ArrayList<NewsMenuData> data;
	//public ArrayList extend;不需要这个
	
	//侧边栏的数据对象
	public class NewsMenuData{
		
		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{
		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 + "]";
	}
}

7.新闻中心页面加载数据,解析数据


public class NewCenterPager extends BasePager{
	private LeftMenuFragment leftfragment;
	private List<BaseMenuDetailPager> baseMenuList;
	private NewsData mNewsData;
	private int position = 0;

	public NewCenterPager(Activity activity) {
		super(activity);
	}
	public void initData() {
		
		setSlidingMenuEnable(true);//关闭侧边栏

		getDataFromServer();
		
	};
	  /**从服务器获取数据*/
	public void getDataFromServer(){
		HttpUtils httpUtils = new HttpUtils();
		httpUtils.send(HttpMethod.GET, GlobalContants.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 mainUi = (MainActivity)mActivity;
		leftfragment = mainUi.getLeftFragment();
		leftfragment.setMenuData(mNewsData);
		
		baseMenuList = new ArrayList<BaseMenuDetailPager>();//顺便将数据传给NewsMenuDetailPager
		baseMenuList.add(new NewsMenuDetailPager(mActivity,mNewsData.data.get(0).children));
		baseMenuList.add(new TopicMenuDetailPager(mActivity));
		baseMenuList.add(new PhotoMenuDetailPager(mActivity));
		baseMenuList.add(new InteractMenuDetailPager(mActivity));
		
		setCurrentMenuDetailPager(position);//设置详情页默认
	}
	
	//点击菜单选择新闻详情页的4个页面(由侧边栏调用)
	public void setCurrentMenuDetailPager(int position){
		this.position = position;
		BaseMenuDetailPager baseMenuDetailPager = baseMenuList.get(position);
		flContent.removeAllViews();//清除所有内容.因为是FrameLayout 不然都会叠加到一起
		flContent.addView(baseMenuDetailPager.mRootView);
		
		//设置标题
		NewsMenuData newsMenuData = mNewsData.data.get(position);
		tvTitle.setText(newsMenuData.title);
		
		baseMenuDetailPager.initData();//当点击的时候才去调用11个页签页的数据
	}

}


8.点击侧边栏切换页面


//侧边栏页面
public class LeftMenuFragment extends BaseFragment {
	@ViewInject(R.id.lv_list)
	private ListView lvList;
	private ArrayList<NewsMenuData> mMenuList;
	private Myadapter adapter;
	private int currentPosition;
	private MainActivity mainUi;

	@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() {

		// 记录选中的位置,刷新listView
		lvList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				currentPosition = position;
				adapter.notifyDataSetChanged();

				setNewsMenuDetailPager(position);//点击按钮切换页面

				toggleSlideMenu();//关闭侧边栏
			}
		});
	}

	class Myadapter extends BaseAdapter {

		@Override
		public int getCount() {
			return mMenuList.size();
		}

		@Override
		public Object 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) {

			NewsMenuData newsMenuData = mMenuList.get(position);
			View view = View.inflate(mActivity, R.layout.list_menu_item, null);
			TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
			tvTitle.setText(newsMenuData.title);
			if (currentPosition == position) {// 判断当前绘制的view是否被选中
				// 显示红色
				tvTitle.setEnabled(true);
			} else {
				// 显示白色
				tvTitle.setEnabled(false);
			}

			return view;
		}

	}

	/** 设置侧边栏数据 */
	public void setMenuData(NewsData data) {
		System.out.println("侧边栏拿到数据了" + data);
		mMenuList = data.data;

		adapter = new Myadapter();
		lvList.setAdapter(adapter);
	}

	/** 点击侧边栏按钮设置新闻详情页的4个页面 */
	public void setNewsMenuDetailPager(int position) {
		mainUi = (MainActivity) mActivity;
		ContentFragment contentFragment = mainUi.getContentFragment();// 再获取到COntentFragment对象
		NewCenterPager newsCenterPager = contentFragment.getNewsCenterPager();// 再获取新闻中心页的对象
		newsCenterPager.setCurrentMenuDetailPager(position);// 调用新闻中心页的切换页面方法
	}

	/**是否隐藏左侧菜单页*/
	protected void toggleSlideMenu() {
		SlidingMenu slidingMenu = mainUi.getSlidingMenu();
		slidingMenu.toggle();//显示时隐藏 .隐藏时显示
	}
}


9.新闻中心页面 点击菜单栏的4个子页面 (新闻。专题。主题。互动)的基类

public abstract class BaseMenuDetailPager {
	public Activity mActivity;
	public View mRootView;
	public BaseMenuDetailPager(Activity activity){
		mActivity = activity;
		mRootView = initView();
	}
	public abstract View initView();
	
	public void initData(){
		
	}
}

   子类:
public class InteractMenuDetailPager extends BaseMenuDetailPager{

	public InteractMenuDetailPager(Activity activity) {
		super(activity);
	}
	@Override
	public View initView() {
		TextView text = new TextView(mActivity);
		text.setText("菜单详情页-互动");
		text.setTextColor(Color.RED);
		text.setTextSize(25);
		text.setGravity(Gravity.CENTER);
		return text;
	}
}


10.   11个页签详情页  



  基类一样基础自
BaseMenuDetailPager 

public class TabDetailPager extends BaseMenuDetailPager {

	private TextView tvText;
	NewsTabData mNewsTabData;

	public TabDetailPager(Activity activity, NewsTabData newsTabData) {
		super(activity);
		mNewsTabData = newsTabData;
	}

	@Override
	public View initView() {
		tvText = new TextView(mActivity);
		tvText.setText("页签详情页");
		tvText.setTextColor(Color.RED);
		tvText.setTextSize(25);
		tvText.setGravity(Gravity.CENTER);
		return tvText;
	}

	@Override
	public void initData() {
		super.initData();
		tvText.setText(mNewsTabData.title);
		System.out.println(mNewsTabData.title);
	}
}


11.第二天  类的框架 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值