简体   繁体   中英

How to get instance of view's parent fragment:

I have a fragment with an XML layout file. in it I have an 2 clickable ImageView s. for each ImageView I set an onClick method for example: android:onClick="commentFragmentRemoveOnClick".

In the FragmentActivity (The Activity no the Fragment) I defined it this way:

public void commentFragmentRemoveOnClick(View v)
{

}

No this Fragment is of type CommentFragment and it has a public void getFragmentTag() method to get it's tag that I save in earlier time. I need to get an instance of the fragment in which the image was clicked to get it's tag.

I tried:

((CommentFragment)v).getParentFragment().getFragmentTag();

and:

((CommentFragment)v).getParent().getFragmentTag();

but eclipse gives me error on both of them, how is this done properly?

To make it more clear this is my 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;
}
 }

and the layout file:

<?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>

I would love for a little assistance.

Thanks.

I have a general advice for you that would solve your problem and help you in the future -

  1. don't use android:onClick in the xml file, use setOnClickListener in the code itself - need to avoid mixing your views with other parts of the app as much as possible.

  2. Try to keep the Fragment independent of its activity.

If the image is part of the fragment, why does the listener is part of the FragmentActivity?

Use setOnClickListener in the fragment itself, and you might be able to use this Framgent in other pats of the app without being depended on the Activity.

It would also solve your problem of identifying the fragment in which the image was clicked.

v is not an instance of Fragment , that's why Eclipse does not like your code. If you want the instance of a fragment you have to use the FragmentManager and one of its findFragmentByXXX methods.

To get the instance of the fragment that the ImageView was clicked in I did the following:

in the Fragment I set two onClickListeners for both of the images like this:

    iEdit = (ImageView)rootView.findViewById(R.id.iEdit);
    iEdit.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            Log.d(TAG, "pressed edit button");
            ((PicturesAndCommentsActivity) getActivity()).commentFragmentEditOnClick(fragmentTag);
        }
    });

    iRemove = (ImageView)rootView.findViewById(R.id.iRemove);
    iRemove.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) 
        {
            Log.d(TAG, "pressed remove button");
            ((PicturesAndCommentsActivity) getActivity()).commentFragmentRemoveOnClick(fragmentTag);
        }
    });

and in the fragment activity I defined those two methods like this:

public void commentFragmentRemoveOnClick (String tag)
{
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.remove(fragmentManager.findFragmentByTag(tag)).commit();
}

for removing the fragment, and Now I'm working on editing the fragment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM