用Tabhost和viewpager导航,数据丢失问题

时间:2014-11-27 10:12:54

标签: android android-fragments android-viewpager android-tabhost navigation-drawer

我使用滑动菜单构建一个应用程序,它有我使用viewpager创建的标签。我遇到了问题。第一次运行应用程序时,数据会在选项卡的第一个屏幕上显示。当我使用滑动菜单更改屏幕时,它会将我带到其他片段,当我回到主屏幕时,我在这些选项卡上的数据消失了。 我使用setRetainInstance(true);它存储视图但仅在我改变方向时。当我改变方向时,数据会显得过于严密。我无法找到这个问题。这是我的代码。

MainActivity

public class MainActivity extends FragmentActivity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    // nav drawer title
    private CharSequence mDrawerTitle;

    // used to store app title
    private CharSequence mTitle;

    // slide menu items
    private String[] navMenuTitles;
    private TypedArray navMenuIcons;

    private ArrayList<NavDrawerItem> navDrawerItems;
    private NavDrawerListAdapter adapter;


    ViewPager Tab;
    TabPagerAdapter TabAdapter;
    ActionBar actionBar;
     private FragmentTabHost mTabHost;
     private static final String TAG_TASK_FRAGMENT = "task_fragment";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);            

        mTitle = mDrawerTitle = getTitle();

        // load slide menu items
        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

        // nav drawer icons from resources
        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>();

        // adding nav drawer items to array
        // Home
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
        // Find People
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
        // Photos
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
        // Communities, Will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22"));
        // Pages
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
        // What's hot, We  will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));


        // Recycle the typed array
        navMenuIcons.recycle();

        mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

        // setting the nav drawer list adapter
        adapter = new NavDrawerListAdapter(getApplicationContext(),
                navDrawerItems);
        mDrawerList.setAdapter(adapter);

        // enabling action bar app icon and behaving it as toggle button
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, //nav menu toggle icon
                R.string.app_name, // nav drawer open - description for accessibility
                R.string.app_name // nav drawer close - description for accessibility
        ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            // on first time display view for first nav item
            displayView(0);
        }


    /*//Testing code for adding sliding fragment
          TabAdapter = new TabPagerAdapter(getSupportFragmentManager());

            Tab = (ViewPager)findViewById(R.id.pager);
            Tab.setOnPageChangeListener(
                    new ViewPager.SimpleOnPageChangeListener() {
                        @Override
                        public void onPageSelected(int position) {

                            actionBar = getActionBar();
                            actionBar.setSelectedNavigationItem(position);                    }
                    });
            Tab.setAdapter(TabAdapter);

            actionBar = getActionBar();
            //Enable Tabs on Action Bar
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            ActionBar.TabListener tabListener = new ActionBar.TabListener(){

                @Override
                public void onTabReselected(android.app.ActionBar.Tab tab,
                        FragmentTransaction ft) {
                    // TODO Auto-generated method stub

                }

                @Override
                 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

                    Tab.setCurrentItem(tab.getPosition());
                }

                @Override
                public void onTabUnselected(android.app.ActionBar.Tab tab,
                        FragmentTransaction ft) {
                    // TODO Auto-generated method stub

                }};
                //Add New Tab
                actionBar.addTab(actionBar.newTab().setText("Android").setTabListener(tabListener));
                actionBar.addTab(actionBar.newTab().setText("iOS").setTabListener(tabListener));
            //  actionBar.addTab(actionBar.newTab().setText("Windows").setTabListener(tabListener));
*/  

    }

    /**
     * Slide menu item click listener
     * */
    private class SlideMenuClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // display view for selected nav drawer item
            displayView(position);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // toggle nav drawer on selecting action bar app icon/title
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle action bar actions click
        switch (item.getItemId()) {
        case R.id.action_settings:
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    /* *
     * Called when invalidateOptionsMenu() is triggered
     */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // if nav drawer is opened, hide the action items
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    /**
     * Diplaying fragment view for selected nav drawer list item
     * */
    private void displayView(int position) {
        // update the main content by replacing fragments
        Fragment fragment = null;
        switch (position) {
        case 0:
            fragment = new HomeFragment();
            /*Intent i = new Intent(MainActivity.this,FrontActivity.class);
            startActivity(i);*/
            /*Intent firstpage= new Intent(MainActivity.this,HomeFragment.class);
            MainActivity.this.startActivity(firstpage);*/

//          Toast.makeText(MainActivity.this, "Home Pressed", Toast.LENGTH_SHORT).show();
            break;
        case 1:
            fragment = new FindPeopleFragment();

//          Toast.makeText(MainActivity.this, "Find People Pressed", Toast.LENGTH_SHORT).show();
    //      fragment = new HomeFragment();
            break;
        case 2:
            fragment = new PhotosFragment();
//          Toast.makeText(MainActivity.this, "Photo Fragment Pressed", Toast.LENGTH_SHORT).show();
    //      fragment = new HomeFragment();
            break;
        case 3:
            fragment = new CommunityFragment();
//          Toast.makeText(MainActivity.this, "Communities Pressed", Toast.LENGTH_SHORT).show();
        //  fragment = new HomeFragment();
            break;
        case 4:
            fragment = new PagesFragment();
//          Toast.makeText(MainActivity.this, "Pages Pressed", Toast.LENGTH_SHORT).show();
    //      fragment = new HomeFragment();
            break;
        case 5:
            fragment = new WhatsHotFragment();
//          Toast.makeText(MainActivity.this, "What's Hot Pressed", Toast.LENGTH_SHORT).show();
    //      fragment = new HomeFragment();
            break;

        default:
            break;
        }

        if (fragment != null) {

            System.out.println("*****on Fragment creation");
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment).commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(navMenuTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            // error in creating fragment
            Log.e("MainActivity", "Error in creating fragment");
        }
    }

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

    /**
     * When using the ActionBarDrawerToggle, you must call it during
     * onPostCreate() and onConfigurationChanged()...
     */

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

}

HomeFragment

public class HomeFragment extends Fragment {
    private TabHost mTabHost;
     private ViewPager mViewPager;
     private TabsPagerAdapter mTabsAdapter;
    public HomeFragment(){}
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        //setRetainInstance(true);
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        return rootView;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        if(savedInstanceState==null)
        {
            Toast.makeText(getActivity(), "onActivityCreated", Toast.LENGTH_SHORT).show();
            setRetainInstance(true);
            System.out.println("@@@ If Condition @@@@");
            mTabHost = (TabHost) getActivity().findViewById(android.R.id.tabhost);
            mTabHost.setup();
            Log.e("mTabHost", mTabHost + "");

            mViewPager = (ViewPager) getActivity().findViewById(R.id.viewpager);
            Log.e("mViewPager", mViewPager + "");
            mTabsAdapter = new TabsPagerAdapter((FragmentActivity) getActivity(), mTabHost, mViewPager, getChildFragmentManager());

            // Here we load the content for each tab. 
            mTabsAdapter.addTab(mTabHost.newTabSpec("Audience").setIndicator("Android"), Android.class, null);
            mTabsAdapter.addTab(mTabHost.newTabSpec("Critics").setIndicator("iOS"), Ios.class, null);
            Log.e("mTabsAdapter", mTabsAdapter + "");
        }
        else
        {
            System.out.println("@@@@@  Else condition  @@@@@");
        }


    }   


}

fragment_home

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TabHost
            android:id="@android:id/tabhost"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="0dp"
                    android:layout_height="0dp"/>

                <android.support.v4.view.ViewPager
                    android:id="@+id/viewpager"
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1" />

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >
                </TabWidget>

            </LinearLayout>
        </TabHost>

</RelativeLayout>

TabsPageradapter

 public class TabsPagerAdapter extends FragmentPagerAdapter
    implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final TabHost mTabHost;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

static final class TabInfo
{
    private final String tag;
    private final Class<?> clss;
    private final Bundle args;

    TabInfo(String _tag, Class<?> _class, Bundle _args)
    {
        tag = _tag;
        clss = _class;
        args = _args;
    }
}

static class DummyTabFactory implements TabHost.TabContentFactory
{
    private final Context mContext;

    public DummyTabFactory(Context context)
    {
        mContext = context;
    }

    public View createTabContent(String tag)
    {
        View v = new View(mContext);
        v.setMinimumWidth(0);
        v.setMinimumHeight(0);
        return v;
    }
}

public TabsPagerAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager, FragmentManager fragmentManager)
{
    //super(fragmentManager);
    super(activity.getSupportFragmentManager());
    mContext = activity;
    mTabHost = tabHost;
    mViewPager = pager;
    mTabHost.setOnTabChangedListener(this);
    mViewPager.setAdapter(this);
    mViewPager.setOnPageChangeListener(this);
}


public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args)
{
    tabSpec.setContent(new DummyTabFactory(mContext));
    String tag = tabSpec.getTag();

    TabInfo info = new TabInfo(tag, clss, args);
    mTabs.add(info);
    mTabHost.addTab(tabSpec);
    notifyDataSetChanged();
}

@Override
public int getCount()
{
    return mTabs.size();
}

@Override
public Fragment getItem(int position)
{
    TabInfo info = mTabs.get(position);

    return Fragment.instantiate(mContext, info.clss.getName(), info.args);

}

public void onTabChanged(String tabId)
{
    int position = mTabHost.getCurrentTab();
    mViewPager.setCurrentItem(position);

}

public String getTag()
{           
    return mTabHost.getCurrentTabTag();
}

public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
     int pos = this.mViewPager.getCurrentItem();
     this.mTabHost.setCurrentTab(pos);
}

public void onPageSelected(int position)
{
    // Unfortunately when TabHost changes the current tab, it kindly
    // also takes care of putting focus on it when not in touch mode.
    // The jerk.
    // This hack tries to prevent this from pulling focus out of our
    // ViewPager.
    TabWidget widget = mTabHost.getTabWidget();
    int oldFocusability = widget.getDescendantFocusability();
    widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
    mTabHost.setCurrentTab(position);
    widget.setDescendantFocusability(oldFocusability);
}

public void onPageScrollStateChanged(int state)
{
}
}

First screen where list showing

Second Screen from where i move to different screen

Third screen where i moved to

When i come back to screen its content is empty.

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,但能够解决它。 我认为在你的课程中你为Fragment导入了类 android.app 。 用android.support.v4.app替换此文件,这将解决您的问题。

android.app替换为android.support.v4.app