FragmentPagerAdapter 和同一片段的多个副本

FragmentPagerAdapter and Multiple Copies of Same Fragment

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

我遇到的问题 运行 是我的代码在 Fragment1 上完美运行,但是当我滑动到 Fragment2 时,按钮更改了 Fragment1 的 TextView 而不是 Fragment2 的值。

这是activity:

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

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

最后,这是片段class

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

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

此处:getActivity().findViewById

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

而不是:fragmentView.findViewById

希望对您有所帮助。