方法一、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
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
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