簡體   English   中英

如何獲取視圖的父片段的實例:

[英]How to get instance of view's parent fragment:

我有一個帶有XML布局文件的片段。 在其中,我有2個可點擊的ImageView 我為每個ImageView設置了一個onClick方法,例如:android:onClick =“ commentFragmentRemoveOnClick”。

在FragmentActivity(活動不是片段)中,我是這樣定義的:

public void commentFragmentRemoveOnClick(View v)
{

}

此Fragment的類型CommentFragment ,它有一個public void getFragmentTag()方法來獲取我早些時候保存的標簽。 我需要獲取單擊圖像以獲取其標簽的片段的實例。

我試過了:

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

和:

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

但是日食給我兩個人都帶來了錯誤,如何正確地做到這一點?

更明確地說,這是我的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>

我希望得到一點幫助。

謝謝。

我為您提供一般性建議,可以解決您的問題並在將來為您提供幫助-

  1. 不要在xml文件中使用android:onClick,而在代碼本身中使用setOnClickListener-需要避免將視圖與應用程序的其他部分盡可能地混合使用。

  2. 嘗試使Fragment與其活動無關。

如果圖像是片段的一部分,為什么偵聽器是FragmentActivity的一部分?

在片段本身中使用setOnClickListener,您可能可以在應用程序的其他樣式中使用此Framgent,而不必依賴於Activity。

它還可以解決您識別單擊圖像的片段的問題。

v不是Fragment的實例,因此Eclipse不喜歡您的代碼。 如果需要片段的實例,則必須使用FragmentManager和它的findFragmentByXXX方法之一。

為了獲得單擊ImageView的片段的實例,我執行了以下操作:

在片段中,我為兩個圖像都設置了兩個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);
        }
    });

在片段活動中,我定義了以下兩種方法:

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

用於刪除片段,現在我正在編輯片段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM