我的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 :(得分: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>
我经常为我的Main和其他人制作一个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();
}
当然我使用DataBinding所以它的连接方式有点不同,但希望这可以指出你的问题。我猜这是一个Z索引问题,所以检查你的布局文件可能会揭示答案。
如果没有,请提供您的示例项目或至少您的xml布局文件(如果可能)。因为这通常是覆盖触摸的叠加问题,并且可能由任何数量的事物引起。您可能已将布局排除在外,因此Z索引将抽屉放在内容下而不是前面。如果发生这种情况,您的布局会将触摸传递给内容而不是抽屉。所以也要检查一下。