从导航抽屉中选择其他选项并返回后,导航抽屉内的左侧寻呼机不会显示片段

时间:2016-03-11 13:40:43

标签: java android android-fragments navigation android-viewpager

感谢您的关注

我在导航抽屉片段中实现viewpager时遇到了一些问题。例如,我有一个带有两个片段fragment1和fragment2的导航抽屉,如图所示。当活动开始时,fragment1显示其viewpager片段很好。但是当我转到片段2并返回到viewpager内的fragment1片段时,只会消失并显示空白。如果您有任何答案,请提出一些答案。

截图 enter image description here

提前致谢

MainActivity.java

package com.ansoft.shutterbox;

import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends FragmentActivity {

public static DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
ArrayList<ListItemData> data;
// 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;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    insertData();
    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>();

    // 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)));
    // 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)));

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


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

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

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


    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) {
            // calling onPrepareOptionsMenu() to show action bar icons
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            // 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);
    }
    int width=getResources().getDisplayMetrics().widthPixels/3;
    DrawerLayout.LayoutParams params=(android.support.v4.widget.DrawerLayout.LayoutParams)mDrawerList.getLayoutParams();
    params.width=width;
    mDrawerList.setLayoutParams(params);
}

/**
 * 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
protected void onResume() {
    Toast.makeText(getApplicationContext(), "Resumed", Toast.LENGTH_LONG).show();
    super.onResume();
}

/**
 * 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();
            break;
        case 1:
            fragment = new MyProfileFragment();
            break;
        case 2:
            fragment = new EventsFragment();
            break;
        case 3:
            fragment = new TopUpFragment();
            break;
        case 4:
            fragment = new InboxFragment();
            break;
        case 5:
            fragment = new ContactUsFragment();
            break;

        case 6:
            fragment = new SettingsFragment();
            break;

        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        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");
    }
}


}

Fragment1.java

package com.ansoft.shutterbox.Fragments;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;

public class EventsFragment extends Fragment {

public static ViewPager mViewPager;

public EventsFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View cv = inflater.inflate(R.layout.fragment_events, container, false);
    InitializeView(cv);
    ImageView icDrawer = (ImageView)cv.findViewById(R.id.ic_drawer);
    icDrawer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MainActivity.mDrawerLayout.openDrawer(Gravity.LEFT);
        }
    });
    adapter = new TabPagerAdapter(getFragmentManager());
    mViewPager.setAdapter(adapter);
    tvBacktoList.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ShowNormal();
        }
    });
    mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            switch (position) {
                case 0:
                    tab1.setBackgroundResource(R.drawable.bg_event_selected);
                    tab2.setBackgroundColor(getResources().getColor(android.R.color.transparent));
                    imgtab1.setImageResource(R.drawable.ic_list_selected);
                    imgtab2.setImageResource(R.drawable.ic_calender_default);
                    tvtab1.setTextColor(getResources().getColor(R.color.colorPrimary));
                    tvtab2.setTextColor(getResources().getColor(R.color.white));
                    break;

                case 1:
                    tab2.setBackgroundResource(R.drawable.bg_event_selected);
                    tab1.setBackgroundColor(getResources().getColor(android.R.color.transparent));
                    imgtab1.setImageResource(R.drawable.ic_list_default);
                    imgtab2.setImageResource(R.drawable.ic_calender_selected);
                    tvtab1.setTextColor(getResources().getColor(R.color.white));
                    tvtab2.setTextColor(getResources().getColor(R.color.colorPrimary));
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
    return cv;
}


class TabPagerAdapter extends FragmentPagerAdapter {

    public TabPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new EventListFragment();

            case 1:
                return new EventCalenderFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 2;
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
}
}

1 个答案:

答案 0 :(得分:3)

我刚用一个简单的技巧解决了它。在我的TabPagerAdapter课程中,我使用了getChildFragmentManager()而不是getFragmentManager()。我不知道这有多大帮助,但确实如此。