我有一個帶有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>
我會一點點幫助愛。
謝謝。
謝謝@Pinhassi,但是如果你看一下昨天提供的代碼作爲答案,那就是我所做的。但再次感謝您的幫助和Hag sameah:) – 2013-03-29 09:04:59