Java 使用ViewPager或FragmentPagerAdapter查找显示的片段
我有一个扩展ActionBarActivity并实现ActionBar.TabListener的主活动Java 使用ViewPager或FragmentPagerAdapter查找显示的片段,java,android,android-intent,android-fragments,Java,Android,Android Intent,Android Fragments,我有一个扩展ActionBarActivity并实现ActionBar.TabListener的主活动 public class MainActivity extends ActionBarActivity implements ActionBar.TabListener { SectionsPagerAdapter mSectionsPagerAdapter; ViewPager mViewPager; View mView; @Override protected void onCreate
public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
View mView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
}
但是,highScore值始终等于该值?
感谢您的回答。谢谢你容忍我。
-Paul T.您可以通过以下方式获得当前片段:
Fragment currentFragment = mSectionsPagerAdapter.getItem(mViewPager.getCurrentItem());
更新片段以获得分数:
Class CustomFragment extends Fragment{
private TextView nView;
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
nView = view.findViewById(R.id.label);
}
public String getScore(){
return nView.getText().toString();
}
}
函数将如下所示:
public void goToHighScore(View view) {
Intent intent = new Intent(this, DisplayScoreActivity.class);
Fragment currentFragment = mSectionsPagerAdapter.getItem(mViewPager.getCurrentItem());
String highScore = currentFragment.getScore();
intent.putExtra(HIGH_SCORE, highScore);
startActivity(intent);
}
但是,我建议您在片段中处理onClick事件,以提供更好的封装。按如下方式更新您的片段:
Class CustomFragment extends Fragment{
private TextView nView;
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
nView = view.findViewById(R.id.label);
Button button = (Button)findViewByid(R.id.button)
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String highScore = nView.getText().toString()
intent.putExtra(HIGH_SCORE, highScore);
startActivity(intent);
}
});
}
}
通过这样做,您的片段可以附加到不同的活动,而不会出现功能故障。currentFragment无法调用findViewById方法,只能调用视图。感谢您的封装建议;我正在尝试。
Class CustomFragment extends Fragment{
private TextView nView;
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
nView = view.findViewById(R.id.label);
Button button = (Button)findViewByid(R.id.button)
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String highScore = nView.getText().toString()
intent.putExtra(HIGH_SCORE, highScore);
startActivity(intent);
}
});
}
}