Dynamically create instances of value transfer between Fragment, Activity and Fragment

I originally wanted to pass a value to the fragment through the constructor, but later I learned that Google does not allow the creation of a constructor with parameters for the fragment. So let's learn how to use setArguments() and getArguments() to pass values, and learn how to dynamically create frames.

First we need to do some prep work

1. Create a fragment as a template

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

}

Its corresponding layout:

<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. Add viewpager to the layout text of MainActivity

<?xml version="1.0" encoding="utf-8"?>
<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. Create an adapter for viewpager:

public class MyPagerAdapter extends FragmentPagerAdapter{

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

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

    @Override
    public int getCount() {
        return fragmentList.size();
    }
}
Let's start to enter the topic: modify the code in MainActivity to create fragments and send values:
public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private MyPagerAdapter pagerAdapter;
    private List<Fragment> 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);


    }
}
Finally receive in 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;
    }

}
Effect display:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324951766&siteId=291194637