我们如何将数据从ViewPagerAdapter发送到TabFragment

时间:2019-01-22 11:37:20

标签: android

trying to send the string "main" from ViewPagerAdapter to 
TabFragment

I tried to send string "main" from ViewPagerAdapter to TabFragment 
using bundle. but in TabFragment the string (String value) is 
getting as null.



enter code here  public class InstallationViewPagerAdapter extends 
     FragmentPagerAdapter {
         private TabFragmentInstallation tabFragmentInstallation = new 
         TabFragmentInstallation();
         public List<String> data;

           public InstallationViewPagerAdapter(FragmentManager manager, List<String> data) {
            super(manager);
            this.data = data;
          }

          @Override
          public Fragment getItem(int position) {
          return TabFragmentInstallation.getInstance(position);
          }

          @Nullable
          @Override
          public String getPageTitle(int position) {
          String main = data.get(position);
          Log.d("SIZE12345","valuefromPA=="+main);

          Bundle args = new Bundle();
          args.putString("key", main);             <---------
          tabFragmentInstallation.setArguments(args);<---------

          return data.get(position);

          }

          @Override
          public int getCount() {
          return  data.size();
          //return title.length;
          }

        public int getItemPosition(Object object) {
        return data.size();
        //return title.length;
       }
   }

TabFragement

   public class TabFragmentInstallation extends Fragment {
   String value = new String();


      @Override
       public void onCreate(@Nullable Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
         position = getArguments().getInt("pos");
         value = getArguments().getString("key"); <---------

            }

           @Override
            public void onViewCreated(@NonNull View view,@Nullable Bundle savedInstanceState) {
              super.onViewCreated(view, savedInstanceState);
                   for (int j=0;j<response.body().getData().getVideos().size();j++){
            ---->if(value.equals(response.body().getData().getVideos().get(j).getCategoryName())){
                                 installationVideoDetails = response.body().getData().getVideos().get(j).getVideo();
                             }

}

 i wanted to get the "value" that sent from ViewPagerAdapter

trying to send the string "main" from ViewPagerAdapter to 
TabFragment

I tried to send a string "main" from ViewPagerAdapter to TabFragment 
using bundle. but in TabFragment the string (String value) is 
 getting as null.

尝试将字符串“ main”从ViewPagerAdapter发送到     TabFragment

尝试将字符串“ main”从ViewPagerAdapter发送到     TabFragment

I tried to send string "main" from ViewPagerAdapter to TabFragment 
using bundle. but in TabFragment the string (String value) is 
getting as null.

尝试将字符串“ main”从ViewPagerAdapter发送到     TabFragment

I tried to send string "main" from ViewPagerAdapter to TabFragment 
using bundle. but in TabFragment the string (String value) is 
getting as null.

尝试将字符串“ main”从ViewPagerAdapter发送到     TabFragment

I tried to send string "main" from ViewPagerAdapter to TabFragment 
using bundle. but in TabFragment the string (String value) is 
getting as null.

尝试将字符串“ main”从ViewPagerAdapter发送到     TabFragment

I tried to send string "main" from ViewPagerAdapter to TabFragment 
using bundle. but in TabFragment the string (String value) is 
getting as null.

1 个答案:

答案 0 :(得分:0)

您应该在适配器中插入List<Fragment>,例如:

class InstallationViewPagerAdapter extends FragmentPagerAdapter {
    private List<Fragment> pages = new ArrayList();

    public InstallationViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    public void addPage(Fragment page) {
        pages.add(page);
    }

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

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

您可以添加带有参数的片段并设置适配器:

 InstallationViewPagerAdapter adapter = new InstallationViewPagerAdapter(getSupportFragmentManager());
    adapter.addPage(TabFragment.newInstance("Farhad"));
    adapter.addPage(TabFragment.newInstance("Feri"));
    viewPager.setAdapter(adapter);

您的片段应该像:

    public class TabFragment extends Fragment {
        public TabFragment(){
           //empty constructor
        }
        public static TabFragment newInstance(String name){
            Bundle args = new Bundle();
            args.putString("ARG_NAME",name);
            TabFragment fragment = new TabFragment();
            fragment.setArguments(args);
            return fragment;
        }
   }