Связь между Fragments
осуществляется usinng Listeners
. Если вы хотите обновить фрагмент, используйте прослушиватель, чтобы сообщить MainActivity
, чтобы обновить второй фрагмент, как рекомендовано Google http://developer.android.com/training/basics/fragments/communicating.html. Создание интерфейса в Fragment
и реализовать это в Activity
Listener в Фрагменте
public interface FragmentUpdateInterface {
void updateFragment(String newText);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (FragmentUpdateInterface) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement FragmentUpdateListener");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
mCallback.updateFragment("New Text");
}
MainActivity Реализовать фрагмент в MainActivity
как
public static class MainActivity extends Activity
implements MyFragment.FragmentUpdateListener{
public void updateFragment(String newText) {
OtherFragment otherFrag = (OtherFragment)
getSupportFragmentManager().findFragmentById(R.id.other_fragment);
if (otherFrag != null) {
otherFrag.updateFragment(newText);
} else {
// Otherwise, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
OtherFragment otherFrag = new OtherFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
otherFrag.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, otherFrag);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
Надеется, что это помогает.
UPDATE:
Вы также можете использовать LocalBroadcastManager.getInstance().sendBroadcast()
уведомить к другому фрагменту, а также.
пытаются аннулировать TextView в фрагмент с обратным вызовом. – Sreekanth
Есть две возможности использования интерфейса между этими фрагментами, или вы можете использовать библиотеку Eventbus, которая позволяет вам создавать локальные события и прослушиватели событий для этого. Если вам нужно больше на eventbus, дайте мне знать. – androidnoobdev
Вы должны добавить слушателя и посмотреть на это http://developer.android.com/intl/ja/training/basics/fragments/communicating.html –