单击菜单项,片段显示

时间:2015-07-07 03:16:49

标签: android android-fragments android-activity

我一直在寻找这个但是无法实现它。我的问题是工具栏中有两个菜单 action_settings repeat_entry ,当我点击action_settings片段类别设置应该显示,当我点击repeat_entry 重复输入片段应显示

这是我的 menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />
    <item android:id="@+id/repeat_entry" android:title="Repeat Entry"
        android:orderInCategory="100" app:showAsAction="never" />
    <item
        android:id="@+id/action_search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="always"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:title="Search"/>
</menu>

继承我的onCreateOptionsMenu和onOptionsItemSelected MainActivity.java

   public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            menu.removeItem(R.id.action_search);
            getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
        }


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


        Fragment fr = null;
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            selectTabToShow(String.valueOf(R.id.action_search));
            CategorySettings fragemnt = (CategorySettings) getFragmentManager().findFragmentById(R.id.categorySettings);
            fr = new CategorySettings();

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

            Toast.makeText(MainActivity.this,"ssdf sdf",Toast.LENGTH_SHORT).show();
            fr = new RepeatEntry();
        }

        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.categorySettings,fr);
        fragmentTransaction.commit();

        return super.onOptionsItemSelected(item);
    }

这是 activity_main.xml

的一部分
<LinearLayout
            android:id="@+id/category_cont"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <fragment
                android:name="me2.nakame.hp.moneytracer.CategorySettings"
                android:id="@+id/categorySettings"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </LinearLayout>

这是我的片段RepeatEntry和CategorySettings几乎相似

public class RepeatEntry extends Fragment{

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.repeat_entry,container,false);

        return v;
    }
}

我的问题只有CategorySettings正在显示

1 个答案:

答案 0 :(得分:1)

我已经解决了我的问题,只需删除 <fragment>并使用.replace方法进行片段交易

这是 activity_main.xml 的更新部分,其中我删除了片段

<LinearLayout
            android:id="@+id/category_cont"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

</LinearLayout>

现在这是我的主要活动

onOptionsItemSelected的更新方法
 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();


            Fragment fragment = null;
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                fragment = new CategorySettings();
            }else if(id == R.id.repeat_entry){
                fragment = new RepeatEntry();

            }

            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.category_cont,fragment);
            ft.setCustomAnimations(android.R.animator.fade_in,android.R.animator.fade_out);
            ft.commit();

            return super.onOptionsItemSelected(item);
        }