Android Navigation Drawable将spinnern值发送到片段

时间:2017-06-28 09:37:23

标签: android android-fragments fragment navigation-drawer android-spinner

我使用了android studio 2.2.3中的导航绘图,片段为frag1,frag2 我想将选定的微调器值发送到所有片段, 我已设法使用bundle将第一个选定的微调器项目发送到所有片段,但是当我从微调器项目列表中选择第二个项目时  什么都行不通,这是我的代码

 public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
String data1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar 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 = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    //add this line to display menu1 when the activity is loaded
    displaySelectedScreen(R.id.nav_menu1);
}

@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.main, menu);
    ArrayList<String> list = new ArrayList<String>();
    list.add("Top News");
    list.add("Politics");
    list.add("Business");
    list.add("Sports");
    list.add("Movies");
    MenuItem item = menu.findItem(R.id.spinner);
    Spinner spinner = (Spinner) MenuItemCompat.getActionView(item);
    spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, list));
    data1=spinner.getSelectedItem().toString();
    Toast.makeText(MainActivity.this,  spinner.getSelectedItem().toString(),Toast.LENGTH_LONG).show();
    return true;
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    displaySelectedScreen(item.getItemId());
    return true;
}

private void displaySelectedScreen(int itemId) {
    //creating fragment object
    Fragment fragment = null;
    switch (itemId) {
        case R.id.nav_menu1:
            fragment = new frag1();
            break;
        case R.id.nav_menu2:
            fragment = new frag2();
            Bundle data2 = new Bundle();
            data2.putString("data", data1);
            fragment.setArguments(data2);
            break;
        case R.id.nav_menu3:
            fragment = new frag3();
            Bundle data = new Bundle();
            data.putString("data", data1);
            fragment.setArguments(data);
            break;
    }

    if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame, fragment);
        ft.commit();
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
}

}

  public class frag2  extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_menu_3, container, false);
    TextView text = (TextView) view.findViewById(R.id.textView3);
    String getArgument = getArguments().getString("data");
    text.setText(getArgument);
    return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getActivity().setTitle("Menu 1");
}

}

那么我在哪里以及如何修改我的代码呢? 1.i可以将选定的微调项目发送到所有片段吗? 2.i可以在片段之间导航时保​​留所选微调器值的状态吗?

1 个答案:

答案 0 :(得分:1)

使用onItemSelected侦听器并执行您想要在那里执行的操作

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //Here
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });