【问题标题】:How do I refresh Fragment after Activity is created using ActionBarSherlock使用 ActionBarSherlock 创建 Activity 后如何刷新 Fragment
【发布时间】:2014-02-27 11:17:28
【问题描述】:

我想在运行时创建东西,并且我的 FragmentActivity 中有我想要设置的全局变量,然后它们被 onActivityCreated() 方法中的 Fragments 使用。但显然它们在创建时为空,直到创建 Fragment 之后我才需要使用数据。有没有人想出一个好的方法来做到这一点?我目前正在使用 ActionBarSherlock 来执行此操作。

我希望能够刷新 Fragment,而不是 Activity,因为那样一切都会再次为空。

请任何人帮助我解决这个问题。提前谢谢你。

FragmentActivity 代码:

public class SlidingTabsActivity extends SherlockFragmentActivity
{   

/* Sliding tabs */
private ViewPager viewPager;
private TabsAdapter tabsAdapter;
private ActionBar actionBarTabs;

/* Custom tab view */
private LayoutInflater customTabViewLayoutInflater;
private View customTabView;

/* Layer interfaces */
private CommsLayerInterface commsLayerInterface;
private DeviceLayerInterface deviceLayerInterface;

private PopupFirmware popupFirmware; // Popup firmware class instance

/* Data set information */
private DeviceInfo deviceInfo;
private Pages pages;
private Page page1;
private Page page2;
private Page page3;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    viewPager = new ViewPager(this);
    viewPager.setId(R.id.pager);
    setContentView(viewPager);

    tabsAdapter = new TabsAdapter(this, viewPager); // Declares the tabs adapter class with the view pager view

    popupFirmware = new PopupFirmware(this); // Declaring popup firmware class

    commsLayerInterface = new CommsLayerInterface();
    deviceLayerInterface = new DeviceLayerInterface(this);

    deviceLayerInterface.setCommsLayerInterface(commsLayerInterface);

    /* Custom tab view instances */
    customTabViewLayoutInflater = (LayoutInflater) this.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    customTabView = customTabViewLayoutInflater.inflate(R.layout.tabs_spinner, null);

    /* Custom view functions */
    TabCustomViewFunctions.createSpinnerList(this, customTabView, deviceLayerInterface);
    TabCustomViewFunctions.createNewFile(this, customTabView, deviceLayerInterface);
    TabCustomViewFunctions.saveFile(customTabView, deviceLayerInterface);

    /* Set up pages and device info */
    deviceInfo = deviceLayerInterface.getDeviceInfo();
    pages = deviceLayerInterface.getPages();
    setUpPages(pages);

    /* Action Bar */
    actionBarTabs = getSupportActionBar();
    actionBarTabs.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBarTabs.setCustomView(customTabView);
    actionBarTabs.setDisplayShowCustomEnabled(true);    

    /* Adds fragments to the tabs adapter */
    tabsAdapter.addTab(actionBarTabs.newTab().setText("FRAG1"), Fragment_1.class, null);
    tabsAdapter.addTab(actionBarTabs.newTab().setText("FRAG2"), Fragment_2.class, null);
    tabsAdapter.addTab(actionBarTabs.newTab().setText("FRAG3"), Fragment_3.class, null);

}
}

片段代码:

public class Fragment_1 extends SherlockFragment
{
private View view;
private CustomLayout layout; // Container view layout class instance
private SlidingTabsActivity slidingTabsActivity;
private DeviceInfo deviceInfo;
private Page page;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    view = inflater.inflate(R.layout.fragment_custom, container, false);

    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) 
{
    super.onActivityCreated(savedInstanceState); 

    /* Creates the new views from the layout class */
    slidingTabsActivity = (SlidingTabsActivity) getSherlockActivity();
    deviceInfo = slidingTabsActivity.getDeviceInfo();
    page = slidingTabsActivity.getPage1(); 

    if(page != null & deviceInfo != null)
    {
        layout = new CustomLayout(slidingTabsActivity);
        layout.setDeviceInfo(deviceInfo);
        layout.setPage(page);
        layout.createView();

        System.out.println("Page and device info are NOT null!");
    }
    else
    {
        System.out.println("Page and device info are null!");
    }

}

}

【问题讨论】:

    标签: android android-fragments actionbarsherlock android-fragmentactivity


    【解决方案1】:

    使用接口使用活动回调。回调用于刷新片段。

    这个链接Communication With Fragments会帮助你。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-19
      • 2013-03-11
      • 1970-01-01
      • 1970-01-01
      • 2014-06-24
      • 1970-01-01
      相关资源
      最近更新 更多