在 Fragment 中单击按钮时的浮动上下文菜单

Floating context menu on button click inside Fragment

在我的应用程序中,我在一个片段中创建了几个按钮,这些按钮将根据点击重定向到多个 activity。现在,如果用户单击按钮 1,他们将获得一个包含公司列表的浮动上下文菜单,例如公司 1、公司 2 等。我已按照此 post Opening a floating menu (context menu) in Android? 在我的应用程序中开发此功能。但问题是这段代码是在 Activity 中实现的,在我的例子中,我希望在 Fragment 中实现它。我编写了代码,但单击按钮没有任何反应。现在如何在单击按钮时生成此菜单

我的上下文菜单是

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

<item android:id="@+id/company_1"
    android:title="@string/company_1"></item>

<item android:id="@+id/company_2"
android:title="@string/company_2"></item>

<item android:id="@+id/company_3"
android:title="@string/company_3"></item>

<item android:id="@+id/company_4"
    android:title="@string/company_4">
</item>
<item android:id="@+id/company_5"
    android:title="@string/company_5"></item>

<item android:id="@+id/company_6"
    android:title="@string/company_6">
</item>

我的片段 Class 是

public class MainFragment extends Fragment implements View.OnClickListener{


public MainFragment() {
    // Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_main, container, false);

    Button button1=(Button) view.findViewById(R.id.button1);
    button1.setOnClickListener(this);
    registerForContextMenu(button);
    Button button2=(Button) view.findViewById(R.id.button2);
    button2.setOnClickListener(this);
    Button button3=(Button) view.findViewById(R.id.button3);
    button3.setOnClickListener(this);

    // Inflate the layout for this fragment
    return view;

}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    if(v.getId()==R.id.button1){
        this.getActivity().getMenuInflater().inflate(R.menu.contextmenu_company,menu);
    }
    super.onCreateContextMenu(menu, v, menuInfo);

}

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

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.company_1:
            Toast.makeText(getActivity().getApplicationContext(),"caompany1code",Toast.LENGTH_LONG).show();
            return true;
        case R.id.company_2:
           ......
        case R.id.company_3:
            .....
        case R.id.company_4:
           ....
            return true;
        case R.id.company_5:
            .....
            return true;
        case R.id.company_6:
           .....
            return true;
    }
    return super.onContextItemSelected(item);
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.button1:

            Toast.makeText(getActivity(),"My colleagues clicked", Toast.LENGTH_SHORT).show();
            break;
        case R.id.button2:
            Toast.makeText(getActivity(), "News clicked", Toast.LENGTH_SHORT).show();
            break;
        case R.id.button3:
            Toast.makeText(getActivity(), "Navigator clicked", Toast.LENGTH_SHORT).show();
            break;

    }

}

主要ActivityClass

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

NavigationView navigationView = null;
Toolbar toolbar = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    //Set the fragment initially
    MainFragment fragment = new MainFragment();
    android.support.v4.app.FragmentTransaction fragmentTransaction =
            getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, fragment);
    fragmentTransaction.commit();
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    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) 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.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) {
   .....

}

好吧,我在这里找到了一些东西:

在你的片段中试试这个:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
if(v.getId()==R.id.button1{getActivity().getMenuInflater().inflate(R.menu.contextmenu_company,menu);
}
}

你这也叫错了:

//Instead of button use button1.
registerForContextMenu(button1);

也许这会有所帮助。

你确实用过

onCreateOptionsMenu(Menu menu)

在您的 Activity 中,但您正在尝试使用

onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)

在你的片段中。

在片段中也使用 onCreateOptionsMenu(Menu menu) 并在 onCreateView 方法中设置 setHasOptionsMenu(true)。