如何在android中选择导航抽屉活动时打开新屏幕

时间:2015-07-03 09:33:27

标签: java android

我是非常新的Android ..我创建了简单的项目,我使用导航抽屉。如果我从该导航中选择任何一个选项意味着它在Mainactivity屏幕中覆盖。因为我在Mainactivity.java中写了所有内容。我想如果我选择任何人选项意味着它在新屏幕上显示如果我点击后退显示前一个屏幕..怎么办..请任何人帮助我!

感谢您提前!

我的代码在这里: MainActivity.java



import android.app.Activity;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.location.Address;
import android.location.Geocoder;
import android.support.v4.app.DialogFragment;
import android.os.Bundle;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.support.annotation.Nullable;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends FragmentActivity {

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

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    CustomDrawerAdapter adapter;

    List<DrawerItem> dataList;
    EditText mEdit;

    private String[] states;
    private Spinner spinner;

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

       /* String[] city= getResources().getStringArray(R.array.city);
        autoTextView = (AutoCompleteTextView) findViewById(R.id.city_autoCompleteTextView);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,city);

        autoTextView.setThreshold(1);
        autoTextView.setAdapter(arrayAdapter);*/

        // Initializing
        dataList = new ArrayList<DrawerItem>();
        mTitle = mDrawerTitle = getTitle();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);

        // Add Drawer Item to dataList
        dataList.add(new DrawerItem(true)); // adding a spinner to the list - 0

        dataList.add(new DrawerItem("Wallet")); // adding a header to the list - 1
        dataList.add(new DrawerItem("Balance", R.drawable.ic_balance)); // - 2


        dataList.add(new DrawerItem("Profile"));// adding a header to the list - 3
        dataList.add(new DrawerItem("Personal", R.drawable.ic_account));
        dataList.add(new DrawerItem("Work", R.drawable.ic_work));
        dataList.add(new DrawerItem("Address", R.drawable.ic_home));
        dataList.add(new DrawerItem("Vehicle", R.drawable.ic_car));
        dataList.add(new DrawerItem("Preference", R.drawable.ic_pref));


        dataList.add(new DrawerItem("Other Option")); // adding a header to the list
        dataList.add(new DrawerItem("About", R.drawable.ic_action_about));
        dataList.add(new DrawerItem("Settings", R.drawable.ic_action_settings));
        dataList.add(new DrawerItem("Help", R.drawable.ic_action_help));

        adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item,
                dataList);

        mDrawerList.setAdapter(adapter);

        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

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

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, R.string.drawer_open,
                R.string.drawer_close) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to
                // onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to
                // onPrepareOptionsMenu()
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }






//Calendar picker
    public void selectDate(View view) {
        DialogFragment newFragment = new SelectDateFragment();
        newFragment.show(getSupportFragmentManager(), "DatePicker");
    }
    public void populateSetDate(int year, int month, int day) {
        mEdit = (EditText)findViewById(R.id.dobText);
        mEdit.setText(month + "/" + day + "/" + year);
    }
    public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener
    {


        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final Calendar calendar = Calendar.getInstance();
            int yy = calendar.get(Calendar.YEAR);
            int mm = calendar.get(Calendar.MONTH);
            int dd = calendar.get(Calendar.DAY_OF_MONTH);
            return new DatePickerDialog(getActivity(), this, yy, mm, dd);
        }

        public void onDateSet(DatePicker view, int yy, int mm, int dd) {
            populateSetDate(yy, mm + 1, dd);
        }

    }



   //Spinner

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void SelectItem(String item, int possition) {
        Fragment fragment = null;
        Bundle args = new Bundle();

        switch(item) {
            case "Balance": fragment = new BalanceFragment(); break;
            case "Personal": fragment = new PersonalFragment(); break;
            case "Work": fragment = new WorkFragment(); break;
            case "Address": fragment = new AddressFragment(); break;
            case "Vehicle": fragment = new VehicleFragment(); break;
            case "Preference": fragment = new PreferenceFragment(); break;
            case "About": fragment = new AboutFragment(); break;
            case "Settings": fragment = new SettingsFragment(); break;
            case "Help": fragment = new HelpFragment(); break;
            default: fragment = new DefaultFragment(); break;
        }

        fragment.setArguments(args);
        FragmentManager frgManager = getFragmentManager();
        frgManager.beginTransaction().replace(R.id.content_frame, fragment)
                .commit();

        mDrawerList.setItemChecked(possition, true);
        setTitle(dataList.get(possition).getItemName());
        mDrawerLayout.closeDrawer(mDrawerList);
    }




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

    @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 toggles
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // The action bar home/up action should open or close the drawer.
        // ActionBarDrawerToggle will take care of this.
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        return false;
    }

    private class DrawerItemClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            if (dataList.get(position).getTitle() == null) {
                SelectItem(dataList.get(position).getItemName(), position);
            }
        }
    }


} 
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

查看selectItem函数。

只需替换第frgManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

即可

final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.content_frame, fragment); transaction.addToBackStack("backAgain"); transaction.commit();