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

时间:2017-07-07 11:29:47

标签: android android-fragments contextmenu android-context android-menu

在我的应用程序中,我在一个片段中创建了几个按钮,这些按钮将根据点击重定向到多个活动。现在,如果用户单击button1,他们将获得一个浮动上下文菜单,其中包含comapny的列表,例如company1,compan2 ...等。我已按照此帖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>

My Fragment 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;

    }

}

在MainActivity Class中

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

}

2 个答案:

答案 0 :(得分:0)

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

在你的片段中试试这个:

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

也许这会有所帮助。

答案 1 :(得分:0)

您确实使用了

  

onCreateOptionsMenu(菜单菜单)

在您的“活动”中,但您尝试使用

  

onCreateContextMenu(ContextMenu菜单,View v,   ContextMenu.ContextMenuInfo menuInfo)

在你的片段中。

在片段中使用onCreateOptionsMenu(菜单菜单),并在onCreateView方法中设置setHasOptionsMenu(true)。