天天看點

161.n1-解析json中的資料傳遞給左邊的側邊欄

左側邊欄和ViewPager中的額news沒有什麼互動,如果要傳遞資料可以通過MainActivity作為中介來傳遞資料,首先MainActivity要擷取到左側邊欄的對象,

//擷取到左側邊欄的對象,娶一個别名
	public LeftMenuFragment getLeftMenuFragment(){
		FragmentManager fm = getSupportFragmentManager();
		LeftMenuFragment leftMenuFragment = (LeftMenuFragment) fm.findFragmentByTag(FRAGMENT_LEFT_MENU);
		
		return leftMenuFragment;
		
	}
           

然後newPager建立MainActivity對象擷取到左側邊欄的對象,然後之前解析的資料傳遞給左側邊欄

/ 重新整理測邊欄的資料,利用MainActivity擷取到左側邊欄的資料
		MainActivity mainUi = (MainActivity) mActivity;
		//擷取到左側邊欄的對象
		LeftMenuFragment leftMenuFragment = mainUi.getLeftMenuFragment();
		//設定網絡資料
		leftMenuFragment.setMenuData(mNewsData);
           

MainActivity.java邏輯檔案,建立方法擷取到左側邊欄的對象

package com.ldw.news;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Window;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.ldw.news.fragment.ContentFragment;
import com.ldw.news.fragment.LeftMenuFragment;

/*
 * 主界面
 */
public class MainActivity extends SlidingFragmentActivity {

	private static final String FRAGMENT_LEFT_MENU = "fragment_left_menu";
	private static final String FRAGMENT_CONTENT = "fragment_content";
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
        initView();
	}

	private void initView() {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
        
		
		//設定左側邊欄
        setBehindContentView(R.layout.left_menu);
        //擷取側邊欄對象
        SlidingMenu slidingMenu = getSlidingMenu();
        //設定全屏觸摸
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        //設定預留螢幕的寬度
        slidingMenu.setBehindOffset(200);
        
        initFragment();
        
		
	}
	
	/*
	 * 初始化fragment,将fragment填充給布局檔案
	 */
	private void initFragment() {
		FragmentManager fm = getSupportFragmentManager();
		FragmentTransaction transaction = fm.beginTransaction();// 開啟事務
		//左側邊欄用布局替代(填充)
		//第三個參數就是給fragment取一個名字,可以使用fm.findFragmentByTag(FRAGMENT_LEFT_MENU);
		transaction.replace(R.id.fl_left_menu, new LeftMenuFragment(),
				FRAGMENT_LEFT_MENU);// 用fragment替換framelayout
		//内容的填充
		transaction.replace(R.id.fl_content, new ContentFragment(),
				FRAGMENT_CONTENT);

		transaction.commit();// 送出事務
		//使用第三個參數
		// Fragment leftMenuFragment = fm.findFragmentByTag(FRAGMENT_LEFT_MENU);
	}
	
	//擷取到左側邊欄的對象,娶一個别名
	public LeftMenuFragment getLeftMenuFragment(){
		FragmentManager fm = getSupportFragmentManager();
		LeftMenuFragment leftMenuFragment = (LeftMenuFragment) fm.findFragmentByTag(FRAGMENT_LEFT_MENU);
		
		return leftMenuFragment;
		
	}

}
           

左側邊欄的布局,是一個ListView.fragmen_left_menu.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:background="#000000"
    android:orientation="vertical" >
    
	<!-- 側邊欄 -->
	<ListView 
	    android:id="@+id/lv_list"
	    android:layout_width="match_parent"
    	android:layout_height="wrap_content"
	    />
</LinearLayout>
           

左側邊欄的單個條目

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="match_parent"
    android:orientation="vertical" >
    
	<!-- 左側菜單單個條目的布局 -->
	<TextView 
	    android:id="@+id/tv_title"
	    android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:textColor="#ff0000"
    	android:textSize="22sp"
    	android:text="新聞"
    	android:layout_gravity="center"
    	android:drawablePadding="5dp"
    	android:drawableLeft="@drawable/menu_arr_select"
	    />
</LinearLayout>
           

左側邊欄的邏輯檔案LeftMenuFragment.java

package com.ldw.news.fragment;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.ldw.news.R;
import com.ldw.news.domain.NewsData;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

/*
 * 左側邊欄的内容
 */
public class LeftMenuFragment extends BaseFragment{
	@ViewInject(R.id.lv_list)
	private ListView lv_list;
	//初始化布局
	@Override
	public View initViews() {
		View view = View.inflate(mActivity, R.layout.fragment_left_menu, null);
		ViewUtils.inject(this, view);
		return view;
	}
	
	public void initData(){
		
	}
	
	//設定網絡資料
	public void setMenuData(NewsData data){
		System.out.println("左側的資料是:" + data);
	}
	
	class MenuAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = View.inflate(mActivity, R.layout.list_menu_item, null);
			TextView tv_title = (TextView)view.findViewById(R.id.tv_title);	
			return view;
		}
		
	}

}
           

news的pager先解析資料然後吧資料傳遞給左邊的菜單欄NewsCenterPager.java

package com.ldw.news.base.impl;

import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.ldw.news.MainActivity;
import com.ldw.news.base.BasePager;
import com.ldw.news.domain.NewsData;
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 {

	//private ArrayList<BaseMenuDetailPager> mPagers;// 4個菜單詳情頁的集合
	private NewsData mNewsData;
	
	public NewsCenterPager(Activity activity) {
		super(activity);
	}

	@Override
	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);
		//從伺服器中擷取資料
		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);
	}

}
           

Json解析的資料NewsData.java

package com.ldw.news.domain;

import java.util.ArrayList;

/*
 * 網絡分類資訊的封裝
 * 各個字段的名名字需要一直,友善Gson解析
 */
public class NewsData {

	public int retcode;//傳回值
	public ArrayList<NewsMenuData> data;//字段資料
	
	//data下面又有一組4個資料
	// 側邊欄資料對象
	public class NewsMenuData {
		public String id;
		public String title;
		public int type;
		public String url;
		//解析children的資料
		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 + "]";
		}
}