今天用到viewPager嵌套fragment,需要fragment调用activity的方法,还需要activity调用fragment的方法,一时间不知道该怎么做了。后来看到别人这样实现:在Activity定义接口和一个Handler,接口的作用是让碎片接收Activity的指令,而Handler用于接收碎片传来的指令,Activity在执行操作.
如下所示:
package com.example.fragmentactivity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentTransaction;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.FrameLayout;public class MainActivity extends FragmentActivity implements OnClickListener { private Button button1; private String fragmentName; private OnButtonClickedListener buttonClickedListener; /** * 定义一个Handler用于接收黄色碎片给Activity发出来的指令 */ public Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg!=null){ switch (msg.what) { case 100: /** * 接收到黄色碎片发来的指令,Activity执行替换操作 */ fragmentName=Fragment2.class.getName(); replaceFragment(R.id.FrameLayout1, fragmentName); break; default: break; } } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1=(Button) findViewById(R.id.button1); button1.setOnClickListener(this); /** * 加载黄色碎片 */ fragmentName=Fragment1.class.getName(); replaceFragment(R.id.FrameLayout1, fragmentName); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: if(buttonClickedListener!=null){ buttonClickedListener.ed("Activity:我不想要这个黄色碎片了,点击下面的按钮给我换个绿色的碎片"); } break; default: break; } } /* * * 加载碎片 */ protected void replaceFragment(int viewResource, String fragmentName) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); Fragment fragment = Fragment.instantiate(this, fragmentName); ft.replace(viewResource, fragment); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); ft.commit(); getSupportFragmentManager().executePendingTransactions(); } /** * 定义一个接口 * @author zqy * */ public interface OnButtonClickedListener{ /** * 里面传个值 * @param s */ public void ed(String s); } /** * * @param buttonClickedListener * 写一个对外公开的方法 */ public void setButtonClickedListener(OnButtonClickedListener buttonClickedListener){ this.buttonClickedListener=buttonClickedListener; } }
这就是Activity里面的代码,我们在去写两个碎片如下所示:
Fragment1:
package com.example.fragmentactivity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import com.example.fragmentactivity.MainActivity.OnButtonClickedListener;public class Fragment1 extends Fragment implements OnClickListener { private TextView textView; private Button button; private Handler handler; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MainActivity activity=(MainActivity) getActivity(); /** * 得到Activity的Handler */ handler=activity.handler; /** * 加监听 */ activity.setButtonClickedListener(new OnButtonClickedListener() { @Override public void ed(String s) { /* * 显示Activity传来的内容 */ textView.setText(s); button.setVisibility(View.VISIBLE); } }); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.item1, null); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); textView=(TextView) getActivity().findViewById(R.id.textView1); button=(Button) getActivity().findViewById(R.id.button2); button.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button2: /** * 发送指令到Activity */ handler.sendEmptyMessage(100); break; default: break; } }
这个碎片让它的Activity实现这个接口,在把接口里面的值显示到页面上,在点击这个碎片里面的Button给它的Activity发送一个消息.这里面我发的是100.
Fragment2:
package com.example.fragmentactivity;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup; public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.item2, null); } }