2017-09-21 6 views
-1

私のナビゲータメニューには本当に興味深い問題があります。なぜ私は考えていない...しかし、私は私のメニューから任意の項目をクリックすることはできません、私は本当に私はどの項目をクリックすることはできないと言いたい。私は新しいプロジェクトの魔女にすでにNavigation Drawer Activityがあるようにしようとしていますが、もちろん動作します。しかし、そのコードをコピーして私の上に置くと、私は同じ問題を抱えています。 Navigation Drawer Activityを使用してコードを新しいプロジェクトに追加してみてください。Navigatorメニューをクリックできませんか?

ここでは、まあ、私は上記のコードから私にオフになりカップルの事を参照してください、私の主な活動

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をクラスレベルの変数にしていますが、これらは通常トランザクションごとに新しいものですが、これはあなたの問題とは関係ありません。

次に、選択したナビアイテムが起動されると、新しいアクティビティが開始されますが、そのコードは共有されていません。ナビゲーション・ドロワーを使用している場合は、通常、メイン・アクティビティーのナビ・ドロアー・フラグメント領域内のプレースホルダーIDからフラグメントをスワップアウトするだけです。

ここでは、引き出しレイアウトを使用したアクティビティの例を示します。

<?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インデックスが引き出しをその前に置くのではなく内容の下に置くようにレイアウトを並べ替えることができます。それが起こると、あなたのレイアウトは引き出しの代わりにコンテンツに触れるでしょう。だからそれもチェックしてください。

関連する問題