2017-06-05 6 views
0

私は主な活動で定義されたアクションバーメニューを持っています。さて、私はそのアクティビティで使用しているフラグメントの1つに独自のアクションバーメニューがありますが、メニューオプションをクリックすると、フラグメントとアクティビティのメニュー項目の両方が取得されます。そのフラグメントのメニュー項目だけが表示されるようにする方法は?そのフラグメントに対する特定のアクションバーメニューのみを膨張させる

私のJavaコードは次のとおりです。

public class ProfileD extends Fragment { 

TextView tv_named, tv_genderd, tv_cfd, tv_aged, tv_biod, tv_statusd; 
ImageView imageView_dp; 

public ProfileD() { 
    // Required empty public constructor 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View rootView = inflater.inflate(R.layout.fragment_profile_d, container, false); 

    tv_named = (TextView)rootView.findViewById(R.id.tv_named); 
    tv_genderd = (TextView)rootView.findViewById(R.id.tv_genderd); 
    tv_cfd= (TextView)rootView.findViewById(R.id.tv_cfd); 
    tv_aged = (TextView)rootView.findViewById(R.id.tv_aged); 
    tv_biod = (TextView)rootView.findViewById(R.id.tv_biod); 

    tv_statusd = (TextView) rootView.findViewById(R.id.tv_statusd); 

    imageView_dp = (ImageView)rootView.findViewById(R.id.imageView_dp); 
    setHasOptionsMenu(true); 

    return rootView; 
} 

@Override 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 

    super.onCreateOptionsMenu(menu, inflater); 

    getActivity().getMenuInflater().inflate(R.menu.profile_menu, menu); 
} 

@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.edit_profile) { 

     Intent intent = new Intent(getContext(),PersonalDetails.class); 
     intent.putExtra("Edit",1); 
     startActivity(intent); 

     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

@Override 
public void onResume() { 
    super.onResume(); 

    AppCompatActivity appCompatActivity = (AppCompatActivity)getActivity(); 
    ActionBar actionBar = appCompatActivity.getSupportActionBar(); 
    actionBar.setTitle("Profile"); 
} 
} 

メニューのxmlファイルは次のとおりです。

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 

<item 
    android:id="@+id/edit_profile" 
    android:orderInCategory="100" 
    android:title="Edit" 
    app:showAsAction="never" /> 

</menu> 

答えて

0

私は私の問題への解決策を見つけました。

私は静的メニューオブジェクトを作成し、そのオブジェクトにアクティビティのアクションバーメニューを保存しました。これは私がここに投稿しています。私はその後、私のフラグメントから、そのオブジェクトと呼ばれ、それによって、ここでは、アクティビティのメニューをクリアし、フラグメントのメニューで

私を残して、それをクリアフラグメント

public class ProfileD extends Fragment { 

TextView tv_named, tv_genderd, tv_cfd, tv_aged, tv_biod, tv_statusd; 
ImageView imageView_dp; 

public ProfileD() { 
    // Required empty public constructor 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View rootView = inflater.inflate(R.layout.fragment_profile_d, container, false); 

    tv_named = (TextView)rootView.findViewById(R.id.tv_named); 
    tv_genderd = (TextView)rootView.findViewById(R.id.tv_genderd); 
    tv_cfd= (TextView)rootView.findViewById(R.id.tv_cfd); 
    tv_aged = (TextView)rootView.findViewById(R.id.tv_aged); 
    tv_biod = (TextView)rootView.findViewById(R.id.tv_biod); 

    tv_statusd = (TextView) rootView.findViewById(R.id.tv_statusd); 

    imageView_dp = (ImageView)rootView.findViewById(R.id.imageView_dp); 
    setHasOptionsMenu(true); 

    return rootView; 
} 

@Override 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 

    super.onCreateOptionsMenu(menu, inflater); 

    HomePage.menu_home.clear(); 

    getActivity().getMenuInflater().inflate(R.menu.profile_menu, menu); 
} 

@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.edit_profile) { 

     Intent intent = new Intent(getContext(),PersonalDetails.class); 
     intent.putExtra("Edit",1); 
     startActivity(intent); 

     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

@Override 
public void onResume() { 
    super.onResume(); 

    AppCompatActivity appCompatActivity = (AppCompatActivity)getActivity(); 
    ActionBar actionBar = appCompatActivity.getSupportActionBar(); 
    actionBar.setTitle("Profile"); 
} 
} 
の更新されたJavaコードであります
関連する問題