一共有4个fragment,分别是contact(联系人),friends(朋友),search(查找),more(更多)。使用的都是同一个布局,每个fragment中都有四个内部按钮,可以切换到其他的3个fragment中。

现在只考虑在contact中,实现点4个(其实有效的是3个),切换到其他3个fragment中。

现在的情况是,点击contactfragment中的按钮,toast会响应,但是fragment不会跳转。这是怎么回事,下面是两个类的代码。

这是contactfragment。

public class ContactFragment extends Fragment implements OnClickListener {
MainActivity activity = (MainActivity) getActivity();
View view = null;
Button btn_inner_contact;
Button btn_inner_friends;
Button btn_inner_search;
Button btn_inner_more; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frag_content, container, false);
TextView textview_hint = (TextView) view
.findViewById(R.id.textview_hint);
textview_hint.setText("Contact");
initComponent();
return view;
} private void initComponent() {
btn_inner_contact = (Button) view
.findViewById(R.id.button_inner_contact);
btn_inner_friends = (Button) view
.findViewById(R.id.button_inner_friends);
btn_inner_search = (Button) view.findViewById(R.id.button_inner_search);
btn_inner_more = (Button) view.findViewById(R.id.button_inner_more); btn_inner_contact.setOnClickListener(this);
btn_inner_friends.setOnClickListener(this);
btn_inner_search.setOnClickListener(this);
btn_inner_more.setOnClickListener(this);
} @Override
public void onClick(View v) { FragmentManager fm = activity.getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
switch (v.getId()) {
case R.id.button_inner_contact:
Toast.makeText(getActivity(), "点击了内部contact按钮", Toast.LENGTH_SHORT)
.show();
if (activity.contactFragment == null) {
activity.contactFragment = new ContactFragment();
}
ft.replace(R.id.framelayout_content, activity.contactFragment); break;
case R.id.button_inner_friends:
Toast.makeText(getActivity(), "点击了内部friends按钮", Toast.LENGTH_SHORT)
if (activity.friendsFragment == null) {
activity.friendsFragment = new FriendsFragment();
}
ft.replace(R.id.framelayout_content, activity.friendsFragment);
break;
case R.id.button_inner_more:
if (activity.moreFragment == null) {
activity.moreFragment = new MoreFragment();
}
ft.replace(R.id.framelayout_content, activity.moreFragment);
break;
case R.id.button_inner_search:
if (activity.searchFragment == null) {
activity.searchFragment = new SearchFragment();
}
ft.replace(R.id.framelayout_content, activity.searchFragment);
break;
default:
break; }
ft.commit();
} }

这是mainactivity类,mainactivity中的那四个按钮及其响应时间是fragment外部的四个按钮,与内部的inner按钮没关系

public class MainActivity extends Activity implements OnClickListener {
public ContactFragment contactFragment;
public FriendsFragment friendsFragment;
public MoreFragment moreFragment;
public SearchFragment searchFragment;
private Button btn_contact;
private Button btn_friends;
private Button btn_search;
private Button btn_more; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initcomponet();
initview(); } private void initview() {
contactFragment = new ContactFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.framelayout_content, contactFragment);
ft.commit();
} private void initcomponet() { btn_contact = (Button) findViewById(R.id.button_contact);
btn_friends = (Button) findViewById(R.id.button_friends);
btn_more = (Button) findViewById(R.id.button_more);
btn_search = (Button) findViewById(R.id.button_search);
btn_contact.setOnClickListener(this);
btn_friends.setOnClickListener(this);
btn_more.setOnClickListener(this);
btn_search.setOnClickListener(this);
} @Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
switch (v.getId()) {
case R.id.button_contact:
if (contactFragment == null) {
contactFragment = new ContactFragment();
}
ft.replace(R.id.framelayout_content, contactFragment); break;
case R.id.button_friends:
if (friendsFragment == null) {
friendsFragment = new FriendsFragment();
}
ft.replace(R.id.framelayout_content, friendsFragment); break;
case R.id.button_more:
if (moreFragment == null) {
moreFragment = new MoreFragment();
}
ft.replace(R.id.framelayout_content, moreFragment); break;
case R.id.button_search:
if (searchFragment == null) {
searchFragment = new SearchFragment();
}
ft.replace(R.id.framelayout_content, searchFragment);
break; default:
break;
}
ft.commit();
} }
我试了一下,把MainActivity activity=(MainActivity)getActivity();这行代码放到onclick方法了,就可以了!

android fragment 跳到另一个fragment的更多相关文章

  1. android studio 2.2.2下fragment的创建和跳转

    一,首先,Fragment是android应用中十分重要的一个功能,十分轻量化,也类似于activity一样,是一个个布局,可以相互跳转和传递参数.但是,它运行起来十分流畅,而且易于管理,下面是在学习 ...

  2. 【Android Developers Training】 20. 创建一个Fragment

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  3. Android学习路线(二十一)运用Fragment构建动态UI——创建一个Fragment

    你能够把fragment看成是activity的模块化部分.它拥有自己的生命周期,接受它自己的输入事件,你能够在activity执行时加入或者删除它(有点像是一个"子activity&quo ...

  4. Android UI开发详解之Fragment

    Fragment是Android自从3.0之后新加入的一个组件,我相信很多人都已经听说过这个组件了,但这个组件到底是个什么,如何去使用他呢,且听我讲来. 以下部分资料来自官网(官网才是王道,其他都是浮 ...

  5. Android Fragment详解(二):Fragment创建及其生命周期

    Fragments的生命周期 每一个fragments 都有自己的一套生命周期回调方法和处理自己的用户输入事件. 对应生命周期可参考下图: 创建片元(Creating a Fragment) To c ...

  6. Android开发之漫漫长途 XII——Fragment详解

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  7. Android为TV端助力 关于Fragment你所需知道的一切!

    转载自刘明渊 的博客地址:http://blog.csdn.net/vanpersie_9987 Fragment 是 Android API 中的一个类,它代表Activity中的一部分界面:您可以 ...

  8. 怎样在一个fragment or 随意类中操作还有一个fragment中的方法

    1 怎样在acitivty中运行fragment中的方法: 首先获得这个Fragment的对象 xxxFragment fragmentObject = (xxxFragment) getFragme ...

  9. Android从一个Fragment跳转到另一个Fragment后原来的组件不消失

    问题描述 Activity上放置了一个Fragment,Fragment上有按钮,点了按钮后,应该跳转到另一个Fragment, but 原来的Fragment的按钮不会消失,新的Fragment不是 ...

随机推荐

  1. This application failed to start because it could not find or load the Qt platform plugin "windows"

    发生以上错误的Qt版本应该是Qt 5.0.0以上的版本吧. 出现标题错误的大致原因是:因为Qt是跨平台的库,需要依赖于相关的平台库.有个Platform的文件夹,里面有相关dll文件. referen ...

  2. 【转】vim文件编码和乱码处理

    原文网址:http://edyfox.codecarver.org/html/vim_fileencodings_detection.html 在 Vim 中,有四个与编码有关的选项,它们是:file ...

  3. c++ 09

    一.数据结构 程序设计=数据结构+算法 1.逻辑结构 1)集合:元素之间没有联系. 2)线性结构:元素之间存在前后顺序. 3)树形结构:元素之间存在一对多的父子关系. 4)图状结构:元素之间存在多对多 ...

  4. Visual Assist X在Windows 8.1下出现中文乱码的解决方法

    这主要是输入法造成的,我的输入法中有US.中文.搜狗输入法三个输入法:通过搜狗输入法管理器把“中文”去掉,或者通过语言首选项把“中文”去掉就不会在出现乱码. 这个办法的思路来自于http://www. ...

  5. 【POJ1003】Hangover(二分搜索)

    直接用库函数二分即可. #include <iostream> #include <cstring> #include <cstdlib> #include < ...

  6. [置顶] vi、akw和sed总结

  7. 使用cx_Freeze 将python3代码打包成.exe程序

    在这里分享一下如何在py3下使用cx_Freeze打包pyqt5的程序 首先吐槽下,深深鄙视一下百度,各种百度各种没有,之前我在py2.7下使用pyqt4开发过一个小软件,用的是py2exe进行打包的 ...

  8. 集合操作出现的ConcurrentModificationException(源码分析)

    摘要: 为了保证线程安全,在迭代器迭代的过程中,线程是不能对集合本身进行操作(修改,删除,增加)的,否则会抛出ConcurrentModificationException的异常. 示例: publi ...

  9. ios获取权限

    ios获取权限 by 伍雪颖 -(void)requestRecord{ [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL ...

  10. Dalvik虚拟机JNI方法的注册过程分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8923483 在前面一文中,我们分析了Dalvi ...