3

XMLレイアウト ファイルを含むフラグメントがあります。その中に2つのクリック可能なImageViewsがあります。それぞれについて、次のよう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>

少しでもお役に立てれば幸いです。

ありがとう。

4

3 に答える 3

3

v は のインスタンスではないFragmentため、Eclipse はあなたのコードを好まないのです。フラグメントのインスタンスが必要な場合は、FragmentManagerおよびその findFragmentByXXX メソッドの 1 つを使用する必要があります。

于 2013-03-27T22:25:43.040 に答える
3

問題を解決し、将来的に役立つ一般的なアドバイスがあります -

  1. xml ファイルで android:onClick を使用しないでください。コード自体で setOnClickListener を使用してください。ビューとアプリの他の部分をできるだけ混在させないようにする必要があります。

  2. Fragment をそのアクティビティから独立させておくようにしてください。

画像がフラグメントの一部である場合、リスナーが FragmentActivity の一部であるのはなぜですか?

フラグメント自体で setOnClickListener を使用すると、Activity に依存することなく、この Framgent をアプリの他の部分で使用できる可能性があります。

また、画像がクリックされたフラグメントを特定するという問題も解決します。

于 2013-03-29T09:01:42.017 に答える
0

クリックされたフラグメントのインスタンスを取得するためにImageView、次のことを行いました。

フラグメントではonClickListeners、次のように両方の画像に2 つを設定します。

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

フラグメント アクティビティでは、これら 2 つのメソッドを次のように定義しました。

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

フラグメントを削除するために、現在、フラグメントの編集に取り組んでいます。

于 2013-03-28T12:10:28.750 に答える