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;
}


}

你可能感兴趣的:(Fragment之间Bundle传值)