前言:我对Android非常陌生。我环顾四周,阅读了大量的教程,我仍然没有把握这个概念。如果事情很简单,我会提前道歉。
我有一个活动正在使用Android Studio中模板的样板代码实现FragmentPagerAdapter。我的目标是在相同片段的各种实例之间切换以跟踪玩家得分(即,相同的布局,每个中的不同得分)。目前,我能够在看起来像是各种片段之间切换。我能够点击第一个片段上的按钮来计算分数,但是当我切换到下一个片段并单击一个按钮时,该片段中没有任何内容发生,但第一个片段的分数发生了变化。我认为这是因为它只使用一个片段而不是我需要的7个片段。如果你可以帮我弄清楚如何保存每个片段的分数,并且片段2上的按钮只会改变片段2上的分数。
这是我到目前为止所做的:
package com.brbecker.tabletopcompanion.Games;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import com.brbecker.tabletopcompanion.MainActivity;
import com.brbecker.tabletopcompanion.R;
public class SwipeActivity extends Activity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
// Supposed to be able to create multiple fragment instances and get at them again later.
private SwipeTestFragment player1, player2, player3, player4, player5, player6, player7;
@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);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_swipe, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
然后是FragmentPagerAdapter类
public class SectionsPagerAdapter extends FragmentPagerAdapter {
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 7 total pages.
return 7;
}
// This method is supposed to save a reference to a created Fragment.
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
switch (position) {
case 0:
player1 = (SwipeTestFragment) createdFragment;
break;
case 1:
player2 = (SwipeTestFragment) createdFragment;
break;
case 2:
player3 = (SwipeTestFragment) createdFragment;
break;
case 3:
player4 = (SwipeTestFragment) createdFragment;
break;
case 4:
player5 = (SwipeTestFragment) createdFragment;
break;
case 5:
player6 = (SwipeTestFragment) createdFragment;
break;
case 6:
player7 = (SwipeTestFragment) createdFragment;
break;
}
return createdFragment;
}
@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;
}
}
然后是片段代码:
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";
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;
}
@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);
String playerHeadingString = "Player " + getArguments().getInt(ARG_SECTION_NUMBER);
playerHeading.setText(playerHeadingString);
//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);
return rootView;
}
@Override
public void onClick(View v) {
TextView score = (TextView) getActivity().findViewById(R.id.scoreSevenWonders);
TextView coins = (TextView) getActivity().findViewById(R.id.coinsSevenWonders);
switch (v.getId()) {
case R.id.minusBtn1:
String presentvaluestring = coins.getText().toString();
if (Integer.parseInt(presentvaluestring) > 0) {
int presentvalueint = Integer.parseInt(presentvaluestring);
presentvalueint--;
coins.setText(String.valueOf(presentvalueint));
if (presentvalueint % 3 == 2) {
setPlayerScore(-1, score);
}
}
break;
case R.id.plusBtn1:
presentvaluestring = coins.getText().toString();
int presetvalueint = Integer.parseInt(presentvaluestring);
presentvalueint++;
if ((presentvalueint % 3) == 0) {
setPlayerScore(1, score);
}
coins.setText(String.valueOf(presentvalueint));
break;
}
}
public TextView setPlayerScore(int increment, TextView total) {
String totalstring = total.getText().toString();
int totalint = Integer.parseInt(totalstring); //see if Integer.parseInt(total.getText().toString()) will work
totalint += increment;
total.setText(String.valueOf(totalint));
return total;
}
}
我认为它与getItem()方法有关,但我不确定。如果有必要,我非常乐意提供额外的代码。
答案 0 :(得分:0)
SwipeActivity.java
package com.brbecker.tabletopcompanion.Games;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import com.brbecker.tabletopcompanion.MainActivity;
import com.brbecker.tabletopcompanion.R;
public class SwipeActivity extends Activity implements View.OnClickListener{
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
int[] scoresVal = new int[7];
int[] coinsVal = new int[7];
int current_pos = 0;
TextView score;
TextView coins;
@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);
score = (TextView) findViewById(R.id.scoreSevenWonders);
coins = (TextView) findViewById(R.id.coinsSevenWonders);
ImageButton minusone = (ImageButton) findViewById(R.id.minusBtn1);
minusone.setOnClickListener(this);
ImageButton plusone = (ImageButton) findViewById(R.id.plusBtn1);
plusone.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.minusBtn1:
int pos = viewpager.getCurrentItem();
presentvalueint = coinsVal[pos];
presentvalueint--;
if (presentvalueint % 3 == 2) {
score.setText(setPlayerScore(-1, pos));
}
coinsVal[pos] = presentvalueint;
coins.setText(String.valueOf(presentvalueint));
break;
case R.id.plusBtn1:
int pos = viewpager.getCurrentItem();
presentvalueint = coinsVal[pos];
presentvalueint++;
if (presentvalueint % 3 == 2) {
score.setText(setPlayerScore(-1, pos));
}
coinsVal[pos] = presentvalueint;
coins.setText(String.valueOf(presentvalueint));
break;
}
}
public String setPlayerScore(int increment, int pos) {
scoresVal[pos] += increment;
return String.valueOf(scoresVal[pos]);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_swipe, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
<强> SectionsPagerAdapter.java 强>
public class SectionsPagerAdapter extends FragmentPagerAdapter {
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 7 total pages.
return 7;
}
@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;
}
}
<强> SwipeTestFragment.java 强>
public class SwipeTestFragment extends Fragment{
// The fragment argument representing the section number for this fragment.
private static final String ARG_SECTION_NUMBER = "section_number";
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;
}
@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);
String playerHeadingString = "Player " + getArguments().getInt(ARG_SECTION_NUMBER);
playerHeading.setText(playerHeadingString);
return rootView;
}
}