0

だから私はこの状況にあります:

を動的に追加するアクティビティがFragmentあり、結果は次のようになります。

ここに画像の説明を入力してください

説明:

黄色: TextView内部Fragment

オレンジ:画像Fragment

青: ImageViewの内側Fragment

フラグメントを追加するためのコードは次のとおりです。

     fragmentManager = getSupportFragmentManager();
     FragmentTransaction newfragmentTransaction;
     for (Comment tempComment : taskCommentList)
     {
         Bundle commentBundle = new Bundle();
         CommentFragment commentFragment = new CommentFragment();
         commentString = tempComment.getText();
         if (tempComment.isPictureFirst())
         {
             if (tempComment.getPictureFilesList().size() > 0)
             {
                 picturesFragment = new PicturesFragment();
                 Bundle picturesBundle = new Bundle();
                 ArrayList<String> picturesStringPathsList = new ArrayList<String>();
                 for (File tempFile : tempComment.getPictureFilesList())
                 {
                     picturesStringPathsList.add(tempFile.getAbsolutePath());
                 }
                 picturesBundle.putStringArrayList(PICTURES_PATHS, picturesStringPathsList);
                 picturesFragment.setArguments(picturesBundle);
                 newfragmentTransaction = fragmentManager.beginTransaction();
                 newfragmentTransaction.add(R.id.containerForFragments, picturesFragment).commit();                 
             }
             if (commentString != "")
             {
                    commentBundle.putString("comment", commentString);
                    commentBundle.putString("user", tempComment.getUser());
                    commentBundle.putString("at", tempComment.getTime()+", "+tempComment.getDate());
                    commentFragment.setArguments(commentBundle);
                    newfragmentTransaction = fragmentManager.beginTransaction();
                    newfragmentTransaction
                    .add(R.id.containerForFragments, commentFragment, "comment"+ String.valueOf(taskCommentList.indexOf(tempComment)))
                    .commit();                  
             }
         }
         else
         {
             if (commentString != "")
             {
                    commentBundle.putString("comment", commentString);
                    commentBundle.putString("user", tempComment.getUser());
                    commentBundle.putString("at", tempComment.getTime()+", "+tempComment.getDate());
                    commentFragment.setArguments(commentBundle);
                    newfragmentTransaction = fragmentManager.beginTransaction();
                    newfragmentTransaction
                    .add(R.id.containerForFragments, commentFragment, "comment"+ String.valueOf(taskCommentList.indexOf(tempComment)))
                    .commit();                  
             }

             if (tempComment.getPictureFilesList().size() > 0)
             {
                 picturesFragment = new PicturesFragment();
                 Bundle picturesBundle = new Bundle();
                 ArrayList<String> picturesStringPathsList = new ArrayList<String>();
                 for (File tempFile : tempComment.getPictureFilesList())
                 {
                     picturesStringPathsList.add(tempFile.getAbsolutePath());
                 }
                 picturesBundle.putStringArrayList(PICTURES_PATHS, picturesStringPathsList);
                 picturesFragment.setArguments(picturesBundle);
                 newfragmentTransaction = fragmentManager.beginTransaction();
                 newfragmentTransaction.add(R.id.containerForFragments, picturesFragment).commit();                 
             }
         }
     }

問題:次に、これらのフラグメントのそれぞれに編集オプションと削除オプションを追加する必要があります。これまで、私はトランザクションを実行し、Activityメインビュー内にフラグメントを「スロー」していました。今、私は自分が作成したフラグメントインスタンスのすべてを手に入れる必要があります(多分それらの配列を作成しますか?)、または別の方法がありますか?たぶんフラグメントマネージャーを使用していますか?

更新: ここでの前置詞に従って、私は次のことを行いました:

1.使用するフラグメントにgetFragmentTagとを定義しました。setFragmentTagしたがって、を介してフラグメントを追加するときは、次のFragmentManagerようにします。

     String tempTag = commentTagString+currentCommentFragmentTagNumber;
     currentCommentFragmentTagNumber++;
     commentFragment.setFragmentTag(tempTag);
     newfragmentTransaction = fragmentManager.beginTransaction();
     newfragmentTransaction.add(R.id.containerForFragments, commentFragment, tempTag).commit();

各フラグメントには、フラグメントImageViewを編集および削除するためのがあります。

     <ImageView
        android:id="@+id/iEdit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="14dp"
        android:layout_marginRight="4dp"
        android:clickable="true"
        android:onClick="pictureFragmentEditOnClick"
        android:src="@drawable/add_comment_button"
        android:contentDescription="@drawable/add_comment_button" />
    <ImageView
        android:id="@+id/iRemove"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="14dp"
        android:layout_marginRight="4dp"
        android:clickable="true"
        android:onClick="pictureFragmentRemoveOnClick"
        android:src="@drawable/add_comment_button"
        android:contentDescription="@drawable/add_comment_button" />

だから私のメインFramgnetActivityでは、フラグメントタグを取得するためにこれを行います:

PicturesFragment tempFragment = (PicturesFragment)v.getParent().getParent();
String tempTag = tempFragment.getFragmentTag();
Log.d(TAG, "The Fragments tag is: "+ tempTag);

しかし、何らかの理由でキャストエラーが発生します:

Caused by: java.lang.ClassCastException: android.support.v4.app.NoSaveStateFrameLayout cannot be cast to com.emildesign.sgtaskmanager.fragments.CommentFragment

したがって、質問は、フラグメントに関連付けたこのタグを取得するにはどうすればよいですか?

もう1つの質問は、中央のフラグメントの1つを削除すると、作成された空のスペースが削除され、下部のフラグメントが「スライド」してそれをカバーするかどうかです。

どんな助けでもいただければ幸いです。

4

2 に答える 2

2

フラグメントを取得する最も簡単な方法は、add(containerId,fragment,tag)を使用してフラグメントにタグを割り当て、一意のタグを持つフラグメントを追加してから、findFragmentByTag(tag)を使用してフラグメントを取得することです。

于 2013-03-18T18:54:39.510 に答える
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:12:11.100 に答える