动态创建Fragment、Activity和Fragment之间传值

本来想通过构造方法给fragment传值,不过后来知道,google是不予许给frament创建有参数的构造方法的。那么下面我们就来学习如何使用setArguments()和getArguments()来实现传值吧,同时学习怎样动态的创建frament。

首先我们需要做一些准备工作

1、创建一个fragment作为模板

public class BlankFragment extends Fragment {

    private TextView textView;


    public BlankFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_blank, container, false);


        return view;
    }

}

其对应的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.fragmentdemo.BlankFragment">


    <TextView
        android:id="@+id/fragment_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="50sp"
        android:text="数字" />

LinearLayout>

2、在MainActivity的布局文中添加viewpager


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.fragmentdemo.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/main_vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    android.support.v4.view.ViewPager>

LinearLayout>

3、创建viewpager的适配器:

public class MyPagerAdapter extends FragmentPagerAdapter{

    private List fragmentList;
    public MyPagerAdapter(FragmentManager fm,List fragmentList) {
        super(fm);
        this.fragmentList = fragmentList;
    }

    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }
}
下面我们开始进入主题:修改MainActivity中的代码,实现创建fragment和发送值:
public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private MyPagerAdapter pagerAdapter;
    private List fragmentList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = findViewById(R.id.main_vp);

        for(int i =0;i<3;i++){
            //创建Fragment对象
            BlankFragment blankFragment = new BlankFragment();
            //创建Bundle对象
            Bundle bundle = new Bundle();
            //将想要传给fragment的值通过键值对放入Bundle对象中
            bundle.putInt("number",i);
            //通过setArguments()发送
            blankFragment.setArguments(bundle);
            //最后将Fragment对象放入集和中
            fragmentList.add(blankFragment);
        }

        pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),fragmentList);
        viewPager.setAdapter(pagerAdapter);


    }
}
最后在fragment中接收:
public class BlankFragment extends Fragment {

    private TextView textView;


    public BlankFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_blank, container, false);

        textView = view.findViewById(R.id.fragment_tv);
        //通过getArguments()获得Bundle对象
        Bundle bundle = getArguments();
        //通过键获得值
        int number = bundle.getInt("number");
        textView.setText(number+"");
        return view;
    }

}
效果展现:

动态创建Fragment、Activity和Fragment之间传值_第1张图片

你可能感兴趣的:(Android,Fragment)