切换时Android片段停止

时间:2014-01-26 21:00:29

标签: android android-fragments android-webview

我的片段每次重新选择时都会重新加载。我需要片段继续它离开的地方,好像片段在取消选择时被最小化了。我在我的网络浏览器应用程序中使用片段作为标签。

问题:

每次重新选择时,如何停止重新加载片段。

以下是MainActivity。谢谢。

public class MainActivity extends Activity {

private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;


private CharSequence mDrawerTitle;

private CharSequence mTitle;

private String[] navMenuTitles;
private TypedArray navMenuIcons;

private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTitle = mDrawerTitle = getTitle();

    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

    navMenuIcons = getResources()
            .obtainTypedArray(R.array.nav_drawer_icons);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

    navDrawerItems = new ArrayList<NavDrawerItem>();

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));

    navMenuIcons.recycle();

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    adapter = new NavDrawerListAdapter(getApplicationContext(),
            navDrawerItems);
    mDrawerList.setAdapter(adapter);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer,
            R.string.app_name,
            R.string.app_name
    ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        displayView(0);
    }
}
private class SlideMenuClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        displayView(position);
    }
}
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    switch (item.getItemId()) {
    case R.id.action_settings:
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}

private void displayView(int position) {
    Fragment fragment = null;

    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;
    case 1:
        fragment = new Fragment2();
        break;
    case 2:
        fragment = new Fragment3();
        break;
    case 3:
        fragment = new Fragment4();
        break;
    case 4:
        fragment = new Fragment5();
        break;
    case 5:
        fragment = new Fragment6();
        break;

    default:
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();

        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        Log.e("MainActivity", "Error in creating fragment");
    }
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}
}

2 个答案:

答案 0 :(得分:1)

您需要改进TabListener。

这就是每次切换后再次加载标签的原因:

.replace(R.id.frame_container, fragment).commit();

最佳解决方案是here.

答案 1 :(得分:0)

这是我的一个应用程序中的MyTabsListener.java类的示例:

class MyTabsListener<T extends Fragment> implements ActionBar.TabListener {
        private Fragment mFragment;
        private final Activity mActivity;
        private final String mTag;
        private final Class<T> mClass;
        EditText mEditText;




        public MyTabsListener(Activity activity, String tag, Class<T> clz){
             mActivity = activity;
                mTag = tag;
                mClass = clz;
    }



        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // Check if the fragment is already initialized
            if (mFragment == null) {
                // If not, instantiate and add it to the activity
                mFragment = Fragment.instantiate(mActivity, mClass.getName());
                ft.add(android.R.id.content, mFragment, mTag);
            } else {
                // If it exists, simply attach it in order to show it
                ft.show(mFragment);
            }
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            if (mFragment != null) {
                // Detach the fragment, because another one is being attached
                ft.hide(mFragment);
            }
            }



        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {


        }

        }

在MainActivity.java中:

- 你的onCreate(Bundle savedInstanceState)至少需要这样做:

ActionBar actionbar = getSupportActionBar();
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab Animals = actionbar.newTab().setText("Animals");
        ActionBar.Tab Frag2Tab = actionbar.newTab().setText("Favorites");
        ActionBar.Tab Frag3Tab = actionbar.newTab().setText("Settings");


        Fragment Fragment1 = new Fragment1(); 

// new Fragment1()是片段的//类的名称,依此类推......

        Fragment Fragment2 = new Fragment2();
        Fragment Fragment3 = new Fragment3();




        Animals.setTabListener(new MyTabsListener<Fragment1>(
                this, "Animals", Fragment1.class));
        Frag2Tab.setTabListener(new MyTabsListener<Fragment2>(
                this, "Favorites", Fragment2.class));
        Frag3Tab.setTabListener(new MyTabsListener<Fragment3>(
                this, "Settings", Fragment3.class));


        actionbar.addTab(Animals);
        actionbar.addTab(Frag2Tab);
        actionbar.addTab(Frag3Tab);

如果您有任何问题,请询问