美文网首页
Android Fragment传递数据以及跳转

Android Fragment传递数据以及跳转

作者: JacksonMrwang | 来源:发表于2019-04-19 10:25 被阅读0次

功能实现建立三个点击事件对三个碎片进行切换,并且在三个碎片之中建立两个点击事件可以实现Fragment与Fragment相互传递数据并跳转。

代码如下:

Main3Activity 
   private Button button1;
    private Button button2;
    private Button button3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        button1=findViewById(R.id.ButtonA);
        button2=findViewById(R.id.ButtonB);
        button3=findViewById(R.id.ButtonC);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment=new OneFragment();//创建待添加的碎片实例
                FragmentManager manager=Main3Activity.this.getSupportFragmentManager();//获取FragmentManager
                FragmentTransaction transaction=manager.beginTransaction();//开启一个事务
                transaction.replace(R.id.name,fragment);//向容器添加或替换碎片
                transaction.commit();//提交事务
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment=new TwoFragment();
                FragmentManager manager=Main3Activity.this.getSupportFragmentManager();
                FragmentTransaction transaction=manager.beginTransaction();
                transaction.replace(R.id.name,fragment);
                transaction.commit();

            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment=new ThreeFragment();
                FragmentManager manager=Main3Activity.this.getSupportFragmentManager();
                FragmentTransaction transaction=manager.beginTransaction();
                transaction.replace(R.id.name,fragment);
                transaction.commit();
            }
        });

    }
activity_main3.xml
<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=".Test.Main3Activity">


    <FrameLayout
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="300dp">
    </FrameLayout>


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:text="展开1"
            android:id="@+id/ButtonA"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <Button
            android:text="展开2"
            android:id="@+id/ButtonB"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <Button
            android:text="展开3"
            android:id="@+id/ButtonC"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />

    </LinearLayout>


</LinearLayout>
创建一个叫oneFragment的class并继承Fragment
public class OneFragment extends Fragment {
    private TextView textView1;
    private TextView textView2;
    private Button   button1;
    private Button   button2;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.layout_one,container,false);
        textView1=view.findViewById(R.id.takeA);
        textView2=view.findViewById(R.id.takeB);
        button1=view.findViewById(R.id.skipA);
        button2=view.findViewById(R.id.skipB);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                TwoFragment twoFragment=new TwoFragment();//实例化要跳转的fragment对象(也就是你要跳转的下一个碎片)
                FragmentTransaction transaction=getFragmentManager().beginTransaction();
                transaction.add(R.id.name,twoFragment).addToBackStack(null).commitAllowingStateLoss();
                Bundle bundle=new Bundle();//声明一个Bundle对象
                String data=textView1.getText().toString();
                String g=textView2.getText().toString();
                Log.d("ff", "onClick: ");
                bundle.putString("msga",data);//用Bundle对象携带需要传递的信息
                bundle.putString("msgg",g);
                twoFragment.setArguments(bundle);//将bundle对象绑定fragment
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ThreeFragment threeFragment=new ThreeFragment();
                Log.d("ff", "onClick: 1");
                FragmentTransaction transaction=getFragmentManager().beginTransaction();
                Log.d("ff", "onClick: 2");
                transaction.add(R.id.name,threeFragment).addToBackStack(null).commitAllowingStateLoss();
                String b=textView1.getText().toString();
                String k=textView2.getText().toString();
                Log.d("ff", "onClick: 6");
                Bundle bundle=new Bundle();
                bundle.putString("msgb",b);
                bundle.putString("msgk",k);
               threeFragment.setArguments(bundle);
            }
        });
        return view;

    }

layout_one.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@android:color/holo_green_light">
    <TextView
        android:id="@+id/takeA"
        android:text="好好"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/takeB"
        android:text="学习"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/skipA"
            android:text="跳转2"
            android:textSize="20dp"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/skipB"
            android:text="跳转3"
            android:textSize="20dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

TwoFragment
public class TwoFragment extends Fragment {
    private TextView textView1;
    private TextView textView2;
    private Button button1;
    private Button button2;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.layout_two,container,false);
        textView1=view.findViewById(R.id.takeE);
        textView2=view.findViewById(R.id.takeF);
        button1=view.findViewById(R.id.skipE);
        button2=view.findViewById(R.id.skipF);

        Bundle bundle1=TwoFragment.this.getArguments();
        if (bundle1!=null){//切记要判断内容是否为空,否则程序运行getString方法会报空指针的错误。
            textView1.setText(bundle1.getString("msga"));//msga在oneFragment设置好的key值,在此处获取传递过来的内容。
            textView2.setText(bundle1.getString("msgg"));
        }

        Log.d("ff", "onCreateView: ");

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OneFragment oneFragment=new OneFragment();
                FragmentTransaction transaction=getFragmentManager().beginTransaction();
                transaction.add(R.id.name,oneFragment).addToBackStack(null).commitAllowingStateLoss();
                String c=textView1.getText().toString();
                String m=textView2.getText().toString();
                Bundle bundle=new Bundle();
                bundle.putString("msgc",c);
                bundle.putString("msgm",m);
                oneFragment.setArguments(bundle);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ThreeFragment threeFragment=new ThreeFragment();
                FragmentTransaction transaction=getFragmentManager().beginTransaction();
                transaction.add(R.id.name,threeFragment).addToBackStack(null).commitAllowingStateLoss();
                Bundle bundle=new Bundle();
                String d=textView1.getText().toString();
                String n=textView2.getText().toString();
                bundle.putString("msgb",d);
                bundle.putString("msgk",n);
               threeFragment.setArguments(bundle);
            }
        });
        return view;
    }
   

}

layout_two.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@android:color/holo_blue_light">
    <TextView
        android:id="@+id/takeE"
        android:text=""
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:text=""
        android:textSize="30dp"
        android:id="@+id/takeF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/skipE"
            android:text="跳转1"
            android:textSize="20dp"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/skipF"
            android:text="跳转3"
            android:textSize="20dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

ThreeFragment
public class ThreeFragment extends Fragment {
    private TextView textView1;
    private TextView textView2;
    private Button button1;
    private Button button2;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.layout_three,container,false);
        textView1=view.findViewById(R.id.takeC);
        textView2=view.findViewById(R.id.takeD);
        button1=view.findViewById(R.id.skipC);
        button2=view.findViewById(R.id.skipD);

        Bundle bundle1=ThreeFragment.this.getArguments();
        if (bundle1!=null) {
            textView1.setText(bundle1.getString("msgb"));
            textView2.setText(bundle1.getString("msgk"));
        }

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OneFragment oneFragment=new OneFragment();
                FragmentTransaction transaction=getFragmentManager().beginTransaction();
                transaction.add(R.id.name,oneFragment).addToBackStack(null).commitAllowingStateLoss();
                String e=textView1.getText().toString();
                String o=textView2.getText().toString();
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TwoFragment twoFragment=new TwoFragment();
                FragmentTransaction transaction=getFragmentManager().beginTransaction();
                transaction.add(R.id.name,twoFragment).addToBackStack(null).commitAllowingStateLoss();
                Bundle bundle=new Bundle();
                String f=textView1.getText().toString();
                String p=textView2.getText().toString();
                bundle.putString("msga",p);
                bundle.putString("msgg",f);
                twoFragment.setArguments(bundle);
            }
        });


        return view;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


}

layout_three.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/colorPrimary">
    <TextView
        android:id="@+id/takeC"
         android:text=""
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/takeD"
        android:text=""
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/skipC"
            android:text="传输1"
            android:textSize="20dp"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/skipD"
            android:text="传输2"
            android:textSize="20dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

相关文章

网友评论

      本文标题:Android Fragment传递数据以及跳转

      本文链接:https://www.haomeiwen.com/subject/gygfgqtx.html