java 如何获取视图的父片段实例:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15670673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:27:18  来源:igfitidea点击:

How to get instance of view's parent fragment:

javaandroidlayoutcastingandroid-fragments

提问by Emil Adz

I have a fragment with an XMLlayout file. in it I have an 2 clickable ImageViews. for each ImageViewI set an onClickmethod for example: android:onClick="commentFragmentRemoveOnClick".

我有一个带有XML布局文件的片段。其中我有 2 个可点击的ImageViews。对于每个ImageView我设置一个onClick方法,例如:android:onClick="commentFragmentRemoveOnClick"。

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

在 FragmentActivity (The Activity no the Fragment) 中,我是这样定义的:

public void commentFragmentRemoveOnClick(View v)
{

}

No this Fragment is of type CommentFragmentand 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.

不,这个 Fragment 是类型的CommentFragment,它有一种public void getFragmentTag()方法可以获取我在早些时候保存的标签。我需要获取一个片段实例,在该片段中单击图像以获取其标签。

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?

但是 eclipse 给我两个错误,这是如何正确完成的?

To make it more clear this is my CommentFragment:

为了更清楚,这是我的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.

谢谢。

回答by Cristian

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 FragmentManagerand one of its findFragmentByXXX methods.

v 不是 的实例Fragment,这就是 Eclipse 不喜欢您的代码的原因。如果您想要片段的实例,则必须使用FragmentManager和它的 findFragmentByXXX 方法之一。

回答by Asaf Pinhassi

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.

  1. 不要在 xml 文件中使用 android:onClick,在代码本身中使用 setOnClickListener - 需要尽可能避免将您的视图与应用程序的其他部分混合。

  2. 尽量保持 Fragment 独立于其活动。

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

如果图像是片段的一部分,为什么侦听器是 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.

在片段本身中使用 setOnClickListener ,您可能能够在应用程序的其他部分中使用此 Framgent 而不依赖于 Activity。

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

它还可以解决您识别单击图像的片段的问题。

回答by Emil Adz

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

为了获取ImageView被点击的片段实例,我执行了以下操作:

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

在片段中,我onClickListeners为两个图像设置了两个,如下所示:

    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.

用于删除片段,现在我正在编辑片段。