0

Estoy implementando el navigation drawer en una aplicacion kotlin. Aparece en la toolbar pero no responde cuando le doy click.

Los xmls correspondientes al navigation estan identicos a cuando el ide los implementa.

No tira error ni nada como si no tuviera el listener activado cuando le doy click.

Soy muy nuevo en kotlin y en android. Si necesitan que copie mas fuentes me avisan no hay drama desde ya muchas gracias.

Me hice un proyecto nuevo y implemente el navigation drawer tal cual y funciona me debe faltar algo tonto

Copio el codigo kotlin:

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

companion object {
    val MODALIDAD = "MODALIDAD"
    val AGREGAR = 1
}
private lateinit var toolbar: Toolbar
private lateinit var username: EditText
private lateinit var password: EditText
private lateinit var logIn: RelativeLayout
private lateinit var createUser: TextView
private lateinit var nombre: String
private var saludar: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    setContentView(R.layout.activity_main)

    val toolbar: Toolbar = findViewById(R.id.toolbarmav)
    setSupportActionBar(toolbar)


    val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
    val navView: NavigationView = findViewById(R.id.nav_view)
    val toggle = ActionBarDrawerToggle(
        this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close
    )
    drawerLayout.addDrawerListener(toggle)
    toggle.syncState()

    navView.setNavigationItemSelectedListener(this)



    setupUI()
    initilizeLogIn()
    initializeToolbar()
    initilizeCreateUser()
    asignarValoresSettings()

    if (saludar) {
        Toast.makeText(this, "Hola $nombre", Toast.LENGTH_LONG).show()
    }

}

override fun onBackPressed() {
    val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
    if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
        drawerLayout.closeDrawer(GravityCompat.START)
    } else {
        super.onBackPressed()
    }
}



override fun onNavigationItemSelected(item: MenuItem): Boolean {
    // Handle navigation view item clicks here.
    when (item.itemId) {
        R.id.nav_home -> {
            // Handle the camera action
        }
        R.id.nav_gallery -> {

        }
        R.id.nav_slideshow -> {

        }
        R.id.nav_tools -> {

        }
        R.id.nav_share -> {

        }
        R.id.nav_send -> {

        }
    }
    val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
    drawerLayout.closeDrawer(GravityCompat.START)
    return true
}

private fun asignarValoresSettings() {
    val pref = PreferenceManager.getDefaultSharedPreferences(this)

    nombre = pref.getString("etPrefNombre", "Emiliano")
    saludar = pref.getBoolean("chkPrefSaludar", false)
}

private fun initilizeLogIn() {
    logIn.setOnClickListener {
        if (completeInfo()) {
            logIn()
        } else {
            Toast.makeText(this, "Completar datos", Toast.LENGTH_LONG).show()
        }

    }
}

private fun logIn() {
    var username = etUsername.text.toString()
    var password = etPassword.text.toString()
    val db = DBHelper(this)
    if (db.search(username, password)) {
        goToTournaments()
        finish()
    } else {
        Toast.makeText(this, "Datos incorrectos", Toast.LENGTH_LONG).show()
    }

}

private fun initializeBtnSettings() {
        val intent = Intent(this@MainActivity, SettingsActivity::class.java)
        startActivity(intent)
}
private fun initilizeCreateUser() {
    createUser.setOnClickListener {
        goToCreateUser()
    }
}

private fun goToCreateUser() {
    val activityInsert = Intent(this, CreateUserActivity::class.java)
    startActivity(activityInsert)
}

private fun completeInfo(): Boolean {
    return !etPassword.text.toString().isEmpty() &&
            !etUsername.text.toString().isEmpty()
}

private fun initializeToolbar() {
    setSupportActionBar(toolbar)
    supportActionBar?.setTitle(R.string.app_name)
  //  supportActionBar?.setDisplayHomeAsUpEnabled(true)

}

private fun setupUI() {
    toolbar = findViewById(R.id.toolbarmav)
    username = findViewById(R.id.etUsername)
    password = findViewById(R.id.etPassword)
    logIn = findViewById(R.id.btnLogIn)
    createUser = findViewById(R.id.btnCreateUser)
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.toolbar1_menu, menu)
    return super.onCreateOptionsMenu(menu)
}

private fun showHelp() {
    var builder: AlertDialog.Builder = AlertDialog.Builder(this)
    builder.setTitle("Ayuda")
        .setMessage("Crea tournaments o visualiza los enfrentamientos")
        .setPositiveButton("OK") { _, _ -> }
        .setCancelable(false)
        .show()
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        R.id.menuAyuda -> showHelp()
        //android.R -> onBackPressed()
        R.id.btnSettings -> initializeBtnSettings()
    }

    return super.onOptionsItemSelected(item)
}



private fun goToTournaments() {
    val activityInsert = Intent(this, ListTournamentsActivity::class.java)
    startActivity(activityInsert)
}

}

copio xml de la activity:

<?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">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/gradient"
        tools:context=".MainActivity">


        <include
                layout="@layout/app_bar_navigation_drawer"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="120dp"
            android:layout_height="150dp"
            android:layout_marginTop="100dp"
            app:srcCompat="@drawable/logo"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"/>

    <EditText
            android:id="@+id/etUsername"
            android:layout_width="250dp"
            android:layout_height="40dp"
            android:background="#11000000"
            android:ems="10"
            android:hint="Usuario"
            android:inputType="textPersonName"
            android:textSize="16dp"
            android:layout_marginBottom="35dp"
            android:layout_above="@+id/etPassword"
            android:layout_centerHorizontal="true" />

    <EditText
            android:id="@+id/etPassword"
            android:layout_width="250dp"
            android:layout_height="40dp"
            android:background="#11000000"
            android:ems="10"
            android:hint="Contraseña"
            android:inputType="textPassword"
            android:layout_marginBottom="130dp"
            android:layout_alignBottom="@+id/cardView"
            android:layout_alignLeft="@+id/etUsername"
            android:layout_alignStart="@+id/etUsername" />

    <android.support.v7.widget.CardView
            android:layout_width="300dp"
            android:layout_height="50dp"
            app:cardBackgroundColor="@color/colorAccent"
            app:cardCornerRadius="25dp"
            app:cardElevation="10dp"
            android:id="@+id/cardView"
            android:layout_marginBottom="31dp"
            android:layout_above="@+id/btnCreateUser"
            android:layout_centerHorizontal="true">

        <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent" android:id="@+id/btnLogIn">

            <TextView
                    android:id="@+id/textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="Ingresar"
                    android:textColor="#fff"
                    android:textSize="18sp"/>
        </RelativeLayout>
    </android.support.v7.widget.CardView>

    <TextView
            android:id="@+id/btnCreateUser"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Registrate."
            android:layout_above="@+id/linearLayout"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="6dp"/>

    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true"
            android:layout_alignLeft="@+id/imageView"
            android:layout_alignStart="@+id/imageView"
            android:id="@+id/linearLayout">

        <ImageView
                android:id="@+id/imageView4"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/twitter" />

        <ImageView
                android:id="@+id/imageView3"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                app:srcCompat="@drawable/instagram" />

        <ImageView
                android:id="@+id/imageView2"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                app:srcCompat="@drawable/facebook" />
    </LinearLayout>
</RelativeLayout>

    <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_navigation_drawer"
            app:menu="@menu/activity_navigation_drawer_drawer"/>
</android.support.v4.widget.DrawerLayout>

1 respuesta 1

0

Para abrir el navigationDrawer debe realizarse de esta forma:

drawer.openDrawer(GravityCompat.START)

En el método onNavigationItemSelected() en realidad lo estas cerrando siempre, debes cambiar a :

override fun onNavigationItemSelected(item: MenuItem): Boolean {
    // Handle navigation view item clicks here.
    when (item.itemId) {
        R.id.nav_home -> {
            // Handle the camera action
        }
        R.id.nav_gallery -> {

        }
        R.id.nav_slideshow -> {

        }
        R.id.nav_tools -> {

        }
        R.id.nav_share -> {

        }
        R.id.nav_send -> {

        }
    }
    val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
    //drawerLayout.closeDrawer(GravityCompat.START)

     //* Abre Navigation Drawer.
     drawerLayout.openDrawer(GravityCompat.START)    

     return true
}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.