如何从片段中的工具栏中侦听事件?

时间:2016-12-20 17:26:13

标签: java android android-fragments fragment android-toolbar

使用tablayout和Viewpager创建一个包含One活动的应用程序,我有3个选项卡,每个选项卡都有一个片段。主要活动有一个带有Swich的工具栏。

重点是我需要在主要活动和片段中监听此开关的事件变化。实际上我在MainActivity中调用了onCateAptionsMenu和onOptionItemSelected并且可以工作但是可以再次使用这些方法吗? o如何从片段中监听工具栏中UI控件的事件?

Mi Code是:

MainActivity.java

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

    Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(myToolbar);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);



    if(ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
        wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
    }else{
        if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.ACCESS_FINE_LOCATION)){
            Toast.makeText(MainActivity.this,"Debes autorizar los permisos de ubicacion",Toast.LENGTH_SHORT).show();
        }else{
            ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},LOCATION_REQUEST_CODE);
        }
    }


}

private void setupViewPager(ViewPager viewPager){
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new OneFragment(), getString(R.string.tabRedes));
    adapter.addFragment(new TwoFragment(),getString(R.string.tabRedesGuardadas));
    adapter.addFragment(new ThreeFragment(),getString(R.string.tabMapas));
    viewPager.setAdapter(adapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.menuactionbar,menu);
    final SwitchCompat switch1= (SwitchCompat) menu.findItem(R.id.switch_wifi).getActionView().findViewById(R.id.switchForActionBar);


    if(ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){

        switch1.setChecked(wifiManager.isWifiEnabled());

        if(wifiManager.isWifiEnabled()){
            switch1.setText(R.string.switchOnWifiEs);
        }else {
            switch1.setText(R.string.swtichOffWifiEs);
            //wifiManager.setWifiEnabled(true);
        }

        switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    switch1.setText(R.string.switchOnWifiEs);
                    wifiManager.setWifiEnabled(true);
                    Toast.makeText(MainActivity.this,R.string.switchingOnWifiEs,Toast.LENGTH_SHORT).show();
                }
                else{
                    switch1.setText(R.string.swtichOffWifiEs);
                    wifiManager.setWifiEnabled(false);
                    Toast.makeText(MainActivity.this,R.string.switchingOffWifiEs,Toast.LENGTH_SHORT).show();
                }
            }
        });

    }else{
        if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.ACCESS_FINE_LOCATION)){
            Toast.makeText(MainActivity.this,"Debes autorizar los permisos de ubicacion",Toast.LENGTH_SHORT).show();
        }else{
            ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},LOCATION_REQUEST_CODE);
        }
    }


    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){

    switch (item.getItemId()){
        case R.id.menu_settings:
            Intent nav = new Intent(MainActivity.this,configuracion_Activity.class);
            startActivity(nav);
            return true;
        case R.id.menu_acercade:
            Intent nav2 = new Intent(MainActivity.this,acercade_Activity.class);
            startActivity(nav2);
            return true;
        default:
            Toast.makeText(MainActivity.this,"Estas entrando en e Default",Toast.LENGTH_SHORT).show();
            return super.onOptionsItemSelected(item);
    }
}

3 个答案:

答案 0 :(得分:0)

在Switch的CheckedChangeListener中

  switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked){
                        switch1.setText(R.string.switchOnWifiEs);
                        wifiManager.setWifiEnabled(true);
                        Toast.makeText(MainActivity.this,R.string.switchingOnWifiEs,Toast.LENGTH_SHORT).show();                 
                    }
                    else{
                        switch1.setText(R.string.swtichOffWifiEs);
                        wifiManager.setWifiEnabled(false);
                        Toast.makeText(MainActivity.this,R.string.switchingOffWifiEs,Toast.LENGTH_SHORT).show();
                    }
final Fragment currentFragment = getFragmentManager().findfragmentId(R.id.container);
if(currentFragment instance HomeFragment){
((HomeFragment)currentFragment).switchClicked(isChecked);
}


                }
            });

在你的片段中:

public void switchClicked(final boolean isChecked){
...... your code.
}

答案 1 :(得分:0)

我自己找到答案,IDK是否是最好的方法,但对我来说效果很好:

在我的片段中:

2

答案 2 :(得分:0)

你应该看看这个 How to get Toolbar from fragment?

很久以前这对我很有帮助。 d: