在操作栏中将抽屉从左向右切换

时间:2016-01-30 16:36:25

标签: android android-actionbar navigation-drawer

在我的活动中,我有一个从右侧弹出的抽屉列表,但我无法从左侧向右侧移动抽屉式拨动开关。如果有人可以帮我解决这个问题。我只想将切换移动到操作栏的右侧。

package com.parse.starter;



import com.parse.ParseUser;

public class UserDrawer extends AppCompatActivity {

//Declaring Variables
private ListView DrawerList;
private ArrayAdapter<String> Adapter;
private ActionBarDrawerToggle DrawerToggle;
private DrawerLayout DrawerLayout;
private String ActivityTitle;



final ParseUser currentUser = ParseUser.getCurrentUser();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_drawer);

    DrawerList = (ListView) findViewById(R.id.navList);
    DrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActivityTitle = getTitle().toString();
    addDrawerItems();
    setupDrawer();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    Button Sample = (Button) findViewById(R.id.button);

}

//Method To Add Items To The List View
private void addDrawerItems() {
    String[] DArray = {"Job List", "Notifications", "Messages", "Log Out"};
    Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, DArray);
    DrawerList.setAdapter(Adapter);
    DrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (position == 0) {
                Intent i0 = new Intent(UserDrawer.this, Set_Info.class);
                startActivity(i0);
            } else if (position == 1) {
                //Intent i1 = new Intent(Drawer1.this, AddPatient.class);
                //startActivity(i1);
            } else if (position == 2) {
                //Intent i2 = new Intent(Drawer1.this, Notifications.class);
                //startActivity(i2);
            } else if (position == 3) {
                //Intent i3 = new Intent(Drawer1.this, Message_Log.class);
                //startActivity(i3);
            } else if (position == 4) {
                Intent i4 = new Intent(UserDrawer.this, MainActivity.class);
                startActivity(i4);
                Toast.makeText(getApplicationContext(), "You are Logged Out", Toast.LENGTH_LONG).show();
                finish();
            }

        }
    });
}

private void setupDrawer() {
    DrawerToggle = new ActionBarDrawerToggle(this, DrawerLayout,
            R.string.drawer_open, R.string.drawer_close) {

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            getSupportActionBar().setTitle("Menu");
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            getSupportActionBar().setTitle(ActivityTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };
    DrawerToggle.setDrawerIndicatorEnabled(true);
    DrawerLayout.setDrawerListener(DrawerToggle);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if(item != null && item.getItemId()==android.R.id.home){
        if(DrawerLayout.isDrawerOpen(Gravity.RIGHT)){
            //Notice the Gravity.Right
            DrawerLayout.closeDrawer(Gravity.RIGHT);
        }else{
            DrawerLayout.openDrawer(Gravity.RIGHT);
        }
    }

   return false;
    }
@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    DrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    DrawerToggle.onConfigurationChanged(newConfig);
}
@SuppressWarnings("ResourceType")
public void SampleClick(View view) {
    try {
        Intent i = new Intent(UserDrawer.this,Set_Info.class);
        startActivity(i);
    } catch (Exception e) {

    }
}


}

0 个答案:

没有答案