删除/编辑动态添加的片段:

时间:2013-03-18 18:47:57

标签: android android-fragments

所以我有这种情况:

我有一个Activity,我在其中动态添加Fragment,结果如下:

enter image description here

解释

{p> 黄色: 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。我在已用过的片段中定义了getFragmentTagsetFragmentTag。 因此,当我通过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

所以问题是:我如何获得与该片段相关联的标签?

另一个问题是,如果我删除其中一个中间片段,那么创建的空白空间将被删除,底部片段会“滑动”以覆盖它吗?

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:2)

检索片段的最简单方法是使用add(containerId,fragment,tag)为其分配标记,以使用唯一标记添加片段,然后使用findFragmentByTag(tag)进行检索。

答案 1 :(得分:0)

要获取点击ImageView的片段实例,我执行了以下操作:

片段中的

我为这两个图像设置了两个onClickListeners

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

在片段活动中我定义了这两种方法:

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

用于删除片段,现在我正在编辑片段。