Fragment传值到Fragment的操作步骤

这篇博客详细介绍了在Android应用中Fragment之间如何进行数据传递,包括方式一的使用Bundle通过setArguments传递,方式二的利用接口回调,以及方式三的通过活动Activity作为桥梁进行传递。针对每种方法,分别在LeftFragment和RightFragment中给出了具体的实现代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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<String> data;
    private ArrayAdapter<String> 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<String>(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<String> data;
    private ArrayAdapter<String> 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<String>(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<String> data;
    private ArrayAdapter<String> 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<String>(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);
    }
}





       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值