去除是动态添加/编辑片段: [英] Removing/Editing Fragments that were add dynamically:

查看:204
本文介绍了去除是动态添加/编辑片段:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这样的情况:

我在我加入片段的活动起动态,结果是这样的:

I have an Activity in which I add Fragments dynamically and the result looks like this:

说明:

黄色: 的TextView 内部的片段

橙:图片片段

蓝: 的ImageView 的内部的片段

下面是code添加片段:

Here is the code for adding the fragments:

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

问题:现在我需要添加一个编辑和删除的那些片段中的每一个选项。
直到现在我正在交易和扔我的片段我的活动的内部主视图。
现在我得每我已经创建了(也许创建它们的数组?)片段的实例中的每一个的持有,或有另一种方式?也许利用碎片经理?

The problem: Now I need to add an edit and delete options for each one of those Fragments. Until now I was making transactions and "throwing" my fragments inside of my Activity main view. Now I have to get hold of each and every one of fragment instances I have created (Maybe create an array of them?), or is there another way? Maybe using the Fragment Manager?

更新:
这里继pre位置,我做了以下内容:

UPDATE: Following the preposition here I did the following:

1 我已经定义了一个 getFragmentTag setFragmentTag 在我使用的片段。
所以,当我通过 FragmentManager 添加片段我这样做:

1. I have defined a getFragmentTag and setFragmentTag in my used fragments. So when I add the fragment via FragmentManager I do this:

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

在每个片段中我有的ImageView S代表编辑和删除片段:

in each fragment I have ImageViews for editing and deleting the fragment:

     <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 我这样做是为了获得片段标记:

So in My main FramgnetActivity I do this to get the fragment tag:

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

但由于某些原因,我收到一个错误铸造:

but for some reason I receive a casting error:

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

所以,问题是:我怎么得到这个标签我相关联的片段

So The Question is: how do I get this tag I associated to the fragment?

另一个问题是,如果我删除会创建空的空间被删除中间片段之一,底部的碎片将滑到覆盖它?

another question is if I delete one of the middle fragment would the empty space that created be removed and the bottom fragments will "slide" up to cover it?

任何帮助,将AP preciated。

Any Help would be appreciated.

推荐答案

要获得该的ImageView 被点击中我的片段的实例做了以下内容:

To get the instance of the fragment that the ImageView was clicked in I did the following:

在我设置两个片段 onClickListeners 为这样的图像:

in the Fragment I set two onClickListeners for both of the images like this:

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

和片段中的活动我定义这两种方法是这样的:

and in the fragment activity I defined those two methods like this:

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

去除片段,而现在我正在编辑的片段。

for removing the fragment, and Now I'm working on editing the fragment.

这篇关于去除是动态添加/编辑片段:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆