FragmentPagerAdapter和同一片段的多个副本

时间:2016-07-10 19:33:55

标签: android android-fragments android-viewpager fragmentpageradapter

我有一个包含FragmentPagerAdapter的活动,可以在多个片段之间滑动(所有片段都使用相同的片段布局)。每个片段包含两个按钮a - 和一个+用于递增TextViewA中的数字。然后,TextViewA的值根据其值更改TextViewB的值。因此,单击加号按钮会增加TextViewA的数量,当它达到3时,它会增加TextViewB。

我遇到的问题是我的代码在Fragment1上完美运行,但是当我滑到Fragment2时,按钮会更改Fragment1&#39的TextViews而不是Fragment2的值。

以下是活动:

public class SwipeActivity extends Activity {
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_swipe);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
}

使用FragmentPagerAdapter的类:

public class SectionsPagerAdapter extends FragmentPagerAdapter {
    private static final int NUM_PLAYERS = 7;

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a SwipeTestFragment.
        return SwipeTestFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show NUM_PLAYERS total pages.
        return NUM_PLAYERS;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "PLAYER 1";
            case 1:
                return "PLAYER 2";
            case 2:
                return "PLAYER 3";
            case 3:
                return "PLAYER 4";
            case 4:
                return "PLAYER 5";
            case 5:
                return "PLAYER 6";
            case 6:
                return "PLAYER 7";
        }
        return null;
    }
}

最后,这是Fragment类

public class SwipeTestFragment extends Fragment implements View.OnClickListener {

// The fragment argument representing the section number for this fragment.
private static final String ARG_SECTION_NUMBER = "section_number";
private static final int NUM_PLAYERS = 7;
int playerNum;
int[] score = new int[NUM_PLAYERS];
int[] coins = new int[NUM_PLAYERS];

public SwipeTestFragment() {
}

// Returns a new instance of this fragment for the given section number.
public static SwipeTestFragment newInstance(int sectionNumber) {
    SwipeTestFragment fragment = new SwipeTestFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
    fragment.setArguments(args);
    return fragment;
}


//When creating, retrieve this instance's number from its arguments.
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    playerNum = getArguments() != null ? getArguments().getInt(ARG_SECTION_NUMBER) : 1;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    setRetainInstance(true);
    View rootView = inflater.inflate(R.layout.fragment_swipe, container, false);

    //Set Page Title
    TextView playerHeading = (TextView) rootView.findViewById(R.id.playerHeading);
    playerHeading.setText("Player " + (playerNum));

    //This section sets up buttons.
    ImageButton minusone = (ImageButton) rootView.findViewById(R.id.minusBtn1);
    minusone.setOnClickListener(this);
    ImageButton plusone = (ImageButton) rootView.findViewById(R.id.plusBtn1);
    plusone.setOnClickListener(this);

    //Populating data arrays with zeros
    score[playerNum] = 0;
    coins[playerNum] = 0;

    return rootView;
}

@Override
public void onClick(View v) {
    String presentvaluestring;
    int presentvalueint;
    int presentVal;

    TextView scoreTV = (TextView) getActivity().findViewById(R.id.scoreSevenWonders);
    TextView coinsTV = (TextView) getActivity().findViewById(R.id.coinsSevenWonders);

    switch (v.getId()) {
        case R.id.minusBtn1:
            presentVal = coins[playerNum];
            if (presentVal > 0) {
                presentVal--;
                coinsTV.setText(String.valueOf(presentVal));
                coins[playerNum] = presentVal;
                if (presentVal % 3 == 2) {
                    setPlayerScore(-1, scoreTV);
                }
            }

            break;
        case R.id.plusBtn1:
            presentVal = coins[playerNum];
            presentVal++;
            if ((presentVal % 3) == 0) {
                setPlayerScore(1, scoreTV);
            }
            coinsTV.setText(String.valueOf(presentVal));
            coins[playerNum] = presentVal;
            break;
    }

}

public TextView setPlayerScore(int increment, TextView total) {
    String totalstring = total.getText().toString();
    int totalint = Integer.parseInt(totalstring); 
    totalint += increment;
    total.setText(String.valueOf(totalint));
    return total;
}

1 个答案:

答案 0 :(得分:0)

实际上,您正在修改Activity中包含的相同TextView,而不是修改OnClick侦听器中的片段视图:

此处: getActivity()。findViewById

TextView scoreTV = (TextView) getActivity().findViewById(R.id.scoreSevenWonders);

而不是: fragmentView.findViewById

希望它有所帮助。