Fragment传值到Fragment的操作步骤

avtivity_main.xml中:

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:id="@+id/linearlayout_left"
        >

        <fragment
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:name="com.example.fragmentpassvaluefragment.fragments.LeftFragment"
            android:id="@+id/fragment_left"
            android:layout_gravity="center_horizontal" />
    LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/linearlayout_right"
        android:layout_gravity="center_vertical">

        <fragment
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:name="com.example.fragmentpassvaluefragment.fragments.RightFragment"
            android:id="@+id/fragment_right"
            android:layout_gravity="center_horizontal" />
    LinearLayout>
LinearLayout>
fragment_right.xml中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >


    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="30sp"
        android:gravity="center"
        android:text="等待传值。。。"
        android:id="@+id/textView_name" />
LinearLayout>

MainActivity.java中:

package com.example.fragmentpassvaluefragment;

import android.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.example.fragmentpassvaluefragment.fragments.PassValue;

public class MainActivity extends AppCompatActivity{

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

    }
}

方式一:

        LeftFragment.java中:

package com.example.fragmentpassvaluefragment.fragments;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.fragmentpassvaluefragment.R;

import java.util.ArrayList;
import java.util.List;


public class LeftFragment_bak02 extends ListFragment{
    private List data;
    private ArrayAdapter adapter;
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        data=new ArrayList<>();
        for(int i=0;i<10;i++){
            data.add("小陈"+i);
        }

        adapter=new ArrayAdapter(this.getActivity(),android.R.layout.simple_list_item_1,data);

        this.setListAdapter(adapter);

    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        String item=data.get(position);
        TextView textView_name= (TextView) getActivity().getFragmentManager().findFragmentById(R.id.fragment_right).getView().findViewById(R.id.textView_name);
        textView_name.setText(item);

    }

}

        RightFragment.java 中:

package com.example.fragmentpassvaluefragment.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.fragmentpassvaluefragment.R;


public class RightFragment_bak02 extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_right,null);
        return view;
    }
}

方式二: 

        LeftFragment.java中:

       

package com.example.fragmentpassvaluefragment.fragments;

import android.app.ListFragment;
import android.os.Bundle;
import android.widget.ArrayAdapter;

import java.util.ArrayList;
import java.util.List;

public class LeftFragment_bak03 extends ListFragment{
    private List data;
    private ArrayAdapter adapter;
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        data=new ArrayList<>();
        for(int i=0;i<10;i++){
            data.add("小陈"+i);
        }

        adapter=new ArrayAdapter(this.getActivity(),android.R.layout.simple_list_item_1,data);

        this.setListAdapter(adapter);

    }



}

        RightFragment.java中:

package com.example.fragmentpassvaluefragment.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.fragmentpassvaluefragment.R;


public class RightFragment_bak03 extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_right,null);
        final TextView textView_name= (TextView) view.findViewById(R.id.textView_name);
        LeftFragment leftFragment= (LeftFragment) getActivity().getFragmentManager().findFragmentById(R.id.fragment_left);
        ListView listView=leftFragment.getListView();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                String name=((TextView)view).getText().toString();
                textView_name.setText(name);
            }
        });

        return view;
    }
}

方式三:

        LeftFragment.java中:

package com.example.fragmentpassvaluefragment.fragments;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.fragmentpassvaluefragment.R;

import java.util.ArrayList;
import java.util.List;

public class LeftFragment extends ListFragment{
    private List data;
    private ArrayAdapter adapter;
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        data=new ArrayList<>();
        for(int i=0;i<10;i++){
            data.add("小陈"+i);
        }

        adapter=new ArrayAdapter(this.getActivity(),android.R.layout.simple_list_item_1,data);

        this.setListAdapter(adapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        String name=data.get(position);
        RightFragment rightFragment= (RightFragment) getActivity().getFragmentManager().findFragmentById(R.id.fragment_right);
        rightFragment.setName(name);
    }
}


        RightFragment.java中:

package com.example.fragmentpassvaluefragment.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.fragmentpassvaluefragment.R;

/**
 * Created by TF on 2018/6/14.
 */
public class RightFragment extends Fragment{
    private TextView textView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_right,null);
        textView= (TextView) view.findViewById(R.id.textView_name);
        return view;
    }

    public void setName(String name) {
        textView.setText(name);
    }
}





       

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