Android中Activity传递参数至Fragment中

来源:互联网 发布:arm linux gcc 4.8.3 编辑:程序博客网 时间:2024/04/30 06:10

这里写链接内容 Fragment现在在项目中已经很常见了,一般都是Activity管理Fragment或者Activity中嵌套Fragment使用,常见的ViewPager左右滑动。我在项目中遇到一个问题就是有一个Activity页面,但Activity页面中有4个Fragment页面需要展示,4个Fragment页面是同个接口,但是不同的参数,4个Fragment可以切换,开始不会做,纠结了半天,做出来了,哈哈,记录一下,
效果图:
这里写图片描述
其实也很简单,就是一个Activity页面里面放了4个按钮,分别显示4个Fragment页面,上代码:
MainActivity

public class MainActivity extends FragmentActivity implements OnClickListener {    private Button btn1, btn2, btn3, btn4;    private FragmentManager manager; // manager    private FragmentTransaction transaction; // 事物    /**     * fragment     */    private Fragment1 fragment1;    private Fragment2 fragment2;    private Fragment3 fragment3;    private Fragment4 fragment4;    //参数    String userIds ;    String userNames;    Bundle bundle;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        findViews();        /**         * 参数赋值         */        userIds = "abc123";        userNames = "admin";        manager = getSupportFragmentManager();              setTabSelection(0); // 设置第一个    }    private void findViews() {        // TODO Auto-generated method stub        btn1 = (Button) findViewById(R.id.btn1);        btn2 = (Button) findViewById(R.id.btn2);        btn3 = (Button) findViewById(R.id.btn3);        btn4 = (Button) findViewById(R.id.btn4);        btn1.setOnClickListener(this);        btn2.setOnClickListener(this);        btn3.setOnClickListener(this);        btn4.setOnClickListener(this);    }    @Override    public void onClick(View v) {        // TODO Auto-generated method stub        switch (v.getId()) {        case R.id.btn1:            setTabSelection(0);            break;        case R.id.btn2:            setTabSelection(1);            break;        case R.id.btn3:            setTabSelection(2);            break;        case R.id.btn4:            setTabSelection(3);            break;        }    }    private void setTabSelection(int index) {        // TODO Auto-generated method stub        // 开启一个事物        transaction = manager.beginTransaction();        // 隐藏掉所有的Fragment        hideFragment(transaction);        switch (index) {        case 0:            if (fragment1 == null) {            /**             * 通过Bundle传递参数             */                bundle = new Bundle();                bundle.putString("userIds", userIds);                bundle.putString("userNames", userNames);                fragment1 = new Fragment1();                fragment1.setArguments(bundle);                transaction.add(R.id.content, fragment1, "TAG");            } else {                transaction.show(fragment1);            }            break;        case 1:            if (fragment2 == null) {                bundle = new Bundle();                bundle.putString("userIds", userIds);                bundle.putString("userNames", "123");                fragment2 = new Fragment2();                fragment2.setArguments(bundle);                transaction.add(R.id.content, fragment2, "TAG");            } else {                transaction.show(fragment2);            }            break;        case 2:            if (fragment3 == null) {                bundle = new Bundle();                bundle.putString("userIds", "123");                bundle.putString("userNames", "admin");                fragment3 = new Fragment3();                fragment3.setArguments(bundle);                transaction.add(R.id.content, fragment3, "TAG");            } else {                transaction.show(fragment3);            }            break;        case 3:        default:            if (fragment4 == null) {                bundle = new Bundle();                bundle.putString("userIds", "userid");                bundle.putString("userNames", "123");                fragment4 = new Fragment4();                fragment4.setArguments(bundle);                transaction.add(R.id.content, fragment4, "TAG");            } else {                transaction.show(fragment4);            }            break;        }        transaction.commit(); // 提交    }    private void hideFragment(FragmentTransaction transaction) {        // TODO Auto-generated method stub        if (fragment1 != null) {            transaction.hide(fragment1);        }        if (fragment2 != null) {            transaction.hide(fragment2);        }        if (fragment3 != null) {            transaction.hide(fragment3);        }        if (fragment4 != null) {            transaction.hide(fragment4);        }    }}

代码就是这样,通过Bundle传递,bundle.put(“key”,”value”);设置参数,最后目标fragment.setArguments(bundle);设置进去

 fragment4 = new Fragment4(); bundle = new Bundle(); bundle.putString("userIds", "userid"); bundle.putString("userNames", "123"); fragment4.setArguments(bundle); transaction.add(R.id.content, fragment4, "TAG"); 

可以看这一块,最后transaction.add(R.id.content, fragment4, “TAG”);
这里给设置了一个TAG,后面接收的时候用。Activity里面的代码就是这样,下面就是Fragment接收参数了
Fragment代码:

public class Fragment1 extends Fragment {    private MainActivity mMainActivity;    View fragment1_layout;    private TextView tv1;    @Override    @Nullable    public View onCreateView(LayoutInflater inflater,            ViewGroup container,Bundle savedInstanceState) {        // TODO Auto-generated method stub        fragment1_layout =   inflater.inflate(R.layout.fragment1, null);        mMainActivity = (MainActivity) getActivity();        String userId = getFragmentManager().findFragmentByTag("TAG").getArguments().getString("userIds").trim();        String userName = getFragmentManager().findFragmentByTag("TAG").getArguments().getString("userNames").trim();        tv1 = (TextView) fragment1_layout.findViewById(R.id.tv1);        tv1.setText("userId:"+userId + "\n"+"\n"+"userName:"+userName);        return fragment1_layout;    }}

fragment接收参数getFragmentManager().findFragmentByTag(“TAG”).getArguments().getString(“userIds”);获取参数,后面的就不介绍了,根据自己的需求。Fragment中的布局也比较简单,就一个用于显示的TextView。

哈哈,文笔太菜,有错误之处,敬请指教。

代码下载

0 0