赞
踩
方法来自Stack OverFlow,利用反射,修改DrawerLayout的侧滑菜单的感应范围,代码如下:
- public class MainActivity extends Activity {
- private DrawerLayout mDrawerLayout;
- private ListView mDrawerList;
- private ActionBarDrawerToggle mDrawerToggle;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
- mDrawerList = (ListView) findViewById(R.id.left_drawer);
-
- // set a custom shadow that overlays the main content when the drawer opens
- mDrawerLayout.setDrawerShadow(R.drawable.your_drawer_shadow, GravityCompat.START);
- // set up the drawer's list view with items and click listener
- mDrawerList.setAdapter(new ArrayAdapter<String>(this,
- R.layout.your_drawer_list, yourItems));
- mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
-
- // enable ActionBar app icon to behave as action to toggle nav drawer
- getActionBar().setDisplayHomeAsUpEnabled(true);
- getActionBar().setHomeButtonEnabled(true);
- //关键代码
- Field mDragger = null;
- try {
- mDragger = mDrawerLayout.getClass().getDeclaredField(
- "mLeftDragger"); //mRightDragger for right obviously
- } catch (NoSuchFieldException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- mDragger.setAccessible(true);
- ViewDragHelper draggerObj = null;
- try {
- draggerObj = (ViewDragHelper) mDragger
- .get(mDrawerLayout);
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- Field mEdgeSize = null;
- try {
- mEdgeSize = draggerObj.getClass().getDeclaredField(
- "mEdgeSize");
- } catch (NoSuchFieldException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- mEdgeSize.setAccessible(true);
- int edge = 0;
- try {
- edge = mEdgeSize.getInt(draggerObj);
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- try {
- mEdgeSize.setInt(draggerObj, edge * 5); //optimal value as for me, you may set any constant in dp
- //You can set it even to the value you want like mEdgeSize.setInt(draggerObj, 150); for 150dp
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- // ActionBarDrawerToggle ties together the the proper interactions
- // between the sliding drawer and the action bar app icon
- mDrawerToggle = new ActionBarDrawerToggle(
- this, /* host Activity */
- mDrawerLayout, /* DrawerLayout object */
- R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
- R.string.drawer_open, /* "open drawer" description for accessibility */
- R.string.drawer_close /* "close drawer" description for accessibility */
- ) {
- public void onDrawerClosed(View view) {
-
- }
-
- public void onDrawerOpened(View drawerView) {
-
- }
- };
- mDrawerLayout.setDrawerListener(mDrawerToggle);
-
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。