Fragment之间Bundle传值

package com.example.fragment39a;


import com.example.fragment39a.BookListFragment.Callbacks;


import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;


public class MainActivity extends FragmentActivity implements Callbacks {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


@Override
public void onItemSelected(int position) {
//获取到当前点击的Book对象
//动态加载详情Fragment(传递给右边Fragmnet)
BookContent.Book model=BookContent.list.get(position);

Bundle bundle=new Bundle();
bundle.putSerializable("data", model);

//通过setArguments给Fragment传递数据
BookDetailFragment f=new BookDetailFragment();
f.setArguments(bundle);

//加载详情fragment
FragmentManager manager=getSupportFragmentManager();
FragmentTransaction action=manager.beginTransaction();
action.replace(R.id.lin_content, f);
action.commit();



}
  
    

}




===============================================================================================

package com.example.fragment39a;


import com.example.fragment39a.BookContent.Book;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class BookDetailFragment extends Fragment{


private BookContent.Book model;


@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//接收传来的数据getArguments
Bundle bun=getArguments();
model = (Book) bun.get("data");


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.bookdetail, null);

TextView tv1=(TextView) v.findViewById(R.id.textView1);
TextView tv2=(TextView) v.findViewById(R.id.textView2);

if(model!=null){
tv1.setText(model.getBookTitle());
tv2.setText(model.getBookContent());
}

return v;
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值