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

时间:2016-12-26 05:56:00

标签: java android android-fragments android-intent android-tabactivity

我有两项活动。一个是包含listview的普通Activity,第二个是Tabbed Layout,它有两个选项卡。一个是"描述"标签和另一个是"示例代码"标签

我为每个创建了两个单独的片段,即#34; OneFragment"和" TwoFragment"。我需要的是当用户从我的普通活动中点击一个项目时,该项目将存储在字符串中,该字符串应该传递给选项卡式布局,并且应该在OneFragment中显示,即"描述"标签

(实施例)。我的列表包含{" Android"," Studio"}如果用户点击Android,此文字应传递到说明标签并使用TextView显示文字。请帮我。在这里,我包括我各自的代码..

NormalActivity - > ListActi.java

{
    "ACL": "", 
    "AccessControlPolicy": {
        "Grants": [
            {
                "Grantee": {
                    "DisplayName": "", 
                    "EmailAddress": "", 
                    "ID": "", 
                    "Type": "", 
                    "URI": ""
                }, 
                "Permission": ""
            }
        ], 
        "Owner": {
            "DisplayName": "", 
            "ID": ""
        }
    }, 
    "Bucket": "", 
    "ContentMD5": "", 
    "GrantFullControl": "", 
    "GrantRead": "", 
    "GrantReadACP": "", 
    "GrantWrite": "", 
    "GrantWriteACP": ""
}

TabActivity - > BothLay

   @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);
                }
           }
        });
    }
}

Decription标签 - &gt; OneFragment (我需要的只是将数据从普通活动传递到此片段)

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

示例代码标签 - &gt; Twofragment

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;
    }
}

2 个答案:

答案 0 :(得分:4)

下面:

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

单击按钮时无效,因为当前正在启动BothLay活动,而不是将片段添加到FragmentManager

要让它发挥作用:

1。使用Intent将捆绑包传递给BothLay单击按钮时的活动:

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

2。onCreate BothLay活动中的Intent获取Bundle,然后在调用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
   }

修改

要在片段getArguments中的片段调用onCreateView方法中获取数据:

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

答案 1 :(得分:1)

执行我提供链接的所有步骤。

当您的片段恢复时,需要实施第4步。

以下代码将出现在您的活动中,

  

SelectedBundle selectedBundle;

最后你将在selectedBundle上获得片段的实例。

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

  fragment.setData(String passedData);

就是这样。