Adding Integers from Different Fragments in a ViewPager

廉价感情. 提交于 2019-12-24 08:56:43

问题


I have a MainActivity that has a ViewPager with 3 fragments (FragA, FragB, FragC)

In FragA, I declared an int a = 10; In FragB, I declared an int b = 20; In FragC, I have a TextView and a Button

Now, all I want to do is that when I click the Button on the FragC, it will add the int a and int b from FragA and FragB and the sum will display on the TextView of the FragC

Here's my current code:

MainActivity.java

public class MainActivity extends FragmentActivity {

    ViewPager viewPager = null; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager)findViewById(R.id.pager);    
        FragmentManager fragmentManager = getSupportFragmentManager();
        viewPager.setAdapter(new MyAdapter(fragmentManager));
    }

public class MyAdapter extends FragmentStatePagerAdapter {  

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

        @Override
        public Fragment getItem(int i) {
            Fragment fragment = null;

            if (i == 0)
            {
                fragment = new FragA();
            }
            if (i == 1)
            {
                fragment = new FragB();
            }
            if (i == 2)
            {
                fragment = new FragC(); 
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 3;
        }   
    }

}

mainactivity.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

</android.support.v4.view.ViewPager>

FragA

public class FragA extends Fragment{

    int a = 10;

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

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

}

FragB

public class FragB extends Fragment{

        int b = 20;

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

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

    }

FragC

public class FragC extends Fragment{

    Button button;
    TextView textView;

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

        View v = inflater.inflate(R.layout.fragc, container, false); 

        textView = (TextView)v.findViewById(R.id.textview);
        button = (Button)v.findViewById(R.id.button);
        button.setOnClickListener(Click);

        return v;
    }

    OnClickListener Click = new OnClickListener() { 

        @Override
        public void onClick(View v) {

        }
    };

}

回答1:


Use your activity to help your fragments communicate...For example:

Add a getter method to return each fragment's integer value. Add a public String sum() method to your activity that would be something like:

public class MainActivity extends FragmentActivity {

ViewPager viewPager = null; 
MyAdapter adapter = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    viewPager = (ViewPager)findViewById(R.id.pager);    
    myAdapter = new MyAdapter(getSupportFragmentManager());
    viewPager.setAdapter(myAdapter);
}


public String sum(){
    return Integer.toString(((FragA)myAdapter.getItem(0)).getInt() + ((FragB)myAdapter.getItem(1)).getInt());
}

public class MyAdapter extends FragmentStatePagerAdapter {  
    public MyAdapter (FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = null;

        if (i == 0)
        {
            fragment = new FragA();
        }
        if (i == 1)
        {
            fragment = new FragB();
        }
        if (i == 2)
        {
            fragment = new FragC(); 
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return 3;
    }   
}

}

In your onClick() method within fragC(), all you need to do is set the text value to this sum when a click event occurs ->

textView.setText(((MainActivity)getActivity()).sum());

My parenthesis might be a bit off, but thats the general idea.

Edit:

public class FragA extends Fragment {

int a = 10;

public int getInt(){
   return a;
}



回答2:


Solved it. Thanks to Submersed

FragA

public class FragA extends Fragment{

    int a = 10;

    public int getInt() {

        return a;
    }
 ....

FragB

public class FragB extends Fragment{

        int b = 20;

        public int getInt() {

            return b;
        }
....

MainActivity

public String sum() {

    FragA FragA = new FragA();
    FragB FragB = new FragB();

    return Integer.toString(FragA.getInt() + FragB.getInt());
}

FragC

OnClickListener Click = new OnClickListener() { 

        @Override
        public void onClick(View v) {

            textView.setText(((MainActivity)getActivity()).sum());

        }
    };

Output on my textView when I click the "Add" button:



来源:https://stackoverflow.com/questions/20641889/adding-integers-from-different-fragments-in-a-viewpager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!