所以我有这种情况:
我有一个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
所以问题是:我如何获得与片段相关联的这个标签?
另一个问题是,如果我删除其中一个中间片段,创建的空白空间是否会被删除,而底部片段会“滑”上去覆盖它?
任何帮助,将不胜感激。