Fragment pass value to Activity

xml layout file

fragment_list.xml (controls the content in the fragment)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#ffff00">
<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="TextView"
    android:textColor="#000000"
    android:textSize="20sp"/>
</RelativeLayout>

Fragment's Java code management class

package com.administrator.fragmentdemo;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * list fragment
 */
public class ListFragment extends Fragment {

    public static final String BUNDLE_TITLE = "bundle_title";
    private String mTitle = "mo_ren_zhi";

    public static ListFragment newInstance(String title){
        ListFragment fragment = new ListFragment();
        Bundle bundle = new Bundle();//Create a bundle object to encapsulate the data
        bundle.putString(BUNDLE_TITLE,title);//put it, there are a lot of basic data such as char, int, such as String(key,Value) here, you can also putChar();
        fragment.setArguments(bundle);//Pass the packaged bundle data through the setArgument method of fragment.
        return fragment;
    }
    //create view
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //new view
        View view = inflater.inflate(R.layout.fragment_list,container,false);
        TextView textView = view.findViewById(R.id.textView);//FindViewById needs to be performed according to the root view view
        textView.setText(mTitle);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnTitleClickListener!=null){
                    mOnTitleClickListener.onClick(mTitle);
                }
            }
        });
        return view;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getArguments() != null){
            mTitle = getArguments().getString(BUNDLE_TITLE);
        }
    }

    // method to set the interface
    public void setOnTitleClickListener(OnTitleClickListener onTitleClickListener){
        mOnTitleClickListener = onTitleClickListener;
    }

    //define variable
    private OnTitleClickListener mOnTitleClickListener;
    /**
     * When the title is clicked, pass the title out
     */
    //Define the interface
    public interface OnTitleClickListener{
        void onClick(String title);
    }
}

Set the displayable area for the fragment in the container activity_main.xml

Set up the two containers listContainer and detailContainer respectively

<?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.administrator.fragmentdemo.MainActivity">

   <TextView
       android:id="@+id/textView"
       android:layout_width="match_parent"
       android:layout_height="100dp"
       android:gravity="center"
       android:text="static load fragment"/>
   <LinearLayout
       android:orientation="horizontal"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
      <LinearLayout
          android:id="@+id/listContainer"
          android:layout_width="150dp"
          android:layout_margin="1dp"
          android:layout_height="match_parent">
      </LinearLayout>
      <LinearLayout
          android:id="@+id/detailContainer"
          android:layout_width="200dp"
          android:layout_margin="1dp"
          android:layout_height="match_parent">
      </LinearLayout>
   </LinearLayout>

</LinearLayout>

The Java code of the Activity that loads the Fragment

package com.administrator.fragmentdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity implements ListFragment.OnTitleClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //1.container container 2.fragment 3.fragment-->container put the fragment into the container container
        //getSupportFragmentManager() gets a fragment container, beginTransaction() opens a thing, and add() puts the fragment in (associated with it). commit() submit to the system
        //R.id.listContainer, listContainer is the container id just set, and add associates the container with the fragment

        //fragment---> activity value fragment passes value to activity
        ListFragment listFragment = ListFragment.newInstance("list");
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.listContainer,listFragment)
                .commit();
        listFragment.setOnTitleClickListener(this);

        ListFragment detail = new ListFragment().newInstance("detail");
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.detailContainer,detail)
                .commit();
        detail.setOnTitleClickListener(this);

    }

    @Override
    public void onClick(String title) {
        setTitle(title);
    }
}


For example, when you want to pass an object, you can't use bundle to pass it

Change the ListFragment.java file

package com.administrator.fragmentdemo;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * list fragment
 */
public class ListFragment extends Fragment {

    public static final String BUNDLE_TITLE = "bundle_title";
    private String mTitle = "mo_ren_zhi";

    /**
     * Build the simulated User object
     */
    private User mUser;
    public void setUser(User user){
        mUser = user;
    }
    public class User{

    }

    public static ListFragment newInstance(String title,User user){//Add User parameter
        ListFragment fragment = new ListFragment();
        Bundle bundle = new Bundle();//Create a bundle object to encapsulate the data
        bundle.putString(BUNDLE_TITLE,title);//put it, there are a lot of basic data such as char, int, such as String(key,Value) here, you can also putChar();
        fragment.setArguments(bundle);//Pass the packaged bundle data through the setArgument method of fragment.
        fragment.setUser(user);//Set the User object
        return fragment;
    }
    //create view
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //new view
        View view = inflater.inflate(R.layout.fragment_list,container,false);
        TextView textView = view.findViewById(R.id.textView);//FindViewById needs to be performed according to the root view view
        textView.setText(mTitle);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnTitleClickListener!=null){
                    mOnTitleClickListener.onClick(mTitle);
                }
            }
        });
        return view;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getArguments() != null){
            mTitle = getArguments().getString(BUNDLE_TITLE);
        }
    }

    // method to set the interface
    public void setOnTitleClickListener(OnTitleClickListener onTitleClickListener){
        mOnTitleClickListener = onTitleClickListener;
    }

    //define variable
    private OnTitleClickListener mOnTitleClickListener;
    /**
     * When the title is clicked, pass the title out
     */
    //Define the interface
    public interface OnTitleClickListener{
        void onClick(String title);
    }
}

Guess you like

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