怎样在Fragment使用php application变量全局变量

[Andriod官方训练教程]使用Fragment创建一个动态的UI之与其他Fragments进行交互 - candycat
- 博客频道 - CSDN.NET
4185人阅读
原文地址:
-------------------------------------------------------------------------------------------------------------------------
In order to reuse the Fragment UI components, you should build each as a completely self-contained, modular component that defines its own layout and behavior. Once you have defined these reusable Fragments, you can associate them with an Activity and connect
them with the application logic to realize the overall composite UI.
为了重用Fragment UI部件,你应该每一个建成完全独立的、模块化的部件,定义了它自己的布局和行为。一旦你已经定义了这些可重用的Fragments,你可以将它们与一个Activity相关联,并将它们和应用逻辑相连接来实现整体复合的UI。
Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
一旦你想要让一个Fragment和另一个进行交流,例如基于用户时间改变内容。所有的Fragment-to-Fragment交互通过相关联的Activity来完成。两个Fragment永远不应该直接交互。
Define an Interface —— 定义一个接口
To allow a Fragment to communicate up to its Activity, you can define an interface in the Fragment class and implement it within the Activity. The Fragment captures the interface implementation during its onAttach() lifecycle method and can then call the
Interface methods in order to communicate with the Activity.
为了允许一个Fragment和它的Activity进行交互,你可以在Fragment类中定义一个接口,然后在Activity中实现它。Fragment在它的onAttach()生命周期方法中捕捉这个接口的实现,并且之后可以调用接口方法来达到和Activity交互的目的。
Here is an example of Fragment to Activity communication:
下面是一个Fragment和Activity交互的例子:
public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener mC
// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
mCallback = (OnHeadlineSelectedListener)
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ & must implement OnHeadlineSelectedListener&);
Now the fragment can deliver messages to the activity by calling the onArticleSelected() method (or other methods in the interface) using themCallback instance of theOnHeadlineSelectedListener interface.
现在,Fragment可以通过调用onArticleSelected()方法(或者其他接口中的方法)、使用OnHeadlineSelectedListener接口的实例mCallback向activity交付信息。
For example, the following method in the fragment is called when the user clicks on a list item. The fragment uses the callback interface to deliver the event to the parent activity.
例如,当用户在一个列表上点击时将调用下面的方法。Fragment使用回调函数接口来向父类activity交付事件。
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
mCallback.onArticleSelected(position);
Implement the Interface —— 实现接口
In order to receive event callbacks from the fragment, the activity that hosts it must implement the interface defined in the fragment class.
为了从Fragment中接收事件回调函数,持有它的activity必须实现在Fragment类中定义的接口。
For example, the following activity implements the interface from the above example.
例如,下面的activity实现了上述例子的接口。
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
Deliver a Message to a Fragment —— 向一个Fragment交付信息
The host activity can deliver messages to a fragment by capturing the
instance with,
then directly call the fragment's public methods.
主activity可以通过实例、使用向Fragment交付信息,然后直接调用Fragment的公共方法。
For instance, imagine that the activity shown above may contain another fragment that's used to display the item specified by the data returned in the above callback method. In this case, the activity can pass the information received in the callback method
to the other fragment that will display the item:
例如,想象上面演示的activity可以包含另一个Fragment来显示上述回调函数返回的数据。在这种情况下,activity可以向另一个显示它们的Fragment传递回调函数中接收的信息。
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
ArticleFragment articleFrag = (ArticleFragment)
getSupportFragmentManager().findFragmentById(R.id.article_fragment);
if (articleFrag != null) {
// If article frag is available, we're in two-pane layout...
// Call a method in the ArticleFragment to update its content
articleFrag.updateArticleView(position);
// Otherwise, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:434932次
积分:6242
积分:6242
排名:第1571名
原创:99篇
转载:20篇
译文:21篇
评论:961条
我叫乐乐,程序媛一枚,就读于上海交通大学软件学院,研究生,数字媒体方向。感兴趣的领域有人机交互,增强现实,游戏等等啦,我喜欢新奇好玩绚丽的东西和技术~
邮件:lelefeng1992 # gmail DOT com
PS:为防止垃圾邮件,请自行转换为正确格式哦~
阅读:15003
阅读:5385
文章:45篇
阅读:183417
(2)(4)(2)(3)(6)(3)(8)(5)(6)(3)(6)(5)(7)(1)(2)(8)(1)(4)(5)(4)(3)(15)(2)(3)(27)(5)ViewPage+Fragment替换Frament问题-中国学网-中国IT综合门户网站
> ViewPage+Fragment替换Frament问题
ViewPage+Fragment替换Frament问题
转载 编辑:李强
为了帮助网友解决“ViewPage+Fragment替换F”相关的问题,中国学网通过互联网对“ViewPage+Fragment替换F”相关的解决方案进行了整理,用户详细问题包括:viewpagerFragment适配器开始ViewPage有LoadCourseFragment、HomeWorkFragment、MeFragment三个Fragment,其中LoadCourseFragment是提示加载课程的(其中有进度条),等加载成功(这个我是模拟3s加载成功的),LoadCourseFragment替换成AllCourseFragment,操作代码如下:&//加载课程成功后更新界面&&&&Handler&handler&=&new&Handler()&{&&&&&&&&@Override&&&&&&&&public&void&handleMessage(Message&msg)&{&&&&&&&&&&&&if&(msg.what&==&ACTION_DESTORY)&{&&&&&&&&&&&&&&&&try&{&&&&&&&&&&&&&&&&&&&&Thread.sleep(1000);&&&&&&&&&&&&&&&&&&&&FragmentManager&fragmentManager&=&getSupportFragmentManager();&&&&&&&&&&&&&&&&&&&&FragmentTransaction&fragmentTransaction&=&fragmentManager.beginTransaction();&&&&&&&&&&&&&&&&&&&&fragmentTransaction.replace(R.id.loadCourse,&allCourseFragment);//替换LoadCourseFragment&&&&&&&&&&&&&&&&&&&&mit();&&&&&&&&&&&&&&&&}&catch&(InterruptedException&e)&{&&&&&&&&&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&&&&&&&&}&&&&&&&&&&&&}&&&&&&&&}&&&&};但是ViewPage滑动到第三个Fragment,即MeFragment,再滑动到第一个的时候是LoadCourseFragment,不是AllCourseFragment(我想让它是AllCourseFragment,ViewPage只缓存相邻的一个页面,Fragment被销毁重建了),后来看网上的资料,说用FragmentStatePagerAdapter适配器,然后重写getItemPosition()方法,返回POSITION_NONE,如果还用FragmentPagerAdapter需要重写两个方法,然后adapter.notifyDataSetChanged()&;通知数据改变,重新加载,但是依然没有解决问题,请求大神。适配器代码如下:package&com.chzu.ntp.import&android.support.v4.app.Fimport&android.support.v4.app.FragmentMimport&android.support.v4.app.FragmentPagerAimport&android.support.v4.app.FragmentStatePagerAimport&android.support.v4.app.FragmentTimport&android.view.ViewGimport&java.util.L/**&*&@author&yanxing&*&&&&&&&&&ViewPager适配器,把Fragment视图装载到ViewPager中&*/public&class&FragAdapter&extends&FragmentStatePagerAdapter&{&&&&private&List&Fragment&&//存放Fragments界面&&&&/**&&&&&*&构造方法,初始化Fragment管理器和Fragment集合&&&&&*&&&&&*&@param&fm&&&&&&&&Fragment管理器&&&&&*&@param&fragments&Fragment集合&&&&&*/&&&&public&FragAdapter(FragmentManager&fm,&List&Fragment&&fragments)&{&&&&&&&&super(fm);&&&&&&&&this.fragments&=&&&&&}&&&&@Override&&&&public&Fragment&getItem(int&i)&{&&&&&&&&return&fragments.get(i);&&&&}&&&&@Override&&&&public&int&getCount()&{&&&&&&&&return&fragments.size();&&&&}&&&&public&FragAdapter(FragmentManager&fm)&{&&&&&&&&super(fm);&&&&}&&&&@Override&&&&public&int&getItemPosition(Object&object)&{&&&&&&&&return&POSITION_NONE;&&&&}}整个项目代码:/yanxing/ntp-android,具体解决方案如下:解决方案1:可不可以设置一个全局变量,记录加载load还是all,然后每次加载第一个fragment的时候在oncreateview里根据变量做判断,是加载load还是all解决方案2:引用&1&楼&u&的回复:可不可以设置一个全局变量,记录加载load还是all,然后每次加载第一个fragment的时候在oncreateview里根据变量做判断,是加载load还是all我的需求是,每次应用被销毁后,或者第一次加载应用或者缓存被清除,会出来load,这里模拟停顿3s,然后调到all,你那样就是没有出现load,即使出现,加载成功,还是会调到all,viewpage重建Fragment还是用的load,我现在目前是这样解决的,让ViewPage缓存两个页面,viewPager.setOffscreenPageLimit(2);解决方案3:相信你在ViewPager里面添加Fragment用的是FragmentPagerAdapter可以再FragmentPagerAdapter增加修改Fragment的接口private&class&MyFragmentPagerAdapter&extends&FragmentPagerAdapter{private&List&Fragment&&mLpublic&MyFragmentPagerAdapter(FragmentManager&fragmentManager,List&Fragment&&list){super(fragmentManager);mList&=&}public&void&setFragment(Fragment&fragment,&int&position){mList.set(position,&fragment);this.notifyDataSetChanged();}@Overridepublic&int&getCount(){return&mList.size();}@Overridepublic&Fragment&getItem(int&arg0){return&mList.get(arg0);}}解决方案4:引用&3&楼&wangdong20&的回复:相信你在ViewPager里面添加Fragment用的是FragmentPagerAdapter可以再FragmentPagerAdapter增加修改Fragment的接口private&class&MyFragmentPagerAdapter&extends&FragmentPagerAdapter{private&List&Fragment&&mLpublic&MyFragmentPagerAdapter(FragmentManager&fragmentManager,List&Fragment&&list){super(fragmentManager);mList&=&}public&void&setFragment(Fragment&fragment,&int&position){mList.set(position,&fragment);this.notifyDataSetChanged();}@Overridepublic&int&getCount(){return&mList.size();}@Overridepublic&Fragment&getItem(int&arg0){return&mList.get(arg0);}}不行,&fragAdapter.setFragment(allCourseFragment,0);然后没有作用,/yanxing/ntp-android项目地址,你可以调试下解决方案5:引用&2&楼&yanxing2012&的回复:Quote: 引用&1&楼&u&的回复:可不可以设置一个全局变量,记录加载load还是all,然后每次加载第一个fragment的时候在oncreateview里根据变量做判断,是加载load还是all我的需求是,每次应用被销毁后,或者第一次加载应用或者缓存被清除,会出来load,这里模拟停顿3s,然后调到all,你那样就是没有出现load,即使出现,加载成功,还是会调到all,viewpage重建Fragment还是用的load,我现在目前是这样解决的,让ViewPage缓存两个页面,viewPager.setOffscreenPageLimit(2);你可以在第一个Fragment也就是LoadCourseFragment的onCreateView()方法里面做一个判断,如果课程已经加载了就直接跳到课程的allCourseFragment,否则就不做跳转至于怎么做这个判断,你要用一些具体的方法,总之每次启动LoadCourseFragment都要做检测判断解决方案6:第一次显示的是:LoadCourseFragment&&当加载完的时候自动替换成&allFragment,你现在的情况是&当从viewPager其他页面返回第一个页面的时候显示的不是allFragment这个页面,而是&LoadCourseFragment&这个页面是吧?如果是这样的话你可以再多加一个rootContainer。添加一个framelayout拉力当作&rootContainer,第一次启动的时候将LoadCourseFragment&添加到rootContainer中,当加载完之后再把allFragment添加到rootContainer中。这样应该可以满足你的需求了。这样你要对第一个页面的onBackpress的事件进行适当的处理解决方案7:引用&5&楼&wangdong20&的回复:Quote: 引用&2&楼&yanxing2012&的回复:Quote: 引用&1&楼&u&的回复:可不可以设置一个全局变量,记录加载load还是all,然后每次加载第一个fragment的时候在oncreateview里根据变量做判断,是加载load还是all我的需求是,每次应用被销毁后,或者第一次加载应用或者缓存被清除,会出来load,这里模拟停顿3s,然后调到all,你那样就是没有出现load,即使出现,加载成功,还是会调到all,viewpage重建Fragment还是用的load,我现在目前是这样解决的,让ViewPage缓存两个页面,viewPager.setOffscreenPageLimit(2);你可以在第一个Fragment也就是LoadCourseFragment的onCreateView()方法里面做一个判断,如果课程已经加载了就直接跳到课程的allCourseFragment,否则就不做跳转至于怎么做这个判断,你要用一些具体的方法,总之每次启动LoadCourseFragment都要做检测判断迟早要跳转的,我要让用户看到这个跳转解决方案8:引用&6&楼&WantD&的回复:第一次显示的是:LoadCourseFragment&&当加载完的时候自动替换成&allFragment,你现在的情况是&当从viewPager其他页面返回第一个页面的时候显示的不是allFragment这个页面,而是&LoadCourseFragment&这个页面是吧?如果是这样的话你可以再多加一个rootContainer。添加一个framelayout拉力当作&rootContainer,第一次启动的时候将LoadCourseFragment&添加到rootContainer中,当加载完之后再把allFragment添加到rootContainer中。这样应该可以满足你的需求了。这样你要对第一个页面的onBackpress的事件进行适当的处理是你说这种情况,能具体点吗,或者你看下我源码,说下通过对数据库的索引,我们还为您准备了:问:默认加载A 里面的数据,为什么点了B,C,D 再切换回A,A的数据没了, onSt...答:因为你的Fragment嵌套了 Fragment里面如果还有Fragment 嵌套的时候,不能用Activity的FragmentManager 即不能使用getActivity().getFragmentManager() 而要使用getChildFragmentManager() 来获取FragmentManager 对象===========================================问:默认加载A 里面的数据,为什么点了B,C,D 再切换回A,A的数据没了, onSt...答:setUserVisibleHint就是告诉系统,fragment可见的时候才加载数据 如果fragment不多的话, 可以在activty里面直接初始化默认内容,等到翻个某个fragment的时候再加载网络或者本地内容。 ViewPager默认会缓存三页,即当前页和左右两页。 滑动到第...===========================================问:默认加载A 里面的数据,为什么点了B,C,D 再切换回A,A的数据没了, onSt...答:由于在ViewPage中PageAdapter来管理所有的Fragment。在加载一个Fragment的时候,会自动缓存左右几个(默认是一个)页面,此时也会调用到正常的生命周期函数,onCreate,onCrateView,onResume.可是这样就干扰了我们统计页面打开次数。 例如:一个V...===========================================问:手机开发。 老师要求在一个activity中一条ActionBar(3格)分别对应3个F...答:/content/14/035.shtml /kf/986.html 这边有两个关于这个问题的,讲的都挺细的,希望能帮到你===========================================问:不是有tab的那种,就单纯的一个页面,左右滑动切换页面的效果。答:提供一下思路,两种实现方式: 1.ViewPage+fragment/activity实现,如果你不熟悉fragment就用activity来实现,具体控件使用不多说,api文档有详细说明,但是建议你...===========================================问:不是有tab的那种,就单纯的一个页面,左右滑动切换页面的效果。答:Fragment fragement = getSupportFragmentManager(). findFragmentByTag("android:switcher:"+R.id.ViewPage+":" + selectTab); 这个Tag可以从FragmentPagerAdapter的源码中找到,如下: @Override public Object instantiateItem(ViewGroup cont...===========================================问:一个activity中包含的两个Fragment,我希望当我获得一个通知的点击事件...答:1、在你的activity中可以定义一个方法,如:getPager();这个方法返回viewpage; 2、在fragment里面使用((xxxActivity)getActivity()).getPager();就能拿到viewpage了。 在2中,注意要强转一下。把它强转为你的activity===========================================问:一个activity中包含的两个Fragment,我希望当我获得一个通知的点击事件...答:内存溢出跟ViewPager嵌套fragment没有关系。===========================================问:使用SlidingMenu 开源的包实现了左侧滑动菜单的功能,结合ViewPage完成...答:Fragment的出现,让android系统上的应用界面变得更丰富。 我觉得我们可以把Fragment看做是一个Activity,Activity在程序运行时被创建,而Fragment在Activity运行时被创建,可以说Fragment是活在Activity中的一个Activity。===========================================
本文欢迎转载,转载请注明:转载自中国学网: []
用户还关注
可能有帮助fragment之间通信
[问题点数:50分,结帖人oqqneet12]
fragment之间通信
[问题点数:50分,结帖人oqqneet12]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
相关帖子推荐:
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。无法找到该页
无法找到该页
您正在搜索的页面可能已经删除、更名或暂时不可用。
请尝试以下操作:
确保浏览器的地址栏中显示的网站地址的拼写和格式正确无误。
如果通过单击链接而到达了该网页,请与网站管理员联系,通知他们该链接的格式不正确。
单击按钮尝试另一个链接。
HTTP 错误 404 - 文件或目录未找到。Internet 信息服务 (IIS)
技术信息(为技术支持人员提供)
转到 并搜索包括&HTTP&和&404&的标题。
打开&IIS 帮助&(可在 IIS 管理器 (inetmgr) 中访问),然后搜索标题为&网站设置&、&常规管理任务&和&关于自定义错误消息&的主题。

我要回帖

更多关于 application全局变量 的文章

 

随机推荐