如何使用ActionBarSherlock创建Activity后刷新Fragment

时间:2014-02-27 11:17:29

标签: android android-fragments actionbarsherlock android-fragmentactivity

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

我希望能够刷新Fragment,但不能刷新Activity,因为一切都将再次为null。

任何人都可以帮助我。提前谢谢。

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!");
    }

}

}

1 个答案:

答案 0 :(得分:0)

使用Interface使用Activity CallBacks。回调用于刷新片段。

此链接Communication With Fragments可以为您提供帮助。