2017-09-21 119 views
-1

我的導航菜單中有一個非常有趣的問題。我不知道爲什麼...但我不能點擊我的菜單中的任何項目,我真的想說我不能點擊任何項目。我試圖做一個新的項目女巫已經有導航抽屜活動,當然它的作品..但是當我已經嘗試複製該代碼,並把我的..我有同樣的問題,反之亦然,我已經嘗試將我的代碼放入帶有「導航抽屜活動」的新項目中,但又是如此。我無法點擊我的導航器菜單?

這是我的主要活動

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; 
    } 
} 

回答

0

嗯,我看到上面的代碼看起來離我有兩件事情。您正在使Fragment Transaction成爲一個類級別的變量,但這些變量通常是每個事務的新變量,但這不太可能與您的問題相關,只需提一提。

然後,當您選擇的導航項目被觸發時,您將啓動一項新活動,但您並未共享該代碼。當您使用導航抽屜時,通常只需從主活動導航抽屜碎片區域中的佔位符標識中替換碎片。

以下是帶抽屜佈局的活動示例。

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

我經常做我的主,別人BaseActivity,將包括一個swapFragment例行所以它就像共享:

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(); 

} 

當然,我使用數據綁定這樣的方式它是有線的有點不同,但希望這會指出你的問題。我猜這是一個Z索引問題,所以檢查你的佈局文件可能會揭示答案。

如果沒有,請提供您的示例項目或至少您的XML佈局文件,如果可能的話,看看。由於這通常是覆蓋物阻塞觸摸的問題,並且可能由任何事物引起。您可以將佈局按順序排列,以便Z索引將抽屜放在內容之下而不是放在其前面。如果發生這種情況,您的佈局會將觸摸傳遞給內容而不是抽屜。所以檢查一下。

相關問題