【问题标题】:How Can I Pass Information to my Fragments Inside ViewPager Tabs?如何将信息传递到 ViewPager 选项卡中的片段?
【发布时间】:2020-08-07 11:04:43
【问题描述】:

我有一个主要的家庭活动,它由屏幕底部的 3 个选项卡组成。

在这个activity里面我有一个fragment

此片段包含view pager。此视图寻呼机用于在 2 个活动之间导航:

1.Exercise Form = 显示练习视频。

2.Exercise History = 显示所有用户记录的锻炼数据。

要获取锻炼表单视频以及锻炼历史记录,我需要能够从 ProgressFagment2(屏幕顶部的文本)访问锻炼名称并将其传递给 2 个子片段(锻炼家庭视频片段和锻炼历史片段)

如何访问子片段中的exerciseName

ProgressFragment2(片段内部活动)

public class Progress_Fragment2 extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.exercises_history_demo, container, false);

    }

    public void onViewCreated(View view, Bundle savedInstanceState) {

         childExerciseName = getArguments().getString("ExerciseName");

        TextView title_tv = getView().findViewById(R.id.textView_exerciseName8);
        title_tv.setText(childExerciseName);

        SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
        // Set up the ViewPager with the sections adapter.
        ViewPager mViewPager = (ViewPager) getView().findViewById(R.id.view_pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        TabLayout tabLayout = (TabLayout) getView().findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Inflate the menu; this adds items to the action bar if it is present.
        inflater.inflate(R.menu.top_navigation_two, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            switch (position) {
                case 0:
                    fragment = new ExerciseHomeVideoFragment();
                    break;
                case 1:
                    fragment = new ExerciseHistoryFragment();
                    break;

            }
            return fragment;
        }

        @Override
        public int getCount() {
            // Show 2 total pages.
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Exercise Form";
                case 1:
                    return "Exercise History";
            }
            return null;
        }
    }
}

ExerciseHomeVideoFragment(viewPager 中的 2 个片段之一)|仅相关代码

public class ExerciseHomeVideoFragment extends Fragment implements View.OnClickListener {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_exercise_info_home_version, container, false);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void onViewCreated(View view, Bundle savedInstanceState) {


        Progress_Fragment2 parentFrag = ((Progress_Fragment2)ExerciseHomeVideoFragment.this.getParentFragment());


        youTubeBtn = getView().findViewById(R.id.youTubeSearchButton);
        parent_tv = getView().findViewById(R.id.textView_exerciseName8);

  childExerciseViewModel = ViewModelProviders.of(this).get(ChildExerciseViewModel.class);

//
// HOW CAN I ACCESS THE CHILDEXERCISE NAME???
//

        
//         childExerciseName = getArguments().getString("ExerciseName"); // THIS DOESNT WORK
       
 //  exerciseNameTV.setText(childExerciseName + " Demo");

    }
}


【问题讨论】:

    标签: android android-fragments android-viewpager android-fragmentactivity


    【解决方案1】:

    您可以使用接口、设置参数或 newInstance 发送数据

    在您的标签片段中

     switch (position) {
            case 0:
              fragment = new ExerciseHomeVideoFragment().newInstance(childExerciseName);
              break;
            case 1:
              fragment = new ExerciseHomeVideoFragment();
              break;
    
          }
    

    ExerciseHomeVideoFragment

    import android.os.Build;
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.annotation.RequiresApi;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    public class ExerciseHomeVideoFragment extends Fragment {
    
    
      public static ExerciseHomeVideoFragment newInstance(String  ExerciseName) {
        Bundle args = new Bundle();
    
        args.putString("ExerciseName", ExerciseName);
        ExerciseHomeVideoFragment fragment = new ExerciseHomeVideoFragment();
        fragment.setArguments(args);
        return fragment;
      }
    
    
      @Nullable
      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
        return inflater.inflate(R.layout.fragment_exercise_info_home_version, container, false);
      }
    
      @RequiresApi(api = Build.VERSION_CODES.O)
      public void onViewCreated(View view, Bundle savedInstanceState) {
    
        TextView title_tv = getView().findViewById(R.id.textView_exerciseName8);
       //
    
        Bundle bundle =  getArguments();
        if(bundle!=null)
        {
          title_tv.setText(bundle.getString("ExerciseName"));
        }
    
    
    
    
      }
    }
    

    你也可以发送模型,但模型应该使用序列化器实现

     switch (position) {
            case 0:
    
              
    
              fragment = new ExerciseHomeVideoFragment().newInstance(childExerciseViewModel);
              break;
            case 1:
              fragment = new ExerciseHomeVideoFragment();
              break;
    
          }
    

    在片段中

    public static ExerciseHomeVideoFragment newInstance(String  ExerciseName,ChildExerciseViewModel childExerciseViewModel) {
        Bundle args = new Bundle();
    
        args.putString("ExerciseName", ExerciseName);
        args.putSerializable("model", childExerciseViewModel);
        ExerciseHomeVideoFragment fragment = new ExerciseHomeVideoFragment();
        fragment.setArguments(args);
        return fragment;
      }
    
    
    
    
     Bundle bundle =  getArguments();
        if(bundle!=null)
        {
          title_tv.setText(bundle.getString("ExerciseName"));
    
          ChildExerciseViewModel  model = (ChildExerciseViewModel) bundle.getSerializable("model");
          title_tv.setText(model.gettourExerciseText());
        }
    

    【讨论】:

      【解决方案2】:

      我认为使用更好的架构是个好主意。例如,如果您使用单例类来存储数据,您将能够在不传递给片段的情况下使用它们。搜索单身人士。 Android 有一个新功能在 3 个月后发布。您可以使用 setFragmentResultListener。 这是文档: https://developer.android.com/reference/androidx/fragment/app/FragmentManager#setfragmentresultlistener

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-24
        • 2012-04-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多