按下导航抽屉下方的按钮不起作用

时间:2015-03-12 04:14:49

标签: android android-intent onclicklistener navigation-drawer

然而,当按钮放置在层次结构中以便可以在导航抽屉的顶部看到时,按钮可以正常工作。但是,当滑动按钮时,按钮应隐藏在导航抽屉后面,因此这是不可取的。

以下是 MainActivity.java

中的代码
public class MainActivity extends ActionBarActivity implements NavigationDrawerCallbacks {

public ProgressDialog progBar;

public final static boolean DEBUG = false;
public final static String TAG = "AppGetter";
private Toolbar mToolbar;
private NavigationDrawerFragment mNavigationDrawerFragment;



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

    mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false); 

    mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
    mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);

    ImageButton cart_button = (ImageButton) findViewById(R.id.button2);
    cart_button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            start_request();

        }
    });



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public void onNavigationDrawerItemSelected(int position) {
    Toast.makeText(this, "Menu item selected -> " + position, Toast.LENGTH_SHORT).show();
}

@Override
public void onBackPressed() {
    if (mNavigationDrawerFragment.isDrawerOpen())
        mNavigationDrawerFragment.closeDrawer();
    else
        super.onBackPressed();
}


public void start_request()
{
    String pkg = getPackageName();
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setComponent(new ComponentName(pkg,pkg+".RequestActivity"));
    startActivity(intent);

    if(DEBUG)Log.v(TAG,"Intent intent: "+intent);
}
}

我假设问题出在上面的类中,但为了完成,我粘贴了下面感兴趣的两个XML文件。

activity_main.xml 如您所见,ImageButton当前位于层次结构中导航抽屉的“上方”,以使其被导航抽屉覆盖。将ImageButton移动到“下方”会使按钮正常工作,但会使其显示在导航抽屉的顶部(而不是像布局的其余部分那样着色)。

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:fontify="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:background="@color/myPrimaryColor">

    <View
        android:id="@+id/block1"
        android:layout_height="240dp"
        android:layout_width="match_parent"
        android:layout_below="@+id/toolbar_actionbar"
        android:background="@drawable/block_primary"
        />

    <TextView
        android:id="@+id/title1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_below="@+id/toolbar_actionbar"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="56dp"
        android:text="@string/title"
        android:textColor="@color/white"
        android:textSize="34sp"
        android:fontFamily="sans-serif"
        />

    <include
        android:id="@+id/toolbar_actionbar"
        layout="@layout/toolbar_default"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        />

    <ImageButton
        android:id="@+id/button2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="268dp"
        android:src="@drawable/ic_chevron_up"
        android:background="@drawable/fab_simple"/>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_below="@+id/toolbar_actionbar"
        >

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <fragment
            android:id="@+id/fragment_drawer"
            android:name="com.onepersonco.iconrequestbase.NavigationDrawerFragment"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            layout="@layout/fragment_navigation_drawer"
            tools:layout="@layout/fragment_navigation_drawer" />
    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>

fragment_navigation_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<!-- Provides a margin at the top of the navigation drawer. -->
<View
    android:id="@+id/navWhiteSpace1"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:background="@color/myNavigationDrawerBackgroundColor"
    />


<android.support.v7.widget.RecyclerView
android:id="@+id/drawerList"
android:layout_below="@+id/navWhiteSpace1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:scrollbars="vertical"
android:scrollbarDefaultDelayBeforeFade="0"
android:scrollbarFadeDuration="0"
android:overScrollMode="never"
android:focusable="true"
android:background="@color/myNavigationDrawerBackgroundColor"/>

</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

我尝试在线关注各种导航抽屉教程时遇到了同样的问题。

对我有用的是在GitHub上使用Mike Penz的MaterialDrawer库。他在页面底部的“自述文件”中有一个非常简单的教程。

希望对Java有更好了解的其他人可以解释为什么代码失败。