在开发中有时候会遇见一些的情况:根据不同的需求切换不同的fragment ,然后一些操作使当前fragment中显示的内容进行一些调整。很多时候我们都是想的在fragment添加到Activity中时通过 fragmet. getView()获取fragment对象的View 。然后用findViewById()获取到fragment中的控件对象进行操作,很悲剧的是不管用啊有木有。我是个小白,研究了近一周都没搞定,只有采用另一种方式对fragment中的控件进行操作了。
一. 发送广播更新UI
UI中需要时发送一条广播 可顺便传入一些需要的值,在fragment中接收广播,获取值并对fragment中的控件进行操作,经本人验证完全可以。以下是Demo关键代码
在Fragment中
public class GasFragment extends Fragment { private TextView gasName; private TextView gasadderss; private ReceiveBroadCast receiveBroadCast; public GasFragment (){} @Override public void onAttach(Activity activity) { /** 注册广播 */ receiveBroadCast = new ReceiveBroadCast(); IntentFilter filter = new IntentFilter(); filter.addAction("com.gasFragment"); //只有持有相同的action的接受者才能接收此广播 activity.registerReceiver(receiveBroadCast, filter); super.onAttach(activity); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_gas, null); gasName = (TextView) view.findViewById(R.id.frag_tv_gasname); gasadderss = (TextView) view.findViewById(R.id.fraggas_tv_adderss); } }); return view; } class ReceiveBroadCast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //得到广播中得到的数据,并显示出来 String gasname = intent.getExtras().getString("gasName"); String address = intent.getExtras().getString("address"); gasadderss.setText("地址:\n "+address); gasName.setText(gasname); } } /** *注销广播 * */ @Override public void onDestroyView() { getActivity().unregisterReceiver(receiveBroadCast); super.onDestroyView(); } }
public class MainPage extends FragmentActivity { private GasFragment gasFragment; //加油站的fragment @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_page); gasFragment = new GasFragment(); FragmentManager fm = getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.main_fl_fragment, couponFragment);// 将帧布局替换成Fragment ft.commit();// 提交 Intent intent = new Intent(); // Itent就是我们要发送的内容 intent.setAction("com.gasFragment"); // 设置你这个广播的action intent.putExtra("gasName","核反应能量加油站"); intent.putExtra("address", "太平洋街11号"); sendBroadcast(intent); // 发送广播 } }
对应xml就不用上传了吧,很简单的布局。这样就可以动态的更新fragment中的显示内容了。
二. 通过Handler刷新Fragment
在需要交互的Activity中建立Handler对象 具体上代码
public class MainActivity extends FragmentActivity implements OnClickListener{ FrameLayout fl_replace; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragmentA = new FragmentA(); fragmentA.setHandler(handler); fl_replace=(FrameLayout) findViewById(R.id.fl_replace); findViewById(R.id.btn_replace).setOnClickListener(this); findViewById(R.id.btn_refresh).setOnClickListener(this); findViewById(R.id.btn_return).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_replace: fl_replace.setVisibility(View.VISIBLE); FragmentManager fm=getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.fl_replace, fragmentA); ft.commit(); break; case R.id.btn_refresh: handler.sendEmptyMessage(FragmentA.ACT_REF); break; case R.id.btn_return: fl_replace.setVisibility(View.GONE); break; } } Handler handler =new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) { case FragmentA.ACT_REF:// 在Activity中刷新Fragment fragmentA.setTextStr("on Activity refresh "); break; case FragmentA.FRAG_REF://在Fragment中刷新Fragment fragmentA.setTextStr("on Fragment refresh "); break; } }; }; private FragmentA fragmentA; }在Fragment 中获取Handler 并通过此对象发消息 代码如下
public class FragmentA extends Fragment{ public static final int FRAG_REF=0;//on Fragment refresh Fragment public static final int ACT_REF=1;//on Activity refresh Fragment private TextView tv; public FragmentA(){} @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.frag_a, null); tv = (TextView) view.findViewById(R.id.fragment_tv); view.findViewById(R.id.fragment_btn).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { handler.sendEmptyMessage(FRAG_REF); } }); return view; } Handler handler; public void setHandler(Handler handler){ this.handler =handler; } public void setTextStr(String str){ tv.setText(str); } }handler 更新是随后添加的 所以我写了一个Demo 需要的可以去下载,一看就懂了 很简单的 http://download.csdn.net/detail/yangbo437993234/8423493