【问题标题】:OnOptionsItemSelected not invoked though implemented mehtods with ScrollViewOnOptionsItemSelected 未通过使用 ScrollView 实现的方法调用
【发布时间】:2018-11-28 10:16:57
【问题描述】:

onOptionsItemSelected 不会在 Activity 和 Fragment 中调用,即使我实现了此处描述的方法:Add onOptionsItemSelected calling in Fragment
我有一个活动:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return super.onCreateOptionsMenu(menu); // false by default. so goes to fragment
    // If return true, than stay in the activity
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Toast.makeText(this, "onOptionsItemSelected", Toast.LENGTH_SHORT).show();
    switch(item.getItemId())
    {
        default:
            return super.onOptionsItemSelected(item);
            // false by default. so goes to fragment
            // If returns true stays in activity.
    }
}

这是不起作用的片段:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);((AppCompatActivity)activity).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}



@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_edit_mode,menu);
    setHasOptionsMenu(true);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Toast.makeText(activity,"onOptionItemSelected", Toast.LENGTH_SHORT).show(); // Not Invoked!!!
    if (!isDataInitialized()) return true;
    switch (item.getItemId()) {

          case R.id.item_share:  // Share Icon
            callUsernamesDialog();
            return true;


        case android.R.id.home: // Back pressing
            ( (ActivityProfile)activity).onBackPressed();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }

}

它的 onOptionsItemSelected 没有被调用但是菜单被成功地膨胀了。 在其他片段中,一切正常,我看不出在实现上有什么不同。
在其他片段中,onOptionsItemSelected 在活动和片段中都被调用。我可以在这个特定的片段中寻找什么可以阻止它工作?

我们将不胜感激!!!提前致谢!

更新
我找到了解决方案,现在可以点击项目,但我对此有疑问。

问题是我在片段中使用了滚动视图,因为其中有很多元素:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
    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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/fragment_edit_data_layout"
    tools:context=".UserStuff.EditData.FragmentEditData"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="55dp"
        android:fontFamily="cursive"
        android:lineSpacingExtra="10sp"
        android:text="@string/edit_data"
        android:textAlignment="center"
        android:textAllCaps="false"
        android:textAppearance="@style/TextAppearance.AppCompat.Button"
        android:textColor="@color/colorPrimary"
        android:textSize="60sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <EditText
        android:id="@+id/et_title"
        style="@android:style/Widget.AutoCompleteTextView"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:fontFamily="serif"
        android:hint="@string/title"
        android:inputType="textEmailAddress"
        android:textAppearance="@style/TextAppearance.AppCompat.Caption"
        android:textColor="@android:color/background_dark"
        android:textSize="18sp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        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"
        >
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="8dp"
            android:text="@string/folder_category"
            />
        <android.support.v7.widget.AppCompatButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="@dimen/standard_21"
            android:layout_gravity="center"
            android:id="@+id/btn_category"
            android:text="@string/untitled"
            android:textSize="14sp"
            />
        <android.support.v7.widget.AppCompatButton
            android:layout_margin="8dp"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="14sp"
            android:text="@string/refresh"
            android:id="@+id/btn_refresh"
            />
    </LinearLayout>
    <EditText
        android:id="@+id/et_description"
        style="@android:style/Widget.AutoCompleteTextView"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:fontFamily="serif"
        android:hint="@string/description"
        android:inputType="textEmailAddress"
        android:layout_margin="8dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Caption"
        android:textColor="@android:color/background_dark"
        android:textSize="18sp" />

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/btn_edit_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/colorPrimary"
        android:text="@string/tap_to_edit"
        android:textAlignment="center"
        android:textAllCaps="false"
        android:textColor="@color/cardview_light_background"
        android:textSize="24sp"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_edit_data"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

</LinearLayout>


</ScrollView>  

移除 ScrollView 并使用 LinearLayout 作为根视图解决了问题,问题是为什么?我看不到关于那个的文档。谢谢!

【问题讨论】:

    标签: android android-fragments android-actionbar fragment android-scrollview


    【解决方案1】:

    试试这个它在我身边工作 在活动中添加菜单

     @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.menu_edit_mode, menu);  
        return true;  
    }  
    
    @Override  
    public boolean onOptionsItemSelected(MenuItem item) {  
       int id = item.getItemId();  
        switch (id){  
            case R.id.item1:  
                Toast.makeText(getApplicationContext(),"Item 1 Selected",Toast.LENGTH_LONG).show();  
                return true;  
            case R.id.item2:  
                Toast.makeText(getApplicationContext(),"Item 2 Selected",Toast.LENGTH_LONG).show();  
                return true;  
            case R.id.item3:  
                Toast.makeText(getApplicationContext(),"Item 3 Selected",Toast.LENGTH_LONG).show();  
                return true;  
            default:  
                return super.onOptionsItemSelected(item);  
        }  
    }  
    

    用于在片段中设置菜单

    在 Fragment 的 onCreate(Bundle savedInstanceState) 方法中添加 setHasOptionsMenu(true) 方法。

    在 Fragment 中覆盖 onCreateOptionsMenu(Menu menu, MenuInflater inflater)(如果您想在 Fragment 的菜单中做一些不同的事情)和 onOptionsItemSelected(MenuItem item) 方法。

    在 onOptionsItemSelected(MenuItem item) Activity 的方法中,确保在 onOptionsItemSelected(MenuItem item) Fragment 的方法中实现菜单项操作时返回 false。

    活动

     @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
    
            case R.id.activity_menu_item:
    
                // Do Activity menu item stuff here
                return true;
    
            case R.id.fragment_menu_item:
    
                // Not implemented here
                return false;
            default:
                break;
        }
    
        return false;
    }
    

    片段

     @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Do something that differs the Activity's menu here
        super.onCreateOptionsMenu(menu, inflater);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.activity_menu_item:
                // Not implemented here
                return false;
            case R.id.fragment_menu_item:
                // Do Fragment menu item stuff here
                return true;
    
            default:
                break;
        }
    
        return false;
    }
    

    【讨论】:

    • 我对活动中的每个片段都有不同的菜单,因此在活动的 onCreateOptionsMenu 中返回 true 将防止膨胀活动片段中的其他菜单。我错过了什么
    • 感谢您的回复和更新。我发现问题不在于代码,而在于片段的布局,因此我更新了问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    • 2019-04-20
    • 1970-01-01
    相关资源
    最近更新 更多