2013-03-27 39 views
2

我有一個帶有XML佈局文件的片段。其中我有一個2點擊ImageView s。 每個ImageView我設置了一個onClick方法,例如:android:onClick =「commentFragmentRemoveOnClick」。如何獲得視圖的父代片段的實例:

在FragmentActivity(活動沒有片段),我定義它是這樣的:

public void commentFragmentRemoveOnClick(View v) 
{ 

} 

沒有這個片段是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> 

我會一點點幫助愛。

謝謝。

回答

2

我對你會解決你的問題,並幫助你在未來的一般建議 -

  1. 不要使用Android:在XML文件中的onClick,使用setOnClickListener在代碼本身 - 需要儘可能避免將您的觀點與應用程序的其他部分混合在一起。

  2. 儘量讓片段獨立於其活動。

如果圖像是片段的一部分,爲什麼監聽器是FragmentActivity的一部分?

在片段本身中使用setOnClickListener,並且您可能能夠在不依賴於Activity的情況下在應用的其他拍片中使用此Framgent。

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

+0

謝謝@Pinhassi,但是如果你看一下昨天提供的代碼作爲答案,那就是我所做的。但再次感謝您的幫助和Hag sameah:) – 2013-03-29 09:04:59

2

v不是Fragment的實例,這就是爲什麼Eclipse不喜歡你的代碼。如果你想要一個片段的實例,你必須使用FragmentManager和它的一個findFragmentByXXX方法。

+0

我知道v不是片段,v是ImageView裏面的一個片段,我需要通過這個ImageView獲取這個片段的實例。我的片段動態添加,並沒有身份證。他們只有我通過setFragmentTag和getFragmentTag分配給他們的標籤,我現在正在使用它。 – 2013-03-27 22:27:54

+0

在這種情況下,您使用findFragmentByTag。另外,如果你知道v不是片段,你爲什麼要做'((CommentFragment)v)'? – Cristian 2013-03-27 22:29:00

+0

你沒有得到我的問題:通過FragmentManager使用findFragmentByTag方法我需要獲取視圖被點擊的片段的實例,並從中提取它分配的標籤,我分配給它早些時候。這就是我想要做的,如果你仔細觀察,你會看到我試圖獲得點擊查看的父母,我只是不知道它是如何完成的。 – 2013-03-27 22:33:40

0

要獲得該ImageView被點擊中我的片段的實例做了以下內容:

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); 
     } 
    }); 

和:

在我設置兩個 onClickListeners兩個這樣的圖像的片段

片段活動我定義這兩種方法是這樣的:

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

用於刪除片段,現在我工作o n編輯片段。

相關問題