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);
}
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;
}
实际上,您修改的是活动中包含的同一个TextView,而不是OnClick侦听器中的片段视图:
这里是:getActivity().findViewByID
TextView scoreTV = (TextView) getActivity().findViewById(R.id.scoreSevenWonders);
替代:FragmentView.FindViewByID
在我的应用程序中,我使用了一个活动和两个片段。该应用程序使用带有容器的布局,因此片段是通过事务添加的。第一个片段包含列表视图,另一个片段包含列表视图项的详细视图。两个片段都使用setRetainInstance(true)。片段是通过替换事务添加的,并设置了addToBackStack(null)。列表片段包含一个实例变量,其中包含列表的一些信息。现在我正在切换到详细并按回,实例变量为null。我
我有一个带选项卡指示器的viewPager。ViewPager是带有FragmentPagerAdapter的setAdaper。 我几乎不了解FragmentPagerAdapter的内部是如何工作的。我注意到邻居片段被恢复(调用OnResume),即使邻居还不可见。 我把更新方法放在OnResume中,以为片断是最新的,它就会更新。 广告横幅刷新 我希望页脚中设置的广告横幅在向左滑动一次或向右
我在使用FragmentPagerAdapter时遇到问题。我想从视图寻呼机中的片段中检索数据。当我调用getItem()方法时,它返回一个片段,但不创建片段。所有片段变量均为null。我需要做什么来实例化片段?这是我的适配器。 这是viewpager中的片段。 我不太了解ViewPager是如何工作的,但据我所知,片段在没有设置任何变量的情况下返回的原因是因为ViewPager没有附加片段。如何
我有一个带有fagmentpageradapter和viewpager的parentfragment。每个页面显示另一个包含imageview的片段。为了避免outofmemoryerrors,我只希望当前显示的片段在imageview中加载其图像。因此,我必须在当前显示的片段中加载imageview,并回收未显示片段中的图像。 父片段: 碎片PageRadapter: 分页器显示的片段: 在适配
我使用带有ViewPager的FragmentPagerAdapter来切换片段 那么我如何使片段调用它们的onResume()呢?