0

所以我有这种情况:

我有一个Fragment动态添加 s 的活动,结果如下所示:

在此处输入图像描述

解释:

黄色: 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用于编辑和删除片段的 s:

     <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

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

另一个问题是,如果我删除其中一个中间片段,创建的空白空间是否会被删除,而底部片段会“滑”上去覆盖它?

任何帮助,将不胜感激。

4

2 回答 2

2

检索片段的最简单方法是使用add(containerId,fragment,tag)为它们分配一个标签,以添加具有唯一标签的片段,然后使用findFragmentByTag(tag)来检索它。

于 2013-03-18T18:54:39.510 回答
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();
}

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

于 2013-03-28T12:12:11.100 回答