我有和Android源代码,我希望幻灯片菜单从右向左打开
这是主要活动,因为我尝试添加重力,但是在此问题上没有任何帮助
主要活动
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import com.universal.R;
import com.universal.rss.ServiceStarter;
import com.universal.web.WebviewFragment;
public class MainActivity extends ActionBarActivity implements NavDrawerCallback {
private Toolbar mToolbar;
private NavDrawerFragment mNavigationDrawerFragment;
public static String DATA = "transaction_data";
SharedPreferences prefs;
String mWebUrl = null;
boolean openedByBackPress = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean newDrawer = getResources().getBoolean(R.bool.newdrawer);
if (newDrawer == true){
setContentView(R.layout.activity_main_alternate);
} else {
setContentView(R.layout.activity_main);
Helper.setStatusBarColor(MainActivity.this, getResources().getColor(R.color.myPrimaryDarkColor));
}
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mNavigationDrawerFragment = (NavDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_drawer);
if (newDrawer == true){
mNavigationDrawerFragment.setup(R.id.scrimInsetsFrameLayout, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
mNavigationDrawerFragment.getDrawerLayout().setStatusBarBackgroundColor(
getResources().getColor(R.color.myPrimaryDarkColor));
} else {
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
}
prefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
//setting push enabled
String push = getString(R.string.rss_push_url);
if (null != push && !push.equals("")){
// Create object of SharedPreferences.
boolean firstStart = prefs.getBoolean("firstStart", true);
if (firstStart){
final ServiceStarter alarm = new ServiceStarter();
SharedPreferences.Editor editor= prefs.edit();
alarm.setAlarm(this);
//now, just to be sure, where going to set a value to check if notifications is really enabled
editor.putBoolean("firstStart", false);
//commits your edits
editor.commit();
}
}
//Checking if the user would prefer to show the menu on start
boolean checkBox = prefs.getBoolean("menuOpenOnStart", false);
if (checkBox == true && null == mWebUrl){
mNavigationDrawerFragment.openDrawer();
}
// New imageloader
Helper.initializeImageLoader(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//getMenuInflater().inflate(R.menu.rss_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public void onNavigationDrawerItemSelected(int position, NavItem item){
Fragment fragment;
try {
fragment = item.getFragment().newInstance();
if (fragment != null && null == mWebUrl) {
//adding the data
Bundle bundle = new Bundle();
String extra = item.getData();
bundle.putString(DATA, extra);
fragment.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
setTitle(item.getText());
if (null != MainActivity.this.getSupportActionBar() && null != MainActivity.this.getSupportActionBar().getCustomView()){
MainActivity.this.getSupportActionBar().setDisplayOptions(
ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
}
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
@Override
public void onBackPressed() {
Fragment webview = getSupportFragmentManager().findFragmentById(R.id.container);
if (mNavigationDrawerFragment.isDrawerOpen(Gravity.RIGHT)) {
mNavigationDrawerFragment.closeDrawer(Gravity.RIGHT);
} else if (webview instanceof WebviewFragment) {
boolean goback = ((WebviewFragment)webview).canGoBack();
if (!goback)
super.onBackPressed();
} else {
super.onBackPressed();
}
}
导航抽屉适配器
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class NavDrawerAdapter extends
RecyclerView.Adapter<NavDrawerAdapter.ViewHolder> {
private List<NavItem> mData;
private NavDrawerCallback mNavDrawerCallbacks;
private NavDrawerFragment mContext;
private int mSelectedPosition;
private int mTouchedPosition;
public NavDrawerAdapter(List<NavItem> data, NavDrawerFragment context) {
mData = data;
mContext = context;
}
public NavDrawerCallback getNavigationDrawerCallbacks() {
return mNavDrawerCallbacks;
}
public void setNavigationDrawerCallbacks(NavDrawerCallback navDrawerCallback) {
mNavDrawerCallbacks = navDrawerCallback;
}
@Override
public NavDrawerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup,
int i) {
if (i == 0) {
return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.drawer_top, viewGroup, false));
} else if (i == 1) {
return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.drawer_section, viewGroup, false));
}
View v = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.drawer_row, viewGroup, false);
return new ViewHolder(v);
}
@Override
public int getItemViewType(int position) {
if (mData.get(position).getType() == NavItem.TOP){
return 0;
} else if (mData.get(position).getType() == NavItem.SECTION) {
return 1;
} else {
return 2;
}
}
@Override
public void onBindViewHolder(NavDrawerAdapter.ViewHolder viewHolder,
final int i) {
Drawable drawable = null;
if (mData.get(i).getDrawable() != 0) {
drawable = mContext.getResources().getDrawable(
mData.get(i).getDrawable());
}
if (mData.get(i).getType() == NavItem.ITEM) {
setText(viewHolder.textViewItem, i, drawable);
setBackgroundColor(i, viewHolder);
} else if (mData.get(i).getType() == NavItem.EXTRA) {
viewHolder.textViewExtra.setVisibility(View.VISIBLE);
viewHolder.textViewItem.setVisibility(View.GONE);
setText(viewHolder.textViewExtra, i, drawable);
setBackgroundColor(i, viewHolder);
viewHolder.setIsRecyclable(false);
} else if (mData.get(i).getType() == NavItem.SECTION) {
setText(viewHolder.textViewSection, i, drawable);
viewHolder.setIsRecyclable(false);
}
}
private void setBackgroundColor(int i,
NavDrawerAdapter.ViewHolder viewHolder) {
if (mSelectedPosition == i || mTouchedPosition == i) {
viewHolder.itemView.setBackgroundColor(viewHolder.itemView
.getContext().getResources()
.getColor(R.color.selected_gray));
} else {
viewHolder.itemView.setBackgroundColor(Color.TRANSPARENT);
}
}
private void setText(TextView textView, final int i, Drawable drawable) {
textView.setText(mData.get(i).getText());
textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null,
null);
textView.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchPosition(i);
return false;
case MotionEvent.ACTION_CANCEL:
touchPosition(-1);
return false;
case MotionEvent.ACTION_MOVE:
return false;
case MotionEvent.ACTION_UP:
touchPosition(-1);
return false;
}
return true;
}
});
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mNavDrawerCallbacks != null)
mNavDrawerCallbacks.onNavigationDrawerItemSelected(i,
mData.get(i));
}
});
}
private void touchPosition(int position) {
int lastPosition = mTouchedPosition;
mTouchedPosition = position;
if (lastPosition >= 0)
notifyItemChanged(lastPosition);
if (position >= 0)
notifyItemChanged(position);
}
public void selectPosition(int position) {
int lastPosition = mSelectedPosition;
mSelectedPosition = position;
notifyItemChanged(lastPosition);
notifyItemChanged(position);
}
@Override
public int getItemCount() {
return mData != null ? mData.size() : 0;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView textViewItem;
public TextView textViewSection;
public TextView textViewExtra;
public ViewHolder(View itemView) {
super(itemView);
textViewItem = (TextView) itemView.findViewById(R.id.item_name);
textViewExtra = (TextView) itemView.findViewById(R.id.extra_name);
textViewSection = (TextView) itemView
.findViewById(R.id.section_name);
}
}
}
导航抽屉片段
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
public class NavDrawerFragment extends Fragment implements NavDrawerCallback {
private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";
private NavDrawerCallback mCallbacks;
private RecyclerView mDrawerList;
private View mFragmentContainerView;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mActionBarDrawerToggle;
private int mCurrentSelectedPosition;
private List<NavItem> mConfiguration;
public void closeDrawer(int gravity) {
mDrawerLayout.closeDrawer(gravity);
}
public void openDrawer(int gravity) {
mDrawerLayout.openDrawer(gravity);
}
public boolean isDrawerOpen(int gravity) {
return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(gravity);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view;
view = inflater.inflate(R.layout.drawer_fragment, container, false);
mDrawerList = (RecyclerView) view.findViewById(R.id.drawerList);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mDrawerList.setLayoutManager(layoutManager);
mDrawerList.setHasFixedSize(true);
final List<NavItem> NavItems = getConfiguration();
NavDrawerAdapter adapter = new NavDrawerAdapter(NavItems, NavDrawerFragment.this);
adapter.setNavigationDrawerCallbacks(this);
mDrawerList.setAdapter(adapter);
selectItem(mCurrentSelectedPosition, NavItems.get(mCurrentSelectedPosition));
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallbacks = (NavDrawerCallback) activity;
} catch (ClassCastException e) {
throw new ClassCastException("Activity must implement NavDrawerCallback.");
}
}
public ActionBarDrawerToggle getActionBarDrawerToggle() {
return mActionBarDrawerToggle;
}
public void setActionBarDrawerToggle(ActionBarDrawerToggle actionBarDrawerToggle) {
mActionBarDrawerToggle = actionBarDrawerToggle;
}
public void setup(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar) {
mFragmentContainerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
mActionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (!isAdded()) return;
getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!isAdded()) return;
getActivity().invalidateOptionsMenu();
}
};
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mActionBarDrawerToggle.syncState();
}
});
mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
//TODO This works (hides the drawer if there is only 1 item), but makes settings and favorites unreachable
boolean newDrawer = getResources().getBoolean(R.bool.newdrawer);
if (newDrawer == false && getConfiguration().size() == 1){
mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
if (newDrawer == true && getConfiguration().size() == 2){
mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
}
public void openDrawer() {
mDrawerLayout.openDrawer(mFragmentContainerView);
}
public void closeDrawer() {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}
@Override
public void onDetach() {
super.onDetach();
mCallbacks = null;
}
void selectItem(int position, NavItem item) {
//If on start, item is section, change it.
if (item.getType() == NavItem.SECTION || item.getType() == NavItem.TOP){
position = position + 1;
item = getConfiguration().get(position);
selectItem(position, item);
return;
}
mCurrentSelectedPosition = position;
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}
if (mCallbacks != null) {
mCallbacks.onNavigationDrawerItemSelected(position, item);
}
((NavDrawerAdapter) mDrawerList.getAdapter()).selectPosition(position);
}
public boolean isDrawerOpen() {
return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mActionBarDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
}
@Override
public void onNavigationDrawerItemSelected(int position, NavItem item) {
//TODO we can also call this method here, but that won't set an initial item.
//mCallbacks.onNavigationDrawerItemSelected(position, item);
if (item.getType() != NavItem.SECTION){
selectItem(position, item);
}
}
public DrawerLayout getDrawerLayout() {
return mDrawerLayout;
}
public void setDrawerLayout(DrawerLayout drawerLayout) {
mDrawerLayout = drawerLayout;
}
private List<NavItem> getConfiguration(){
if (null == mConfiguration){
mConfiguration = Config.configuration();
boolean newDrawer = getResources().getBoolean(R.bool.newdrawer);
if (newDrawer == true){
mConfiguration.add(0, new NavItem("Header", NavItem.TOP));
}
}
return mConfiguration;
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_gravity="right"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar_actionbar"
layout="@layout/toolbar_default"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:layout_below="@+id/toolbar_actionbar">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- android:layout_marginTop="?android:attr/actionBarSize"-->
<fragment
android:id="@+id/fragment_drawer"
android:name="com.universal.NavDrawerFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
app:layout="@layout/drawer_fragment"/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
最佳答案
看看这对我有用
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="@+id/app_toolbar"
layout="@layout/layout_toolbar"/>
</LinearLayout>
<FrameLayout
android:id="@+id/frame_main_to_replcae_menu"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
<fragment
android:id="@+id/fragment_navigation_drawer"
android:name="com.views.fragments.NavigationDrawerFragment"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer"/>
<fragment
android:id="@+id/fragment_profile"
android:name="com.views.fragments.ProfileFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
app:layout="@layout/fragment_profile"
tools:layout="@layout/fragment_profile"/>
</android.support.v4.widget.DrawerLayout>
关于java - Android Slide菜单从右到左,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31109747/