将数据从 Activity 传递到选项卡式布局片段?

Passing a data from Activity to Tabbed Layout Fragment?

我有两个活动。一个是普通的 Activity,它包含列表视图,第二个是选项卡式布局,它有两个选项卡。一个是 "Description" 选项卡,另一个是 "Sample Code" 选项卡。

我为每个创建了两个单独的片段,即 "OneFragment" 和 "TwoFragment"。我只需要当用户从我的正常 Activity 中单击一个项目时,该项目将存储在字符串中,该字符串应传递给选项卡式布局,并且应显示在 OneFragment 中,即 "Description" 选项卡

(例如)。我的列表包含 {"Android","Studio"} 如果用户单击 Android,此文本应传递到描述选项卡并使用 TextView 显示文本。请帮我。在这里我包括我各自的代码..

正常Activity - >ListActi.java

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        lv = (ListView) findViewById(R.id.listView);
        final List<String> ListElementsArrayList = new ArrayList<String>(Arrays.asList(listElements));
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (getApplicationContext(), android.R.layout.simple_list_item_1, ListElementsArrayList);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Toast.makeText(getApplicationContext(), listElements[position],Toast.LENGTH_LONG).show();
                s = listElements[position];
                if(position == 1){
                    s = listElements[position];
                    Bundle bundle = new Bundle();
                    bundle.putString("ClickedItem", listElements[position]);
                    OneFragment fragobj = new OneFragment();
                    TwoFragment fag = new TwoFragment();
                    fragobj.setArguments(bundle);
                    fag.setArguments(bundle);
                    Intent i = new Intent(getApplicationContext(),BothLay.class);
                    startActivity(i);
                }
           }
        });
    }
}

TabActivity -> BothLay

public class BothLay extends AppCompatActivity  {
    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_both_lay);
        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(), "Description");
        adapter.addFragment(new TwoFragment(), "Sample Code");
        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);
        }
    }
}

Description 选项卡 - >OneFragment(我只需要将数据从 Normal Activity 传递到此 Fragment)

public class OneFragment extends Fragment{
    String strtext = "Ajay";
    public OneFragment() {
        // Required empty public constructor
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootview = inflater.inflate(R.layout.fragment_one, container, false);
        TextView textView = (TextView) rootview.findViewById(R.id.t1);
        //strtext = getIntent().getStringExtra("ClickedItem"); //this doesn't work  
        textView.setText(strtext);
        return rootview;
    }
}

示例代码选项卡 ->Twofragment

public class TwoFragment extends Fragment{
    public TwoFragment() {
        // Required empty public constructor
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_two, container, false);
    }

}

这里:

fragobj.setArguments(bundle);
fag.setArguments(bundle);

点击按钮无效,因为当前开始 BothLay Activity 而不是将 Fragment 添加到 FragmentManager

让它工作:

1. 使用 Intent 将包传递到 BothLay 单击按钮时的活动:

  Intent i = new Intent(getApplicationContext(),BothLay.class);
  i.putExtras(bundle);
  startActivity(i);

2.BothLay onCreate 中的 Intent 获取 Bundle Activity 然后在调用 adapter.addFragment 时将其传递给 Fragment方法:

  Bundle bundle = getIntent().getExtras();
  tabLayout.setupWithViewPager(viewPager,bundle);

将包传递给 Fragment 对象:

   private void setupViewPager(ViewPager viewPager,Bundle bundle) {
        ...
        Fragment fragmentOne=new OneFragment();
        fragmentOne.setArguments(bundle);
        adapter.addFragment(fragmentOne, "Description");
        // do same for second Framgment
   }

编辑:

要获取 Fragment 中的数据调用 onCreateView 中 Fragment 的 getArguments 方法:

Bundle bundle=getArguments();
if(bundle!=null){
  if(bundle.containsKey("ClickedItem")){
     String strClickedItem=bundle.getString("ClickedItem");
   }
}

执行我提供的所有步骤 link。

当您的片段恢复时,需要执行第 4 步。

以下代码将在您的 activity,

SelectedBundle selectedBundle;

最后你会得到selectedBundle上的fragment实例。

因此,您可以轻松地将值传递给片段。喜欢,

  fragment.setData(String passedData);

就这些了。