【发布时间】:2015-09-02 16:16:29
【问题描述】:
我正在我的应用中实现导航抽屉,我想使用 Material Design。
我使用以下参考来创建导航抽屉link。
对我来说效果很好。导航抽屉在从左到右滑动时工作正常。
现在我必须通过从右向左滑动来实现我的导航抽屉。我做了以下更改。听到是我的代码
文件:MainActivity.java
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
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.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends ActionBarActivity {
String TITLES[] = {"Home","Events","Mail","Shop","Travel"};
int ICONS[] = {R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher};
//Similarly we Create a String Resource for the name and email in the header view
//And we also create a int resource for profile picture in the header view
String NAME = "Akash Bangad";
String EMAIL = "akash.bangad@android4devs.com";
int PROFILE = R.mipmap.ic_profile;
private Toolbar toolbar; // Declaring the Toolbar Object
RecyclerView mRecyclerView; // Declaring RecyclerView
RecyclerView.Adapter mAdapter; // Declaring Adapter For Recycler View
RecyclerView.LayoutManager mLayoutManager; // Declaring Layout Manager as a linear layout manager
DrawerLayout Drawer; // Declaring DrawerLayout
private ActionBarDrawerToggle mDrawerToggle; // Declaring Action Bar Drawer Toggle
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Assinging the toolbar object ot the view
and setting the the Action bar to our toolbar
*/
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
//getSupportActionBar().setHomeButtonEnabled(true);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//toolbar.setNavigationIcon(getResources().getDrawable(R.mipmap.ic_launcher));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("cek", "home selected");
if (Drawer.isDrawerOpen(Gravity.RIGHT)) {
Drawer.closeDrawer(Gravity.RIGHT);
} else {
Drawer.openDrawer(Gravity.RIGHT);
}
}
});
mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView); // Assigning the RecyclerView Object to the xml View
mRecyclerView.setHasFixedSize(true); // Letting the system know that the list objects are of fixed size
mAdapter = new MyAdapter(TITLES,ICONS,NAME,EMAIL,PROFILE); // Creating the Adapter of MyAdapter class(which we are going to see in a bit)
// And passing the titles,icons,header view name, header view email,
// and header view profile picture
mRecyclerView.setAdapter(mAdapter); // Setting the adapter to RecyclerView
mLayoutManager = new LinearLayoutManager(this); // Creating a layout Manager
mRecyclerView.setLayoutManager(mLayoutManager); // Setting the layout Manager
Drawer = (DrawerLayout) findViewById(R.id.DrawerLayout); // Drawer object Assigned to the view
mDrawerToggle = new ActionBarDrawerToggle(this,Drawer,toolbar,R.string.openDrawer,R.string.closeDrawer){
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
// code here will execute once the drawer is opened( As I dont want anything happened whe drawer is
// open I am not going to put anything here)
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
// Code here will execute once drawer is closed
}
}; // Drawer Toggle Object Made
/*Drawer.setDrawerListener(mDrawerToggle); // Drawer Listener set to the Drawer toggle*/
/* mDrawerToggle = new ActionBarDrawerToggle(this, Drawer,
R.mipmap.ic_launcher, //navigation menu toggle icon
R.string.app_name,
R.string.app_name
) {
public void onDrawerClosed(View view) {
Log.e("onDrawerClosed", "onDrawerClosed");
}
public void onDrawerOpened(View drawerView) {
Log.e("onDrawerOpened","onDrawerOpened");
}
};*/
/*mDrawerToggle = new ActionBarDrawerToggle(this, Drawer, toolbar,R.string.openDrawer,R.string.closeDrawer) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == android.R.id.home) {
if (Drawer.isDrawerOpen(Gravity.RIGHT)) {
Drawer.closeDrawer(Gravity.RIGHT);
} else {
Drawer.openDrawer(Gravity.RIGHT);
}
}
return true;
}
public void onDrawerClosed(View view) {
Log.e("onDrawerClosed", "onDrawerClosed");
}
public void onDrawerOpened(View drawerView) {
Log.e("onDrawerOpened","onDrawerOpened");
}
};*/
Drawer.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState(); // Finally we set the drawer toggle sync State
/* setActionBarUpIndicator((Drawable) mSlider,
Drawer.isDrawerOpen(Gravity.RIGHT) ?
mCloseDrawerContentDescRes : mOpenDrawerContentDescRes);*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (item != null && item.getItemId() == android.R.id.home) {
//open close drawer
if (Drawer.isDrawerOpen(Gravity.RIGHT)) {
Drawer.closeDrawer(Gravity.RIGHT);
} else {
Drawer.openDrawer(Gravity.RIGHT);
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
文件:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="7dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/tool_bar"
layout="@layout/toolbar">
</include>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/RecyclerView"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#ffffff"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.DrawerLayout>
这里我为 ActionBarDrawerToggle 使用了 android.support.v7.app.ActionBarDrawerToggle 导入。但是如果我使用 android.support.v4.app.ActionBarDrawerToggle 我可以使用它,那么问题是我没有得到材料设计中默认的后退按钮效果。
如果我使用 V4 库,那么我不能直接使用工具栏,还必须启用并显示主页按钮。
如果我使用 V7 库,基本上我无法制作 Gravity.RIGHT。如何覆盖该方法。
文件:错误日志
06-17 17:06:22.772 3296-3296/com.demo.nasir.materialdesign E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.demo.nasir.materialdesign, PID: 3296
java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1322)
at android.support.v7.app.ActionBarDrawerToggle.toggle(ActionBarDrawerToggle.java:289)
at android.support.v7.app.ActionBarDrawerToggle.access$100(ActionBarDrawerToggle.java:65)
at android.support.v7.app.ActionBarDrawerToggle$1.onClick(ActionBarDrawerToggle.java:201)
at android.view.View.performClick(View.java:4463)
at android.view.View$PerformClick.run(View.java:18789)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
-
您是否尝试过仅在 xml 中设置重力?这是我需要的唯一改变,让抽屉向另一方向滑动。
-
android:layout_gravity="right"
-
@Capricorn 也试过这个。仍然出现与上述相同的错误。
-
@Johnson 也试过这个。仍然出现与上述相同的错误。
-
我遇到了同样的问题。调试这个,我查看了 ActionBarDrawerToggle 的代码,它似乎只支持从左到右的抽屉!
标签: android material-design navigation-drawer android-appcompat