Activity如何传递数据给Fragment——回调方法

【声明】转载请注明出处,此文出自指尖飞落的博客:http://blog.csdn.net/huntersnail

——尊重作者,知识无价,交流无限!


需求:Activity传递数据给Fragment

问题:界面之间传递数据在开发中是经常遇到的一种情况,如果是Actvity之间传递咱们可以用Intent,非常方便。但是,Activity传递数据给Fragment还能用Intent吗?答案是显然是否定的,Fragment不能用Intent,所以,咱们该如何传递数据呢?我采用的是回调的方法。

思路:①在Fragment创建一个方法,且为setData(OrderDetail data)

  ②在Activity中实例化该Fragment,通过该实例调用setData()方法,并将数据传进去。


网上看了一些关于Java回调函数的解释,下面贴出来给大家看看。有兴趣可以去看看点击打开链接

首先说说什么叫回调函数(网上查询了一些资料,对它的解说还是比较全面,但是很难理解,特别是在JAVA当中实现回调函数,不理解什么叫回调函数的,就更难实现了),以下介绍根据网上的资料整合而成。
    在WINDOWS中,程序员想让系统DLL调用自己编写的一个方法,于是利用DLL当中回调函数(CALLBACK)的接口来编写程序,使它调用,这个就称为回调。在调用接口时,需要严格的按照定义的参数和方法调用,并且需要处理函数的异步,否则会导致程序的崩溃。这样的解释似乎还是比较难懂,这里举个简单的例子,程序员A写了一段程序(程序a),其中预留有回调函数接口,并封装好了该程序。程序员B要让a调用自己的程序b中的一个方法,于是,他通过a中的接口回调自己b中的方法。目的达到。在C/C++中,要用回调函数,被掉函数需要告诉调用者自己的指针地址,但在JAVA中没有指针,怎么办?我们可以通过接口(interface)来实现定义回调函数。

不废话了,直接贴代码,看红色关键代码。

1、Activity

public class OrderDetailsActivity extends BaseActivity{
	private static final String TAG = "订单ID";
    private ViewPager mPager;
    private ArrayList fragmentsList;
    private ImageView ivBottomLine;
    private TextView tvInfo, tvGood;

    private int currIndex = 0;
    private int bottomLineWidth;
    private int offset = 0;
    private int position_one;
    private int position_two;
    private String OrderId;
	private FzHttpRequest mRequest;
	public OrderInfoFragment mOrderInfoFragment;
	private OrderGoodsListFragment mOrderGoodsListFragment;
	public OrderDetail mOrderDetail;
	private TextView mTitle;
	private Resources resources;
	public String getOrderId() {
		return OrderId;
	}

	public void setOrderId(String orderId) {
		OrderId = orderId;
	}

	@Override
	protected void fzOnCreate(Bundle arg0) {
		setContentView(R.layout.activity_order_detail);
		mTitle=(TextView) findViewById(R.id.tv_title);//标题
		mTitle.setText("订单详情");
		resources=getResources();
		mRequest = FzHttpRequest.getInstance();
        InitViewPager();
        InitTextView();
        InitWidth();
        InitData();
	}

	@SuppressWarnings("deprecation")
	public void InitData() {
		//获得订单ID
        OrderId=getIntent().getStringExtra("ORDERID");
		AjaxParams params = new AjaxParams();
		params.put("code", MyApplication.getInstance().getCode());
		params.put("SupplierID", MyApplication.getInstance()
				.getCurrentSupplierid());
		params.put("orderId", getIntent().getStringExtra("ORDERID"));
		System.out.println(getIntent().getStringExtra("ORDERID"));
		mRequest.post(Config.GETORDERLIST, params, new SimpleFzHttpListener(
				OrderDetailsActivity.this) {
			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				mOrderDetail = (OrderDetail) t;
				if (mOrderDetail != null) {
					Log.i(TAG+"DATA", mOrderDetail.toString());
					if (mOrderInfoFragment!=null) {
						mOrderInfoFragment.setData(mOrderDetail);
					}
					if (mOrderGoodsListFragment!=null) {
						mOrderGoodsListFragment.setData(mOrderDetail.getOrderItemsIList());
					}
					Log.i(TAG, mOrderDetail.toString());
					
				} else {
					ToastUtils.showLongToast("未赋值!");
				}
			}
			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				ToastUtils.showLongToast(strMsg);
			}
		},OrderDetail.class);
	}
private void InitTextView() {
		tvInfo = (TextView) findViewById(R.id.tv_tab_info);
		tvGood = (TextView) findViewById(R.id.tv_tab_good);
		tvInfo.setTextColor(resources.getColor(R.color.lightwhite));
		tvInfo.setOnClickListener(new MyOnClickListener(0));
		tvGood.setOnClickListener(new MyOnClickListener(1));
		
	}

	private void InitViewPager() {
		mPager = (ViewPager) findViewById(R.id.vPager);
        fragmentsList = new ArrayList();

        mOrderInfoFragment = new OrderInfoFragment();
        mOrderGoodsListFragment =new OrderGoodsListFragment();

        fragmentsList.add(mOrderInfoFragment);
        fragmentsList.add(mOrderGoodsListFragment);

        
        mPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentsList));
        mPager.setCurrentItem(0);
        mPager.setOnPageChangeListener(new MyOnPageChangeListener());
	}

2、Fragment

/**
 * 订单的详细信息界面(不包括商品信息)
 * 
 * @author Endoon 2015-6-12 11:32:59
 */
public class OrderInfoFragment extends BaseFragment {
	private TextView mOrderId;

	private TextView mOrderTime;

	private TextView mOrderUsername;

	private TextView mOrderAdress;

	private TextView mOrderMoney;

	private TextView mOrderPhone;
	public OrderInfoFragment infoFragment;
	public OrderDetail mData;
	
	public OrderInfoFragment() {
		super();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	protected View inflaterView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {
		return arg0.inflate(R.layout.fragment_order_good, null);
		
	}

	@Override
	protected void initView(View view) {
		super.initView(view);
		mOrderId = (TextView) view.findViewById(R.id.tv_order_detail_id);
		mOrderTime = (TextView) view.findViewById(R.id.tv_order_detail_time);
		mOrderUsername = (TextView) view.findViewById(R.id.tv_order_detail_username);
		mOrderAdress = (TextView) view.findViewById(R.id.tv_order_detail_address);
		mOrderMoney = (TextView) view.findViewById(R.id.tv_order_detail_money);
		mOrderPhone = (TextView) view.findViewById(R.id.tv_order_detail_phone);
		
	}
	
	public void setData(OrderDetail data)
	{
		this.mData = data;
		if (mData!=null)
		{
			mOrderId.setText(mData.getOrderId());
			
			mOrderUsername.setText(mData.getUsername());
			
			String orderTime=getDateToStr(mData.getOrderDate(), "yyyy-MM-dd HH:mm");
			mOrderTime.setText(orderTime);
			
			mOrderAdress.setText(mData.getAddress());
			mOrderMoney.setText("¥"+mData.getOrderTotal()+"(运费 3 元)");
			mOrderPhone.setText(mData.getCellPhone());
			
			mOrderPhone.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// 调到拨号界面
					Uri uri = Uri.parse("tel:"+mData.getCellPhone());
					Intent intent = new Intent(Intent.ACTION_CALL, uri);
					startActivity(intent);
				}
			});
		}
	}
	
	private String getDateToStr(String str, String format) {
		try {
			str = str.replace("/Date(", "").replace(")/", "");
			Log.i("try", str);

			SimpleDateFormat _format = new SimpleDateFormat(format);

			Long time = new Long(str);
			String d = _format.format(time);
			Date date = _format.parse(d);

			return _format.format(date);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

}






Activity如何传递数据给Fragment——回调方法_第1张图片 Activity如何传递数据给Fragment——回调方法_第2张图片


O(∩_∩)O哈哈~欢迎各种吐槽、鄙视、指教、交流......

☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆转载请注明出处☞指尖飞落的博客☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆






你可能感兴趣的:(☞Android☞)