Android:打开另一个片段 [英] Android: Opening a Fragment from another
问题描述
我是Android新手,这是我的第二款应用。我正在创建一个标签式活动,其中第一个片段具有创建新任务的表单,第二个片段具有所有已保存任务的列表,第三个片段将显示从第二个列表中选择的任务的注释分段。第三个片段也应该像聊天活动一样,当您输入注释时发布注释并点击发送按钮。当我从单独的活动
( CommentsActivity.java
在下面的链接中的GitHub分支中)实现此聊天活动时,该应用程序运行绝对应有的运行。但是,当我尝试从第三个 Fragment
实现相同的代码时,我遇到以下问题:
I am new to Android and this is my second app. I am creating a tabbed activity where the first fragment has a form to create a new task, the second fragment has the list of all the saved tasks, and the third fragment will show the comments on a task when selected from the list in the second fragment. The third fragment is also supposed to act like a chat activity which posts comments when you type them in and tap the send button. When I implement this chat activity from a separate Activity
(CommentsActivity.java
in the GitHub branch in the link below), the app runs absolutely as it is supposed to. However, when I try to implement the same code from the third Fragment
, I have the following problems:
-
我必须在
CommentsFragment $ c $之前两次点击列表项(在
TasksFragment.java
中) c>已启动。
I have to tap the list item (in
TasksFragment.java
) twice before theCommentsFragment
is launched.
该片段仍为空白,未显示任何详细信息或评论。
The fragment remains blank and shows no details or comments.
标题栏搞砸了。当第一次启动 CommentsFragment
时,它会正确设置标题。然而,当我在片段之间滑动时,它随后变成空白。有时甚至会显示最后一个打开片段的标题,而不是当前打开的片段。
The title bar is messed up. It does set the title correctly when CommentsFragment
is launched for the first time. However, it becomes blank subsequently, when I swipe between the fragments . Sometimes it even shows the title of the last open fragment instead of the one currently open.
你可以找到我所有的代码在这里: https://github.com/geekskool/android-teamwork/tree/only_fragments
You can find all my code here: https://github.com/geekskool/android-teamwork/tree/only_fragments
其他细节:我在 TasksFragment
和之间传递数据CommentsFragment
包含与主要活动交互的片段交互方法 - AddTask.java
。这是我所知道的唯一方式。如果有更好的方法,请告诉我。
Other details: I am passing the data between the TasksFragment
and the CommentsFragment
with fragment interaction methods that interact with the main activity--AddTask.java
. This is the only way I know of. If there is a better way, please let me know.
推荐答案
我看到你的分支,发现你使用的是 ViewPager
并尝试使用 FragmentTransaction
更新 ViewPager
内的片段。这不起作用,因为创建 ViewPagerAdapter
时已经创建了片段,并且无法将其附加到 ViewPager
通过 FragmentTransaction
。
I saw your branch and noticed that you are using a ViewPager
and trying to use FragmentTransaction
to update the fragments inside the ViewPager
. This does not work as the fragments will already have been created when the ViewPagerAdapter
is created and cannot be attached to the ViewPager
through a FragmentTransaction
.
以下是我解决它的方法。由于我使用的是ViewPager,因此使用 FragmentTransaction
如下所示:
Here is how I solved it. Since I was using a ViewPager, using FragmentTransaction
as below did not work:
错误方式:
FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
CommentsFragment commentsFragment = CommentsFragment.newInstance(mTaskId, mTaskName);
ft.replace(R.id.container, commentsFragment);
ft.addToBackStack(null);
ft.commit();
使用ViewPager,我必须更新填充它的适配器,然后将其设置为正确的片段。这就是我做的:
With the ViewPager, I had to update the adapter populating it and then set it to the correct fragment. This is what I did:
正确的方式:
在onListItemClick( )在TasksFragment中,在onFragmentInteraction上调用此接口方法。然后在主要活动AddTask中,按如下方式实现:
In the onListItemClick() in the TasksFragment, call this interface method onFragmentInteraction. And then in the main activity AddTask, implement it as below:
@Override
public void onFragmentInteraction(String taskId, String taskName, String assigneeRef) {
CommentsFragment commentsFragment = CommentsFragment.newInstance(taskId, taskName);
mSectionsPagerAdapter.fragmentList.remove(2);
mSectionsPagerAdapter.fragmentList.add(commentsFragment);
mSectionsPagerAdapter.notifyDataSetChanged();
mViewPager.setCurrentItem(2);
}
希望这有助于您和其他人在更新内部碎片时遇到问题ViewPager。
Hope this helps you and someone else who faces a problem with updating Fragments inside a ViewPager.
这篇关于Android:打开另一个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!