我想添加一个片段到一个活动,以编程方式实现其布局。 我查看了Fragment文档,但没有很多例子描述我需要什么。 这是我试图写的代码的types: public class DebugExampleTwo extends Activity { private ExampleTwoFragment mFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FrameLayout frame = new FrameLayout(this); if (savedInstanceState == null) { mFragment = new ExampleTwoFragment(); FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.add(frame.getId(), mFragment).commit(); } setContentView(frame); } } … public class ExampleTwoFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, […]
Googlebuild议我们使用DialogFragment而不是使用Fragments API的简单Dialog ,但是使用隔离的DialogFragment作为简单的Yes-No确认消息框是荒谬的。 这种情况下的最佳做法是什么?
调用这些方法的主要区别是什么: fragmentTransaction.addToBackStack(name); fragmentTransaction.replace(containerViewId, fragment, tag); fragmentTransaction.add(containerViewId, fragment, tag); 这是什么意思replace一个已经存在的片段,并添加一个片段的活动状态,并添加一个活动到后面的堆栈? 其次,使用findFragmentByTag() ,是否通过add() / replace()方法或addToBackStack()方法add()标签?
我试图使用FragmentPagerAdapter与ViewPager使用片段。 我正在寻找的是将位于ViewPager第一页的片段replace为另一个片段。 寻呼机由两页组成。 第一个是FirstPagerFragment,第二个是SecondPagerFragment。 点击第一页的button。 我想用NextFragmentreplaceFirstPagerFragment。 下面是我的代码。 public class FragmentPagerActivity extends FragmentActivity { static final int NUM_ITEMS = 2; MyAdapter mAdapter; ViewPager mPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_pager); mAdapter = new MyAdapter(getSupportFragmentManager()); mPager = (ViewPager) findViewById(R.id.pager); mPager.setAdapter(mAdapter); } /** * Pager Adapter */ public static class MyAdapter extends FragmentPagerAdapter { public MyAdapter(FragmentManager […]
我已经更新了我的应用程序以使用最新的支持库(版本23.0.0),我发现他们弃用了Fragment类的onAttach()函数。 代替: onAttach (Activity activity) 下雪了: onAttach (Context context) 由于我的应用程序使用通过弃用之前通过的活动,我认为一个可能的解决scheme是: @Override public void onAttach(Context context) { super.onAttach(context); activity = getActivity(); } 那会是正确的做法吗? 更新: 如果我运行API低于23的设备,新的onAttach()甚至不被称为。 我希望这不是他们打算做的! 更新2: 问题已经解决与SDK的最新更新。 我已经在我的API 22设备上进行了testing,onAttach(Context)正在被调用。 点击这里查看几星期前我已经打开的错误报告以及Google的家伙的答案。
我有一个三个选项卡的应用程序。 每个选项卡都有自己的布局.xml文件。 main.xml有自己的地图片段。 这是应用程序首次启动时显示的那个。 一切工作正常,除了当我改变选项卡之间。 如果我尝试切换回地图片段选项卡,我得到这个错误。 切换到其他标签和之间工作得很好。 这里有什么可能是错的? 这是我的主类,我的main.xml,以及我使用的相关类(你也可以在底部find错误日志) 主要类 package com.nfc.demo; import android.app.ActionBar; import android.app.ActionBar.Tab; import android.app.Activity; import android.app.Fragment; import android.app.FragmentTransaction; import android.os.Bundle; import android.widget.Toast; public class NFCDemoActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActionBar bar = getActionBar(); bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); bar.addTab(bar .newTab() .setText("Map") .setTabListener( new TabListener<MapFragment>(this, "map", MapFragment.class))); bar.addTab(bar .newTab() […]
我想在Android Fragments中有一个选项菜单。 ActionBar选项菜单不显示在我的片段。 这里是我的代码,我有onCreateOptionsMenu()和onOptionSelected()函数。 我的代码不显示任何错误。 但是选项菜单不显示。 package org.reachout; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import org.general.R; public class ViewMessageFragment extends Fragment { /* (non-Javadoc) * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (container == null) { // We have different […]
我正在使用这个参考的代码。 当我把代码放在我的程序中时,会出现如下图所示的错误。 任何错误的原因? The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, ExampleFragments) 来自我主要活动的代码: public void red(View view) { android.app.FragmentManager fragmentManager = getFragmentManager(); android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); ExampleFragments fragment = new ExampleFragments(); fragmentTransaction.replace(R.id.frag, fragment); fragmentTransaction.commit(); } ExampleFragments.java package com.example.learn.fragments; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; […]
我有一个ViewPager,每个页面是一个片段视图。 我想testing一个片段是否在可见区域。 Fragment.isVisible唯一的testing 该片段被附加到一个活动 该片段设置为可见 该片段已被添加到视图 ViewPager将创build3个(默认)片段,并且它们全部三个满足上述标准,但是只有一个实际上对于用户(人眼 )是可见的,
我有一个ViewPager与多个片段。 在一个Fragment我播放audio。 当我滑动到另一个片段时,我想停止audio播放。 如何检测另一个片段现在在ViewPager可见? 我试过重写onStop和onHiddenChanged 。 没有成功。 必须有一些“你不活跃了”的方法来覆盖。 没有?