如何获得视图的父片段的实例: [英] How to get instance of view's parent fragment:

查看:133
本文介绍了如何获得视图的父片段的实例:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 XML 布局文件片段。在这我有一个2点击的ImageView 秒。
每个的ImageView 我设置例如一个的onClick 方法:机器人:的onClick =commentFragmentRemoveOnClick

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

在FragmentActivity(活动没有片段)我这样定义它:

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

public void commentFragmentRemoveOnClick(View v)
{

}

没有这个片段的类型为 CommentFragment ,它有一个公共无效getFragmentTag()方法
得到它的标签,我保存时间较早。我需要在图像被点击来获得它的标签片段的一个实例。

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.

我试过:

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

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

但是Eclipse使我对他们两个人的错误,这是怎么得当?

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

要更清楚,这是我的 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;
}
 }

和布局文件:

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

感谢。

推荐答案

我对你有一个一般性的建议,将解决您的问题,并帮助你在未来的 -

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


  1. 不要使用Android:在XML文件中的onClick,使用setOnClickListener在code本身 - 必须避免混合与应用程序尽可能的其他部分的意见

  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.

尽量保持独立片段的活性。

Try to keep the Fragment independent of its activity.

如果图像片段的一部分,为什么监听器是FragmentActivity的一部分?

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

使用setOnClickListener在片段本身,你可能能够使用此Framgent在应用程序的其他拍而不取决于活动。

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.

这篇关于如何获得视图的父片段的实例:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆