如何将选定的微调器 id 传递给片段

Posted

技术标签:

【中文标题】如何将选定的微调器 id 传递给片段【英文标题】:How to pass selected spinner id to fragment 【发布时间】:2018-07-19 21:11:13 【问题描述】:

您好,我是新手,正在开发 android 应用程序。原谅我的英语。

在我的应用程序中,我有 MainActivity.java 文件,我在其中使用 json 解析来解析 Languagelist 数组。我将它发送到下一个活动,即抽屉活动。 Drawer Activity 包含带有 TabLayout 的片段。在这个 DrawerActivity 中,我有工具栏,我在其中打开微调器并设置我从以前 (MainActivity.java) 获得的数组列表。到这里一切正常,但我想要的是当用户从工具栏微调器中选择或更改语言时,第一个选项卡片段的列表应该被刷新。但它不起作用任何人都可以帮助我解决这个问题。

DrawerActivity.java

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


        Bundle bundle=new Bundle();
        bundle.putString("frommusic", CAT_ID);
        bundle.putString("action", DRAWER_ACTION_LATEST);
        bundle.putString("sel_lang", SELECTED_LANGUAGE);

        fragmentManager = getSupportFragmentManager();
        final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragment = new LibraryFragment();
        fragment.setArguments(bundle);

        fragmentTransaction.replace(R.id.main_container_wrapper, fragment);
        fragmentTransaction.commit();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        View header = navigationView.inflateHeaderView(R.layout.nav_header_music);
        TextView profileName = (TextView) header.findViewById(R.id.profile_name);


        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() 
            @Override
            public boolean onNavigationItemSelected(MenuItem item) 
                int id = item.getItemId();

                if (id == R.id.nav_library) 
                    Bundle bundle=new Bundle();
                    bundle.putString("frommusic", CAT_ID);
                    bundle.putString("action", DRAWER_ACTION_LATEST);
                    bundle.putString("sel_lang", SELECTED_LANGUAGE);

                    fragmentManager = getSupportFragmentManager();
                    final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragment = new LibraryFragment();
                    fragment.setArguments(bundle);
                    fragmentTransaction.replace(R.id.main_container_wrapper, fragment);
                    fragmentTransaction.commit();
                 else if (id == R.id.nav_song) 
                    Bundle bundle=new Bundle();
                    bundle.putString("frommusic", CAT_ID);
                    bundle.putString("action", DRAWER_ACTION_POPULAR);
                    bundle.putString("sel_lang", SELECTED_LANGUAGE);

                    fragmentManager = getSupportFragmentManager();
                    final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragment = new LibraryFragment();
                    fragment.setArguments(bundle);
                    fragmentTransaction.replace(R.id.main_container_wrapper, fragment);
                    fragmentTransaction.commit();
                 /*else if (id == R.id.nav_playlist) 

                 else if (id == R.id.nav_sound_cound) 

                 else if (id == R.id.nav_raw_folder) 

                */

                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.replace(R.id.main_container_wrapper, fragment);
                transaction.commit();

                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                assert drawer != null;
                drawer.closeDrawer(GravityCompat.START);
                return true;
            
        );

    



    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        getMenuInflater().inflate(R.menu.music, menu);

        MenuItem item = menu.findItem(R.id.action_settings);
         spinner = (Spinner) MenuItemCompat.getActionView(item);

        rcAdapter = new CustomBaseAdapter(MusicActivity.this,  langList);
         spinner.setAdapter(rcAdapter);


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

                 LanguageModel ln=new LanguageModel();
                 ln=langList.get(position);


                 SELECTED_LANGUAGE=ln.getLangid();
                 System.out.println("IF SELECT LA"+SELECTED_LANGUAGE);

                 /*if(SELECTED_LANGUAGE.equals("1"))
                 
                     SELECTED_LANGUAGE=ln.getLangid();
                     System.out.println("IF SELECT LA"+SELECTED_LANGUAGE);
                     Bundle bundle=new Bundle();
                     bundle.putString("frommusic", CAT_ID);
                     bundle.putString("action", DRAWER_ACTION_LATEST);
                     bundle.putString("sel_lang", SELECTED_LANGUAGE);

                     fragmentManager = getSupportFragmentManager();
                     final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                     fragment = new LibraryFragment();
                     fragment.setArguments(bundle);
                     fragmentTransaction.replace(R.id.main_container_wrapper, fragment);
                     fragmentTransaction.commit();
                 
                 else
                 
                     SELECTED_LANGUAGE=ln.getLangid();
                     System.out.println("ELSE SELECT LA"+SELECTED_LANGUAGE);

                     Bundle bundle=new Bundle();
                     bundle.putString("frommusic", CAT_ID);
                     bundle.putString("action", DRAWER_ACTION_LATEST);
                     bundle.putString("sel_lang", SELECTED_LANGUAGE);

                     fragmentManager = getSupportFragmentManager();
                     final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                     fragment = new LibraryFragment();
                     fragment.setArguments(bundle);
                     fragmentTransaction.replace(R.id.main_container_wrapper, fragment);
                     fragmentTransaction.commit();
                 */


             

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

             
         );
        /*ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.spinner_list_item_array, android.R.layout.simple_spinner_item);*/

        //adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


        return true;
    

LibraryFragment.java

catid=getArguments().getString("frommusic");

        SELECTED_ACTION=getArguments().getString("action");
        SELECTED_LANGUAGE=getArguments().getString("sel_lang");
        System.out.println("in library"+SELECTED_LANGUAGE);

        tabLayout = (TabLayout)view.findViewById(R.id.tabs);
        viewPager = (ViewPager)view.findViewById(R.id.view_pager);

        viewPager.setAdapter(new CustomFragmentPageAdapter(getChildFragmentManager()));
        tabLayout.setupWithViewPager(viewPager);

        return view;
    

    public class CustomFragmentPageAdapter extends FragmentPagerAdapter 

        private  final String TAG = CustomFragmentPageAdapter.class.getSimpleName();

        private static final int FRAGMENT_COUNT = 3;

        public CustomFragmentPageAdapter(FragmentManager fm) 
            super(fm);
        

        @Override
        public Fragment getItem(int position) 
            switch (position)
                case 0:
                    Fragment f1 = new SongFragment();
                    Bundle args1 = new Bundle();
                    args1.putString("fromlibfrag", catid);
                    args1.putString("action", SELECTED_ACTION);
                    args1.putString("sel_lang", SELECTED_LANGUAGE);

                    f1.setArguments(args1);
                    return f1;
                case 1:

                    Fragment f2 = new PhotoFragment();
                    Bundle args2 = new Bundle();
                    args2.putString("fromlibfrag", catid);
                    args2.putString("action", SELECTED_ACTION);
                    f2.setArguments(args2);
                    return f2;

                case 2:
                    Fragment f3 = new SmsFragment();
                    Bundle args3 = new Bundle();
                    args3.putString("fromlibfrag", catid);
                    args3.putString("action", SELECTED_ACTION);
                    f3.setArguments(args3);
                    return f3;


            
            return null;
        

        @Override
        public int getCount() 
            return FRAGMENT_COUNT;
        

        @Override
        public CharSequence getPageTitle(int position) 
            switch (position)
                case 0:
                    return "Videos";
                case 1:
                    return "Photos";
                case 2:
                    return "SMS";

            
            return null;
        
    

SongFragment.java

getActivity().setTitle("Your Status");
        catid=getArguments().getString("fromlibfrag");

        SELECTED_ACTION=getArguments().getString("action");
        SELECTED_LANGUAGE=getArguments().getString("sel_lang");
        System.out.println("in songfragment"+SELECTED_LANGUAGE);

        vidRecyclerView = (RecyclerView)view.findViewById(R.id.song_list);
         linearLayoutManager = new LinearLayoutManager(getActivity());
        vidRecyclerView.setLayoutManager(linearLayoutManager);
        vidRecyclerView.setHasFixedSize(true);

        VIDEO_LIST_URL= WebUrls.WEB_URL+"GetVideos&language_id="+SELECTED_LANGUAGE+"&action="+SELECTED_ACTION+"&category_id="+catid;

        System.out.println("FINAL URL"+VIDEO_LIST_URL);
        pDialog = new ProgressDialog(getActivity());
        // Showing progress dialog before making http request
        pDialog.setMessage("Loading...");
        pDialog.show();
        makeJsonArrayRequestvideos();

【问题讨论】:

【参考方案1】:

LibraryFragment.java中创建一个公共方法 将其命名为 updateData() // 传递您要使用的参数。

现在

DrawerActivity.java 使 LibraryFragment 实例公开

 navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() 
            @Override
            public boolean onNavigationItemSelected(MenuItem item) 
                int id = item.getItemId();

                if (id == R.id.nav_library) 
                    Bundle bundle=new Bundle();
                    bundle.putString("frommusic", CAT_ID);
                    bundle.putString("action", DRAWER_ACTION_LATEST);
                    bundle.putString("sel_lang", SELECTED_LANGUAGE);

                    fragmentManager = getSupportFragmentManager();
                    final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    mLibraryFragment= new LibraryFragment();
                    fragment.setArguments(bundle);
                    fragmentTransaction.replace(R.id.main_container_wrapper, fragment);
                    fragmentTransaction.commit();
                 else if (id == R.id.nav_song) 
                    Bundle bundle=new Bundle();
                    bundle.putString("frommusic", CAT_ID);
                    bundle.putString("action", DRAWER_ACTION_POPULAR);
                    bundle.putString("sel_lang", SELECTED_LANGUAGE);

                    fragmentManager = getSupportFragmentManager();
                    final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragment = new LibraryFragment();
                    fragment.setArguments(bundle);
                    fragmentTransaction.replace(R.id.main_container_wrapper, fragment);
                    fragmentTransaction.commit();
                 /*else if (id == R.id.nav_playlist) 

                 else if (id == R.id.nav_sound_cound) 

                 else if (id == R.id.nav_raw_folder) 

                */

                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.replace(R.id.main_container_wrapper, fragment);
                transaction.commit();

                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                assert drawer != null;
                drawer.closeDrawer(GravityCompat.START);
                return true;
            
        );
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
             @Override
             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 

                 LanguageModel ln=new LanguageModel();
                 ln=langList.get(position);


                 SELECTED_LANGUAGE=ln.getLangid();
                 System.out.println("IF SELECT LA"+SELECTED_LANGUAGE);
                 if(mLibraryFragment!=null)
                      mLibraryFragment.updateData()//pass the param 
                you want upate in library fragment.
               

【讨论】:

能否详细说明一下 你没有得到什么? 在哪里创建方法?以及在微调监听器中写什么?

以上是关于如何将选定的微调器 id 传递给片段的主要内容,如果未能解决你的问题,请参考以下文章

将微调器值从片段传递到活动

如何将浮点矩阵作为 2D 纹理传递给片段着色器?

如何从该片段中的 onItemSelectedListener 中获取微调器单击的项目?

将统一值传递给顶点和片段着色器

如何在活动和浏览器片段之间传递值? [复制]

Android 日期选择器片段更改为微调器