我如何通过FragmentPagerAdapter一个片段传递变量?变量、片段、FragmentPagerAdapter

2023-09-06 01:11:38 作者:与君共聚梦一场

我是一个机器人的初学者,努力学习,这是我的第一个问题,所以请原谅我,如果这个问题过于简单,请告诉我,如果我使用的是论坛不正确。

我使用的是布局,其中包括一个viewpager一个FragmentActivity;的活动创建MyFragmentPagerAdapter,它创建MyFragment的几个实例的一个实例。所有的support.v4版本。

我试图找到一种方法来传递的整数(重presenting选择在用户中取得previously,这实际上可能是一个绘制ID)的片段。我使用的意图传递 这个活动,我知道我可以可以使用一个包从FragmentPagerAdapter的片段传递的价值,但我不能找到一种方法,从活动到FragmentPagerAdapter得到它。我曾尝试多种方法,包括改变构造,但不能得到那个工作。

我失去了一些东西简单?什么是做到这一点的最好方法是什么?

活动

 公共类SecondActivity扩展FragmentActivity {
      私人MyIntegerAdapter1 mAdapter1;
      私人ViewPager mPager1;
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.second_layout);
        mAdapter1 =新MyIntegerAdapter1(getSupportFragmentManager());
        mPager1 =(ViewPager)findViewById(R.id.pager1);
        mPager1.setAdapter(mAdapter1);
 

FragmentPagerAdapter

 公共类MyIntegerAdapter1扩展FragmentPagerAdapter {
    公共MyIntegerAdapter1(FragmentManager FM){
        超(FM);
    }
    @覆盖
    公共片段的getItem(INT位置){
        开关(位置){
        情况下0:
            片段F1 =新IntegerFragment();
            捆绑args1 =新包();
            args1.putInt(参数,R.drawable.image1);
            args1.putInt(号码,1);
            f1.setArguments(args1);
            回到F1;
    等等
 

片段

 公共类IntegerFragment扩展片段{
      私人诠释imageResourceId;
      私人诠释numberSelected;
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        //改变,以避免撞车的方向
        imageResourceId = getArguments()调用getInt(参数)。
        numberSelected = getArguments()调用getInt(数字)。
 
这些狗血的男女故事比 前任3 真实100倍

解决方案

问题了。我发现了(从下面的链接)的片段如何调用其活性的方法。该方法被提供的值。我不知道这是最好的方式,但它的简单和它的作品。

Passing片段及其容器活动

之间的数据

I'm an android beginner, trying to learn, and this is my first question, so please excuse me if the question is too simple and please tell me if I'm using the forum incorrectly.

I have a FragmentActivity using a layout that includes a viewpager; the activity creates an instance of MyFragmentPagerAdapter, which creates several instances of MyFragment. All are support.v4 versions.

I'm trying to find a way to pass an integer value (representing a selection the user has made previously, which could actually be a drawable id) to the fragment. I'm using an intent to pass it to this activity and I know I can can use a bundle to pass a value from the FragmentPagerAdapter to the fragment, but I can't find a way to get it from the activity to the FragmentPagerAdapter. I have tried several ways including changing the constructor but couldn't get that to work.

Am I missing something simple? What is the best way to do this?

Activity

    public class SecondActivity extends FragmentActivity {
      private MyIntegerAdapter1 mAdapter1;
      private ViewPager mPager1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);
        mAdapter1 = new MyIntegerAdapter1(getSupportFragmentManager());
        mPager1 = (ViewPager) findViewById(R.id.pager1);
        mPager1.setAdapter(mAdapter1);

FragmentPagerAdapter

    public class MyIntegerAdapter1 extends FragmentPagerAdapter {
    public MyIntegerAdapter1(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
            Fragment f1 = new IntegerFragment();
            Bundle args1 = new Bundle();
            args1.putInt("param", R.drawable.image1);
            args1.putInt("number", 1);
            f1.setArguments(args1);
            return f1;
    etc

Fragment

    public class IntegerFragment extends Fragment {
      private int imageResourceId;
      private int numberSelected;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //change to avoid orientation crash
        imageResourceId = getArguments().getInt("param");
        numberSelected = getArguments().getInt("number");

解决方案

Problem over. I found out (from the link below) how a fragment can call a method in its activity. That method is providing the value. I'm not sure it's the best way but it's simple and it works.

Passing data between a fragment and its container activity