本文介绍了添加来自不同片段整数在ViewPager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MainActivity 有3个片段一个ViewPager(弗拉加 FragB FragC)

在弗拉加,我宣布一个int a = 10;
在FragB,我宣布一个int B = 20;
在FragC,我有一个TextView和一个Button

现在,所有我想要做的是,当我点击FragC的按钮,它会从弗拉加和FragB添加int类型的和INT B和总和将显示在FragC

的TextView的

下面是我目前的code:

MainActivity.java

 公共类MainActivity扩展FragmentActivity {    ViewPager viewPager = NULL;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        viewPager =(ViewPager)findViewById(R.id.pager);
        FragmentManager fragmentManager = getSupportFragmentManager();
        viewPager.setAdapter(新MyAdapter(fragmentManager));
    }公共类MyAdapter扩展FragmentStatePagerAdapter {        公共MyAdapter(FragmentManager FM){
            超(FM);
        }        @覆盖
        公共片段的getItem(int i)以{
            片段片段= NULL;            如果(我== 0)
            {
                片段=新弗拉加();
            }
            如果(ⅰ== 1)
            {
                片段=新FragB();
            }
            如果(ⅰ== 2)
            {
                片段=新FragC();
            }
            返回片段;
        }        @覆盖
        公众诠释的getCount(){
            返回3;
        }
    }}

mainactivity.xml

 < android.support.v4.view.ViewPager的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID /寻呼机
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    工具:上下文=MainActivity。>< /android.support.v4.view.ViewPager>

弗拉加

 公共类弗拉加扩展片段{    INT A = 10;    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){        返回inflater.inflate(R.layout.fraga,集装箱,FALSE);
    }}

FragB

 公共类FragB扩展片段{        INT B = 20;        @覆盖
        公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                捆绑savedInstanceState){            返回inflater.inflate(R.layout.fragb,集装箱,FALSE);
        }    }

FragC

 公共类FragC扩展片段{    Button按钮;
    TextView中的TextView;    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){        视图V = inflater.inflate(R.layout.fragc,集装箱,FALSE);        的TextView =(TextView中)v.findViewById(R.id.textview);
        按钮=(按钮)v.findViewById(R.id.button);
        button.setOnClickListener(点击)        返回伏;
    }    OnClickListener点击=新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){        }
    };}


解决方案

使用您的活动来帮助您的片段沟通...例如:

添加一个getter方法​​返回的每个片段的整数值。添加公共字符串和()方法,你的活动,这将是这样的:

公共类MainActivity扩展FragmentActivity {

  ViewPager viewPager = NULL;
MyAdapter适配器= NULL;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    viewPager =(ViewPager)findViewById(R.id.pager);
    myAdapter =新MyAdapter(getSupportFragmentManager());
    viewPager.setAdapter(myAdapter);
}
公共字符串和(){
    返回Integer.toString(((弗拉加)myAdapter.getItem(0))调用getInt()+((FragB)myAdapter.getItem(1))调用getInt());
}公共类MyAdapter扩展FragmentStatePagerAdapter {
    公共MyAdapter(FragmentManager FM){
        超(FM);
    }    @覆盖
    公共片段的getItem(int i)以{
        片段片段= NULL;        如果(我== 0)
        {
            片段=新弗拉加();
        }
        如果(ⅰ== 1)
        {
            片段=新FragB();
        }
        如果(ⅰ== 2)
        {
            片段=新FragC();
        }
        返回片段;
    }    @覆盖
    公众诠释的getCount(){
        返回3;
    }
}

}

在中fragC(你的onClick()方法),所有你需要做的是文本值设置为这笔款项时,点击事件发生时 - >

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

我的括号可能有点过,但多数民众赞成的总体思路。

编辑:

 公共类弗拉加扩展片段{INT A = 10;公众诠释调用getInt(){
   返回;
}

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) {

        }
    };

}
解决方案

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

这篇关于添加来自不同片段整数在ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 08:19