入力チェック有りのViewPager(1)の続き、
Activity のサンプル
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
/**
* SwipeSample2Activity
*/
public class SwipeSample2Activity extends Activity implements IActionMenuController{
ViewPager mPager;
private PagerAdapter mPagerAdapter;
List<Fragment> pagelist;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pagelist = new ArrayList<Fragment>();
Bundle args = new Bundle();
pagelist.add(Entry1Page.create(args));
pagelist.add(Entry2Page.create(args));
pagelist.add(Entry3Page.create(args));
pagelist.add(FinishPage.create(args));
mPager = (ViewPager)findViewById(R.id.pager);
mPagerAdapter = new FragmentStatePagerAdapter(getFragmentManager()){
@Override
public Fragment getItem(int position){
return pagelist.get(position);
}
@Override
public int getCount(){
return pagelist.size();
}
};
mPager.setAdapter(mPagerAdapter);
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
@Override
public void onPageSelected(int position){
invalidateOptionsMenu();
switch(position){
case 0 :
setNextEnable( *1.isValidate() );
break;
case 1 :
if (((Entry1Page)pagelist.get(0)).isValidate()==false){
mPager.arrowScroll(View.FOCUS_LEFT);
}
setNextEnable( *2.isValidate() );
break;
case 2 :
if (((Entry2Page)pagelist.get(1)).isValidate()==false){
mPager.arrowScroll(View.FOCUS_LEFT);
}
setNextEnable( *3.isValidate() );
break;
case 3 :
if (((Entry3Page)pagelist.get(2)).isValidate()==false){
mPager.arrowScroll(View.FOCUS_LEFT);
}
FinishPage finishPage = (FinishPage)pagelist.get(3);
setNextEnable(finishPage.isValidate());
finishPage.notifyView();
break;
default:
break;
}
}
});
}
public Fragment getPage(int position){
return pagelist.get(position);
}
private static Menu _menu;
@Override
public boolean onCreateOptionsMenu(Menu menu){
_menu = menu;
getMenuInflater().inflate(R.menu.activity_swipe_sample2, menu);
// previous action
menu.findItem(R.id.action_previous).setEnabled(mPager.getCurrentItem() > 0);
// next action
menu.findItem(R.id.action_next).setEnabled(false);
return true;
}
@Override
public void setNextEnable(boolean enable){
_menu.findItem(R.id.action_next).setEnabled(enable);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.action_previous :
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
return true;
case R.id.action_next :
mPager.setCurrentItem(mPager.getCurrentItem() + 1);
return true;
default:
break;
}
return true;
}
/*
* Prev と Next
*/
public void preview(){
if (mPager.getCurrentItem() > 0){
mPager.arrowScroll(View.FOCUS_LEFT);
}
}
public void nextview(){
if (mPager.getCurrentItem() < (mPagerAdapter.getCount() - 1)){
mPager.arrowScroll(View.FOCUS_RIGHT);
}
}
}
アクション Menu の定義 activity_swipe_sample2.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_previous" android:title="@string/action_previous"
android:showAsAction="ifRoom|withText"></item>
<item android:id="@+id/action_next" android:title="@string/action_next"
android:showAsAction="ifRoom|withText"></item>
</menu>
strings.xml で必要な文字列定義
<string name="action_previous">Prev</string>
<string name="action_next">Next</string>
Manifest で必要な Activity の定義
android:configChanges="orientation|screenSize"
android:uiOptions="splitActionBarWhenNarrow"
画面、、、