导航抽屉无法正常工作

时间:2018-07-04 12:46:31

标签: android navigation-drawer

当我打开此活动时,默认情况下已打开抽屉。当我尝试关闭抽屉时,它没有关闭。总之,抽屉有故障。我已经附上了XML代码和JAVA代码。

这是XML文件代码:

<?xml version="1.0" encoding="utf-8"?>

<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"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg_home">

    <include
        android:id="@+id/action_bar"
        layout="@layout/actionbar"
        />

    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:id="@+id/nav_view"
        app:headerLayout="@layout/navigation"
        app:menu="@menu/drawer_menu"/>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="210dp"
        android:src="@drawable/add_to_cart"
        android:elevation="6dp"
        android:id="@+id/fab_place_order"
        app:pressedTranslationZ="12dp"
        app:backgroundTint="@color/fabOrder"
        android:visibility="invisible"
        />

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="150dp"
        android:src="@drawable/ic_account_balance_wallet_black_24dp"
        android:elevation="6dp"
        android:id="@+id/fab_balance"
        app:pressedTranslationZ="12dp"
        app:backgroundTint="@color/fabBalance"
        android:visibility="invisible"
        />

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="90dp"
        android:src="@drawable/ic_exit_to_app_black_24dp"
        android:elevation="6dp"
        android:id="@+id/fab_logout"
        app:pressedTranslationZ="12dp"
        app:backgroundTint="@color/fabExit"
        android:visibility="invisible"
        />


    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="20dp"
        android:src="@drawable/menu"
        android:elevation="6dp"
        android:id="@+id/fab_menu"
        app:pressedTranslationZ="12dp"
        android:backgroundTint="@color/fabMain"
        />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />

        <TextView
            android:id="@+id/label1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="230dp"
            android:layout_marginEnd="80dp"
            android:layout_marginRight="70dp"
            android:text="Place Order"
            android:textColor="#003200"
            android:textStyle="bold"
            android:visibility="invisible" />

        <TextView
            android:id="@+id/label2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@+id/label1"
            android:layout_alignLeft="@+id/label1"
            android:layout_alignParentBottom="true"
            android:layout_alignRight="@+id/label1"
            android:layout_marginBottom="170dp"
            android:text="Check Balance"
            android:textColor="#000032"
            android:textStyle="bold"
            android:visibility="invisible" />

        <TextView
            android:id="@+id/label3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@+id/label1"
            android:layout_alignParentBottom="true"
            android:layout_alignRight="@+id/label1"
            android:layout_marginBottom="110dp"
            android:text="Logout"
            android:textColor="#320000"
            android:textStyle="bold"
            android:visibility="invisible" />

    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

</android.support.v4.widget.DrawerLayout>

这是JAVA文件代码:

public class HomeActivity extends AppCompatActivity {

    ConnectionDetector connectionDetector = new ConnectionDetector(this);
    DatabaseHelper helper = new DatabaseHelper(this);
    Handler handler = new Handler();
    FloatingActionButton fab_menu,fab_balance,fab_logout,fab_order;
    Animation fabOpen,fabClose,fabClockwise,fabAnticlockwise;
    boolean isOpen = false, status = false;
    ProgressBar progressBar;

    private  boolean isUserClickedBackButton = false;
    private DrawerLayout drawerLayout;

    TextView t1,t2,t3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        helper.startFirebaseDB();
        Toolbar toolbar = findViewById(R.id.action_bar);
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        setSupportActionBar(toolbar);
        drawerLayout = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
        toggle.syncState();
        progressBar = findViewById(R.id.progressBar);
        fab_menu = findViewById(R.id.fab_menu);
        fab_menu.setEnabled(false);
        fab_order = findViewById(R.id.fab_place_order);
        fab_balance = findViewById(R.id.fab_balance);
        fab_logout = findViewById(R.id.fab_logout);
        t1 = findViewById(R.id.label1);
        t2 = findViewById(R.id.label2);
        t3 = findViewById(R.id.label3);
        fabOpen= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fab_open);
        fabClose= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fab_close);
        fabClockwise= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotate_clockwise);
        fabAnticlockwise= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotate_anticlockwise);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                progressBar.setVisibility(View.GONE);
                fab_menu.setEnabled(true);
            }
        },2000);
        fab_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isOpen)
                {
                    fab_order.startAnimation(fabClose);
                    fab_balance.startAnimation(fabClose);
                    fab_logout.startAnimation(fabClose);
                    fab_menu.startAnimation(fabAnticlockwise);
                    fab_balance.setClickable(false);
                    fab_logout.setClickable(false);
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            t1.setVisibility(View.INVISIBLE);
                            t2.setVisibility(View.INVISIBLE);
                            t3.setVisibility(View.INVISIBLE);
                        }
                    },200);
                    isOpen = false;
                }
                else
                {
                    fab_order.startAnimation(fabOpen);
                    fab_balance.startAnimation(fabOpen);
                    fab_logout.startAnimation(fabOpen);
                    fab_menu.startAnimation(fabClockwise);
                    fab_balance.setClickable(true);
                    fab_logout.setClickable(true);
                    isOpen = true;
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            t1.setVisibility(View.VISIBLE);
                            t2.setVisibility(View.VISIBLE);
                            t3.setVisibility(View.VISIBLE);
                        }
                    },200);
                }
            }
        });

        fab_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                status = helper.checkUserStatus(helper.getUser());
                if(!connectionDetector.isConnected()) {
                    Toast.makeText(HomeActivity.this, "Check your internet connection !!!", Toast.LENGTH_LONG).show();
                    return;
                }
                else if(!status) {
                    Toast t1 = Toast.makeText(HomeActivity.this, "Access Denied !!!", Toast.LENGTH_LONG);
                    Toast t2 = Toast.makeText(HomeActivity.this, "Please contact: Ayaz Handloom Store", Toast.LENGTH_LONG);
                    t1.setGravity(Gravity.CENTER,0,0);
                    t1.show();
                    t2.setGravity(Gravity.CENTER,0,0);
                    t2.show();
                    return;
                }
                else {
                    Intent newOrder = new Intent(HomeActivity.this, NewOrderActivity.class);
                    startActivity(newOrder);
                    finish();
                }
            }
        });

        fab_balance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent balance = new Intent(HomeActivity.this,BalanceActivity.class);
                startActivity(balance);
                finish();
            }
        });

        fab_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent logout = new Intent(HomeActivity.this,MainActivity.class);
                helper.logout();
                Toast.makeText(HomeActivity.this, "Logout success !!!", Toast.LENGTH_LONG).show();
                startActivity(logout);
                finish();
            }
        });
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId())
        {
            case R.id.edit_profile:

                break;
            case R.id.change_password:

                break;
            case R.id.settings:

                break;
            case R.id.logout:
                DialogInterface.OnClickListener dialogBox = new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which){
                            case DialogInterface.BUTTON_POSITIVE:
                                Intent logout = new Intent(HomeActivity.this,MainActivity.class);
                                helper.logout();
                                Toast.makeText(HomeActivity.this, "Logout success !!!", Toast.LENGTH_LONG).show();
                                startActivity(logout);
                                finish();
                                break;
                            case DialogInterface.BUTTON_NEGATIVE:
                                Toast cancel = Toast.makeText(HomeActivity.this, "Request cancelled !!!", Toast.LENGTH_SHORT);
                                cancel.setGravity(Gravity.CENTER,0,0);
                                cancel.show();
                                break;
                        }
                    }
                };
                AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
                builder.setMessage("Are you sure to logout?").setPositiveButton("Yes", dialogBox).setNegativeButton("No", dialogBox).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START))
            drawerLayout.closeDrawer(GravityCompat.START);
        else {
            if (!isUserClickedBackButton) {
                Toast.makeText(this, "Press back key again for Main Page !!!", Toast.LENGTH_LONG).show();
                isUserClickedBackButton = true;
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        isUserClickedBackButton = false;
                    }
                }, 3000);
            } else {
                Intent back = new Intent(HomeActivity.this, MainActivity.class);
                startActivity(back);
                finish();
            }
        }
    }
}

如果我删除了DrawerLayout内部的XML代码,则可以正常工作。

1 个答案:

答案 0 :(得分:0)

您忘记在drawerLayout.addDrawerListener(toggle);之前添加toggle.syncState();