Fragment跳到另一个Fragment

day_moon 2015-12-31 05:44:31
在MyFragment1类中:

public void onClick(View v) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
switch (v.getId()) {
case R.id.ib_update:
if (activity.theLatestFragment == null) {
theLatestFragment = new TheLatestFragment();
}
ft.replace(R.id.framelayout_content, activity.theLatestFragment);

break;
}

}
跪求大神!!!

为了保持底部的菜单导航不变,跳转的时候,改变的是标题和中间的内容。所以我用Fragment,但是不懂跳转。红色的区域是错误的部分?
本页面:MyFragment1
跳转的页面:theLatestFragment。
该怎么改红色部分的代码?replace中的第一个参数是什么?
...全文
150 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
day_moon 2016-01-05
  • 打赏
  • 举报
回复
跪求大神,解决!!!
day_moon 2016-01-05
  • 打赏
  • 举报
回复
MyFragment1类的布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/ll_home"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:background="#FF0000" android:gravity="center" android:text="@string/tab_menu_home" android:textSize="20sp" /> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="221dip"> <FrameLayout android:layout_width="match_parent" android:layout_height="210dp" > <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="200dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="35dip" android:layout_gravity="bottom" android:background="#33000000" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/text_image" android:textColor="@android:color/white" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dip" android:orientation="horizontal" > <View android:id="@+id/dot_0" android:layout_width="5dip" android:layout_height="5dip" android:layout_marginLeft="2dip" android:layout_marginRight="2dip" android:background="@drawable/dot_focused"/> <View android:id="@+id/dot_1" android:layout_width="5dip" android:layout_height="5dip" android:layout_marginLeft="2dip" android:layout_marginRight="2dip" android:background="@drawable/dot_normal"/> <View android:id="@+id/dot_2" android:layout_width="5dip" android:layout_height="5dip" android:layout_marginLeft="2dip" android:layout_marginRight="2dip" android:background="@drawable/dot_normal"/> <View android:id="@+id/dot_3" android:layout_width="5dip" android:layout_height="5dip" android:layout_marginLeft="2dip" android:layout_marginRight="2dip" android:background="@drawable/dot_normal"/> <View android:id="@+id/dot_4" android:layout_width="5dip" android:layout_height="5dip" android:layout_marginLeft="2dip" android:layout_marginRight="2dip" android:background="@drawable/dot_normal"/> </LinearLayout> </LinearLayout> </FrameLayout> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="74dp" android:gravity="center_horizontal" android:orientation="horizontal" > <RadioGroup android:id="@+id/rg_home" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageButton android:id="@+id/ib_update" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@drawable/button_culture"/> <ImageButton android:id="@+id/ib_cuture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@drawable/button_phone"/> <ImageButton android:id="@+id/ib_team" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@drawable/button_update"/> </RadioGroup> </LinearLayout> </LinearLayout> MyFragment1类中:我修改了 case R.id.ib_update: ft.replace(R.id.ll_home,new TheLatestFragment()); ft.commit(); break; 我想要的结果是:标题最新动态的位置是替换标题首页的fragment,不是在按钮下面(即运行的结果,2楼中的上面的图片)。
day_moon 2016-01-05
  • 打赏
  • 举报
回复
我已经看过文档了, ft.replace(R.id.framelayout_content, activity.theLatestFragment);参数R.id.framelayout_content是要替换MyFragment1类布局文件中的地方,activity.theLatestFragment是要跳转的Activity的地方。我在按钮已经添加监听了,可是跳转的位置不对啊。
跪求大神!!!!
MoneyCitys 2015-12-31
  • 打赏
  • 举报
回复
1.你應該先看看文件 最基本的參數意義起碼要知道 2.沒說報的什麼錯誤,就code來看沒有錯誤 頂多功能無法正常執行, replace之後要commit

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧