2017-09-21 12 views
-1

J'ai un problème très intéressant avec mon menu Navigateur. Je n'ai aucune idée pourquoi ... Mais je ne peux pas cliquer sur un élément de mon menu, je veux vraiment dire que je ne peux pas cliquer sur un élément. J'ai essayé de faire un nouveau projet qui a déjà une activité de tiroir de navigation, bien sûr cela fonctionne .. mais quand j'ai essayé de copier ce code et de mettre sur le mien .. j'ai le même problème et vice versa, j'ai essayez de mettre mon code dans un nouveau projet avec Navigation Drawer Activity, mais encore une fois.Je ne peux pas cliquer sur mon menu Navigateur?

Ici, il est mon activité principale

package radiofm.arabel; 

import android.app.ProgressDialog; 
import android.content.Intent; 
import android.media.AudioManager; 
import android.media.MediaPlayer; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentTransaction; 
import android.util.Log; 
import android.view.View; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Button; 
import android.widget.ImageButton; 

import java.io.IOException; 

import static android.widget.AbsListView.OnScrollListener.SCROLL_STATE_IDLE; 

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    ImageButton id_play,id_pause; 

    private Button btn; 
    private boolean playPause; 
    private MediaPlayer mediaPlayer; 
    private ProgressDialog progressDialog; 
    private boolean initialStage = true; 
    Boolean prepared = false; 

    String AudioRd ="http://arabelfm.ice.infomaniak.ch/arabelprodcastfm.mp3"; 

    FragmentTransaction fragmentTransaction; 
    NavigationView navigationView=null; 
    Toolbar toolbar =null; 



    private DrawerLayout mDrawerLayout; 
    private ActionBarDrawerToggle mToggle; 



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

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     Log.d("myTag", "This is my toolbar"); 


     id_play = (ImageButton) findViewById(R.id.id_play); 
     progressDialog = new ProgressDialog(this); 
     id_play.setEnabled(true); 
     id_play.setImageResource(R.drawable.play); 
     id_play.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if (!playPause) { 
        id_play.setImageResource(R.drawable.pause); 
        startService(new Intent(MainActivity.this, Myservice.class)); 
        Log.e("start", "start radio"); 

        playPause = true; 

       } else { 
        id_play.setImageResource(R.drawable.play); 
        stopService(new Intent(MainActivity.this, Myservice.class)); 
        Log.e("stop", "start radio"); 

        playPause = false; 
       } 
      } 
     }); 




     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
    } 



    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.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; 
     } 

     return super.onOptionsItemSelected(item); 
    } 



    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_alarm) { 
      Intent goToNextActivity = new Intent(getApplicationContext(), Add_Alarm.class); 
      startActivity(goToNextActivity); 
     } else if (id == R.id.nav_aaaaa) { 


     } else if (id == R.id.nav_slideshow) { 

     } else if (id == R.id.nav_manage) { 

     } 


     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 
} 

Répondre

0

Eh bien, je vois un couple de choses qui ont l'air de me de votre code ci-dessus. Vous faites de Fragment Transaction une variable de niveau classe, mais celles-ci sont généralement nouvelles par transaction, mais cela n'est probablement pas pertinent pour votre problème.

Ensuite, lorsque votre élément nav sélectionné est déclenché, vous lancez une nouvelle activité, mais vous ne partagez pas ce code. Lorsque vous utilisez un tiroir de nav, vous échangez généralement des fragments d'un espace réservé dans la zone principale du fragment de tiroir de l'activité.

Voici un exemple d'activité avec la disposition des tiroirs.

<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 
<data> 
    <variable name="activity" type="com.appstudio35.activities.HomeActivity"/> 
    <variable name="isLegalSelected" type="boolean"/> 
</data> 

<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

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

     <!-- FrameLayout is used to insert fragments to display --> 
     <FrameLayout 
      android:id="@+id/fragPlaceholder" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

    </LinearLayout> 

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

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

      <include layout="@layout/nav_drawer_header" 
       android:id="@+id/navHeader"/> 

      <TextView 
       android:id="@+id/txtLegal" 
       android:layout_alignParentBottom="true" 
       android:layout_width="match_parent" 
       android:layout_height="50dp" 
       android:textSize="@dimen/am_text_size_18" 
       android:gravity="center_vertical" 
       android:onClick="@{activity::btnLegal_onClick}" 
       android:paddingStart="16dp" 
       android:textColor="@{isLegalSelected ? @color/white : @color/text_color_dark_gray}" 
       android:background="@{isLegalSelected ? @color/colorPrimary : @color/white}" 
       tools:background="@color/colorPrimary" 
       tools:textColor="@color/white" 
       android:text="Legal"/> 

      <android.support.v7.widget.RecyclerView 
       android:id="@+id/lstMenuItems" 
       android:layout_above="@+id/txtLegal" 
       android:layout_below="@+id/navHeader" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       app:rcvInterface="@{activity}" 
       app:rcvList="@{activity.getDrawerItemList()}"/> 

     </RelativeLayout> 

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

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

Je fais souvent un BaseActivity pour mon principal et d'autres et comprendra une routine de swapFragment il est partagé comme:

protected void swapFragment(BaseFragment fragment, @Nullable Bundle bundle) { 
    if (fragment.isVisible()) { 
     return; 

    } 

    A35Log.v(TAG, "swapFragment(" + fragment.getClass().getSimpleName() + ")"); 
    Bundle currentFragBundle = fragment.getArguments(); 
    if (currentFragBundle == null && bundle != null) { 
     fragment.setArguments(bundle); 

    } else if (bundle != null) { 
     fragment.getArguments().putAll(bundle); 

    } 


    FragmentManager fragmentManager = getSupportFragmentManager(); 
    fragmentManager.executePendingTransactions(); 

    //Make sure the requested fragment isn't already on the screen before adding it 
    if (fragment.isAdded()) { 
     if (fragment.isHidden()) { 
      fragmentManager.beginTransaction().show(fragment).commit(); 

     } 

     A35Log.v(TAG, "Fragment was hidden, so removeFullScreen instead of replace"); 
     return; 

    } 

    mSelectedFragment = fragment; 
    A35Log.v(TAG, "Replacing Active Fragment with " + fragment.getClass().getSimpleName()); 

    FragmentManager manager = getSupportFragmentManager(); 
    FragmentTransaction fragmentTransaction = manager.beginTransaction(); 
    fragmentTransaction.replace(R.id.fragPlaceholder, fragment); 
    fragmentTransaction.commit(); 

} 

Bien sûr, je suis en utilisant DataBinding donc la façon il est câblé est un peu différent, mais j'espère que cela vous indique votre problème. Je suppose qu'il s'agit d'un problème lié à l'index Z, donc l'examen de votre fichier de disposition révèlera probablement la réponse.

Si ce n'est pas le cas, veuillez fournir votre projet exemple ou au moins votre fichier de mise en page xml si possible. Comme il s'agit généralement d'un problème de superposition qui bloque le toucher, il peut être causé par un certain nombre de choses. Vous auriez pu mettre votre mise en page dans le désordre, donc l'index Z place le tiroir sous le contenu plutôt que devant. Si cela arrive, votre mise en page passera le contact au contenu au lieu du tiroir. Alors vérifiez cela aussi.