Android:在片段之间切换

时间:2014-07-16 13:10:23

标签: android android-fragments

我有一个Main Activity用作菜单,用户可以从中转到不同的片段。当从一个活动切换到另一个活动时,我们会将Intent用于此目的。我们以同样的方式使用onBackPressed()方法返回上一个Activity。现在我的场景是First我将从MainActivity(Activity类)转到我从Fragment扩展的类,之后我将转到另一个片段(第二个的子片段)。现在我想从第二个片段返回到前一个片段,但是虽然我尝试了很多东西,但我无法实现。只是为了测试我做了这个

Intent in = new Intent(getActivity(), MainActivity.class) ; 
startActivity(in);
getActivity().finish();

这将我带到主要活动。但我想回到之前的片段。我尝试了OnKeyDown但是对于片段,它显示了onkeydown对于Fragments类型未定义的错误。请给我一个简单的例子,说明从一个活动到另一个活动的背压。 我已经使用此代码转到第二个片段,它让我很好地使用第二个片段,但是当我按下返回键时它会显示一个白色的空白屏幕,这是我的代码

Bundle bundle = new Bundle();
                bundle.putSerializable("KEY", dealItem);

                Fragment fragment = new DealCompleteDetailFragment();
                fragment.setArguments(bundle);

                FragmentManager fragmentManager = getActivity()
                        .getSupportFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(R.id.frame_container, fragment)
                        .addToBackStack(null).commit();
                actionBar = MainActivity.myCustomActionBar;
                actionBar.hide();

编辑:

public class DealsFragment extends Fragment {
    DatabaseService dbService;
    Services service;
    ActionBar actionBar;
    FragmentDealAdapter lazyadapter;
    RefreshableListView mPullRefreshListView;
    MyArrayList<DealItemDetail> dealDetailItem = new MyArrayList<DealItemDetail>();

    public DealsFragment() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            final Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_deals, container,
                false);

        ListView shopList = (ListView) rootView
                .findViewById(R.id.fragment_deal_listview);

        dbService = DatabaseService.getInstance(getActivity()
                .getApplicationContext());
        service = new Services(getActivity());
lazyadapter = new FragmentDealAdapter(getActivity(), dealDetailItem,
                (dealDetailItem.size() > limit) ? limit : dealDetailItem.size());
        shopList.setAdapter(lazyadapter);

        shopList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                DealItemDetail dealItem = dealDetailItem.get(position - 1);

                if (savedInstanceState == null) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("KEY", dealItem);

                    Fragment fragment = new DealCompleteDetailFragment();
                    fragment.setArguments(bundle);

                    FragmentManager fragmentManager = getActivity()
                            .getSupportFragmentManager();
                    getActivity().getSupportFragmentManager()
                            .beginTransaction()
                            .replace(R.id.frame_container, fragment)
                            .addToBackStack(null).commit();
                    actionBar = MainActivity.myCustomActionBar;
                    actionBar.hide();
                }
            }
        });

return rootView;
}

活动Oncrate:

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

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerListLeft = (ListView) findViewById(R.id.list_slidermenu_left);
        mDrawerListRight = (ListView) findViewById(R.id.list_slidermenu_right);

        navDrawerItemsLeft = new ArrayList<NavDrawerItem>();
        navDrawerItemsRight = new ArrayList<NavDrawerItem>();

        // Left Drawer Items

        navDrawerItemsLeft.add(new NavDrawerItem(Constants.SHOP_SCREEN_TITLE, false, false, false));
        navDrawerItemsLeft.add(new NavDrawerItem(Constants.DEALS_SCREEN_TITLE, false, false, false));


mDrawerListLeft.setOnItemClickListener(new SlideMenuLeftClickListener());

        // setting the nav drawer list adapter
        adapterLeftDrawer = new NavDrawerListAdapter(getApplicationContext(), navDrawerItemsLeft);
        mDrawerListLeft.setAdapter(adapterLeftDrawer);


LayoutInflater inflator=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.header, null);

        imgLeftMenu  = (ImageButton)v.findViewById(R.id.imgLeftMenu);
actionBarTitle = (TextView) v.findViewById(R.id.title_action_bar);



getSupportActionBar().setHomeButtonEnabled(true);

        getSupportActionBar().setDisplayShowTitleEnabled(false);

        getSupportActionBar().setDisplayUseLogoEnabled(false);

        getSupportActionBar().setDisplayShowCustomEnabled(true);

        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#93b636")));

        getSupportActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));

        getSupportActionBar().setCustomView(v);

        myCustomActionBar = getSupportActionBar();

        View homeIcon = findViewById(android.R.id.home);

        ((View)homeIcon.getParent()).setVisibility(View.GONE);

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        imgLeftMenu.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(mDrawerLayout.isDrawerOpen(mDrawerListLeft)){
                    mDrawerLayout.closeDrawer(mDrawerListLeft);
                } else {
                    if (mDrawerLayout.isDrawerOpen(mDrawerListRight)){
                        mDrawerLayout.closeDrawer(mDrawerListRight);
                    }
                    mDrawerLayout.openDrawer(mDrawerListLeft);
                }
            }
        });

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


private void LeftMenuClickListner(int position) {
        // update the main content by replacing fragments
        Fragment fragment = null;
        switch (position) {
        case 0:
            fragment = new ShopsNew();
            break;
        case 1:
            fragment = new DealsNew();
            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
            mDrawerListLeft.setItemChecked(position, true);
            mDrawerListLeft.setSelection(position);
            mDrawerLayout.closeDrawer(mDrawerListLeft);
        }}

0 个答案:

没有答案