Fragment之间通过Activity相互传值和调用的2个方式

方法一、AFragment提供接口回调调用Aty的方法,Aty再调用BFragment的方法来实现
这里写图片描述
1。A中

  private  void  frag2aty2frag(int position){
        /**操作传值 通过接口回调 ( 一.Fragment传值给Aty)(1.Fragment调用Aty)*/
        mFragment2AtyListener.onFragment2Aty_A(position);

        /**操作传值 通过方法调用 ( 二.Fragment传值给Aty)(2.Fragment调用Aty)*/
        //((MainActivity)getActivity()).callAty(position);
    }

2。Aty中

...
  @Override
    public void onFragment2Aty_A(Object object) {
        //调用B
        atyCallBFragment(object);
    }
    ...
  private  void  atyCallBFragment(Object object)
    {
    //bFragment是全局实例  B中有callBFragment(object)方法
        bFragment.callBFragment(object);
    }
    ...

3。B中

   //供Aty调用
    public void callBFragment(Object object){
        // textView.setText("xxx");

        getNewData(object.toString());
        simpleAdapter.notifyDataSetChanged();

    }

demo下载:http://download.csdn.net/detail/richiezhu/9477675

方法二、接口关联

1。LeftFragment

 package com.louisgeek.louiseventbusdemo;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link LeftFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class LeftFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    ListView listView;
    List<Map<String,Object>> mapList=new ArrayList<>();




    static LoadMoreListener mLoadMoreListener;
    public LeftFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment LeftFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static LeftFragment newInstance(String param1, String param2,LeftFragment.LoadMoreListener LoadMoreListener) {

        mLoadMoreListener=LoadMoreListener;

        LeftFragment fragment = new LeftFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

        for (int i=0;i<25;i++){
            Map<String,Object> map=new HashMap<>();
            map.put("name","pa_name"+i);
            mapList.add(map);
        }

    }

    @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);
        listView= (ListView) view.findViewById(R.id.id_lv);
        SimpleAdapter simpleAdapter=new SimpleAdapter(getActivity(),mapList, android.R.layout.simple_list_item_1,new String[]{"name"},new int[]{android.R.id.text1});
        listView.setAdapter(simpleAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //
         mLoadMoreListener.onLoad(position);
            }
        });
        return  view;
    }



    public interface  LoadMoreListener{
        void  onLoad(int pos);
    }


}

2.ItemFragment

package com.louisgeek.louiseventbusdemo;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ItemFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ItemFragment extends Fragment implements LeftFragment.LoadMoreListener{
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    ListView  listView;
    SimpleAdapter simpleAdapter;
    List<Map<String,Object>> mapList=new ArrayList<>();


    int  nowIndex=0;
    public ItemFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment ItemFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ItemFragment newInstance(String param1, String param2) {

        ItemFragment fragment = new ItemFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        for (int i=0;i<5;i++){

            Map<String,Object> map=new HashMap<>();
            map.put("name","name"+i);
            mapList.add(map);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
         View view=inflater.inflate(R.layout.fragment_item, container, false);



        listView= (ListView) view.findViewById(R.id.id_lv);
        simpleAdapter=new SimpleAdapter(getActivity(),mapList, android.R.layout.simple_list_item_1,new String[]{"name"},new int[]{android.R.id.text1});
        listView.setAdapter(simpleAdapter);
        return view;
    }


    @Override
    public void onLoad(int pos) {
        nowIndex= pos;
        //Toast.makeText(getActivity(),"pos="+pos,Toast.LENGTH_SHORT).show();
        getNewData(pos);
        simpleAdapter.notifyDataSetChanged();
    }



    void getNewData(int pos){
        mapList.clear();
        for (int i=0;i<5;i++){
            Map<String,Object> map=new HashMap<>();
            map.put("name",pos+"name"+i);
            mapList.add(map);
        }
    }
}

3.MainActivity

package com.louisgeek.louiseventbusdemo;

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

public class MainActivity extends AppCompatActivity{

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

        //步骤一:添加一个FragmentTransaction的实例
        android.support.v4.app.FragmentManager fragmentManager =getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
        //步骤二:用add()方法加上Fragment的对象Fragment

        ItemFragment itemfa= ItemFragment.newInstance("", "");
        transaction.add(R.id.id_right_item,itemfa);

        LeftFragment bf= LeftFragment.newInstance("", "", itemfa);
        transaction.add(R.id.id_left_blank, bf);
        // 步骤三:调用commit()方法使得FragmentTransaction实例的改变生效
        transaction.commit();

    }

}

这里写图片描述

demo下载:http://download.csdn.net/detail/richiezhu/9477722

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值