如何将一个活动的片段内的选定导航抽屉项目和微调器项目传递给第二个活动?

时间:2016-09-01 20:39:11

标签: android android-fragments android-spinner

我的第一个活动中有一个导航抽屉,在导航抽屉上选择后,我将显示一个名为SpinnerFragment的碎片,里面有一个微调器。我想将选定的导航项和选定的微调器项传递给我的第二个活动MainActivity2.java。我没有添加xml文件,如果需要添加它们请告诉我,我会添加它们。

有人可以帮我如何传递我在导航抽屉和旋转器中选择的数据???

MainActivity.java

navigationView=(NavigationView)findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId())
            {
                case R.id.home_id1:
                    fragmentTransaction=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.maincontainer,new HomeFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("HomeFragmentYoyo");
                    item.setChecked(true);
                    drawerLayout.closeDrawers();
                    break;
                 case  R.id.home_id2:
                    fragmentTransaction=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.maincontainer,new SpinnerFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("Airtel");
                    item.setChecked(true);
                    drawerLayout.closeDrawers();
                    break;
                  }//switch case close
            return false;
        }//navigationitemselected close

SpinnerFragment.java(在home_id_2中传递片段)

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View v=   inflater.inflate(R.layout.fragment_spinner, container, false);
    setSpinnerContent(v);

    Button button=(Button)v.findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent= new Intent(getActivity(),MainActivity2.class);
            startActivity(intent);
        }
    });
    return  v;
}

private void setSpinnerContent(final View v)
{

    spinner =(Spinner)v.findViewById(R.id.spinner);
    final ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(v.getContext(),R.array.states_names,android.R.layout.simple_spinner_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            String selected=adapterView.getItemAtPosition(i).toString();
            Context context=adapterView.getContext();
            CharSequence text=selected;
            int duration= Toast.LENGTH_SHORT;
            Toast toast=Toast.makeText(context,text,duration);
            toast.show();
        }

MainActivity2.java

public class MainActivity2 extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;


@Override
protected void onCreate(Bundle savedInstanceState) {

    String m;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}


private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new OneFragment(), "ONE");
    adapter.addFragment(new TwoFragment(), "TWO");
    adapter.addFragment(new ThreeFragment(), "THREE");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);

    }
}
}

0 个答案:

没有答案