Comment créer une instance de la vue parent du fragment:
J'ai un fragment avec une XML
fichier de mise en page. j'ai un 2 cliquable ImageView
s.
pour chaque ImageView
j'ai défini un onClick
méthode pour exemple: android:onClick="commentFragmentRemoveOnClick".
Dans le FragmentActivity (L'Activité ne le Fragment) j'ai défini de cette façon:
public void commentFragmentRemoveOnClick(View v)
{
}
Pas de ce Fragment est de type CommentFragment
et il a un public void getFragmentTag()
méthode
pour obtenir balise que j'ai enregistrer dans des temps antérieurs. J'ai besoin d'obtenir une instance de ce fragment dans lequel l'image a été cliqué pour obtenir balise.
J'ai essayé:
((CommentFragment)v).getParentFragment().getFragmentTag();
et:
((CommentFragment)v).getParent().getFragmentTag();
mais eclipse me donne l'erreur sur les deux, qu'en est-il correctement?
Pour rendre cela plus clair, c'est mon CommentFragment
:
public class CommentFragment extends Fragment {
private final static String TAG = CommentFragment.class.getSimpleName();
private String fragmentTag;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.comment_fragment_layout,
container, false);
Bundle bundle = getArguments();
String text = bundle.getString("comment");
String fullUser = bundle.getString("user");
String user = fullUser.substring(0, fullUser.indexOf("@"));
String at = bundle.getString("at");
TextView tvCmment = (TextView) rootView.findViewById(R.id.tvComment);
TextView tvUser = (TextView) rootView.findViewById(R.id.tvUser);
TextView tvAt = (TextView) rootView.findViewById(R.id.tvDate);
tvCmment.setText(text);
tvUser.setText(user);
tvAt.setText(at);
return rootView;
}
public void setText(String item)
{
TextView view = (TextView) getView().findViewById(R.id.tvComment);
view.setText(item);
}
public void setFragmentTag(String tag)
{
this.fragmentTag = tag;
}
public String getFragmentTag()
{
return this.fragmentTag;
}
}
et la mise en page du fichier:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llCommentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/try2">
<TextView
android:id="@+id/tvUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvComment"
android:layout_alignParentTop="true"
android:background="@color/my_gray"
android:text="demo"
android:textStyle="bold"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textColor="@color/my_even_darker_gray" />
<TextView
android:id="@+id/tvComment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tvDate"
android:padding="5dp"
android:text="This task is described in more details if you click on it."
android:textColor="@color/my_even_darker_gray" />
<TextView
android:id="@+id/tvAt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingRight="5dp"
android:textColor="@color/my_even_darker_gray"
android:layout_toRightOf="@+id/tvUser"
android:background="@color/my_gray"
android:text="at" />
<TextView
android:id="@+id/tvDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvAt"
android:layout_alignBottom="@+id/tvAt"
android:layout_toRightOf="@+id/tvAt"
android:background="@color/my_gray"
android:text="12/02"
android:textColor="@color/my_even_darker_gray" />
<ImageView
android:id="@+id/iEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tvComment"
android:layout_marginRight="4dp"
android:clickable="true"
android:contentDescription="@drawable/add_comment_button"
android:onClick="commentFragmentEditOnClick"
android:src="@drawable/add_comment_button" />
<ImageView
android:id="@+id/iRemove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/iEdit"
android:layout_toRightOf="@+id/iEdit"
android:layout_marginRight="4dp"
android:clickable="true"
android:contentDescription="@drawable/add_comment_button"
android:onClick="commentFragmentRemoveOnClick"
android:src="@drawable/add_comment_button" />
</RelativeLayout>
J'aimerais un peu d'aide.
Grâce.
Vous devez vous connecter pour publier un commentaire.
J'ai un conseil pour vous, qui permettrait de résoudre votre problème et vous aider dans l'avenir -
ne pas utiliser android:onClick dans le fichier xml, l'utilisation setOnClickListener dans le code lui-même - il faut éviter de mélanger vos opinions avec d'autres parties de l'application autant que possible.
Essayer de garder le Fragment indépendant de son activité.
Si l'image fait partie du fragment, pourquoi ne l'auditeur fait partie de la FragmentActivity?
Utilisation setOnClickListener dans le fragment lui-même, et vous pourriez être en mesure d'utiliser cette Framgent dans d'autres pats de l'application sans être dépendait de l'Activité.
Elle permettrait également de résoudre votre problème de l'identification du fragment dans lequel l'image a été cliqué.
v n'est pas une instance de
Fragment
, c'est pourquoi Eclipse n'aimez pas votre code. Si vous voulez que l'instance d'un fragment, vous devez utiliser leFragmentManager
et l'un de ses findFragmentByXXX méthodes.((CommentFragment)v)
?android:onClick
et plutôt affecter votre cliquez auditeurs manuellement. Aussi, si vous avez ajouté dynamiquement des fragments vous pouvez définir la balise du fragment à votre image de vues. De cette façon, si vous voulez que le fragment de la balise du fragment contenant les cliquéImageView
vous avez juste à fairev.getTag()
Pour obtenir l'instance de l'fragment de la
ImageView
a cliqué dans je n'ai suivantes:dans le Fragment, j'ai mis deux
onClickListeners
pour les deux images comme ceci:et dans le fragment activité que j'ai défini ces deux méthodes comme ceci:
pour enlever le fragment, et Maintenant je travaille sur la modification du fragment.