XML
レイアウト ファイルを含むフラグメントがあります。その中に2つのクリック可能なImageView
sがあります。それぞれについて、次のようImageView
なメソッドを設定onClick
します: android:onClick="commentFragmentRemoveOnClick".
FragmentActivity (The Activity no the Fragment) では、次のように定義しました。
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>
少しでもお役に立てれば幸いです。
ありがとう。