-1
多次
我使用我的活動與我附上一些碎片進入,當第一個片段連接成的片段稱爲多片段呼籲ViewPager
我簡單的代碼來測試簡單ViewPager
:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
private static final int NUM_PAGES = 3;
private ViewPager pager;
private PagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate Pager & Adapter
pager = (ViewPager) findViewById(R.id.viewpager);
adapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
}
@Override
public void onBackPressed() {
if(pager.getCurrentItem() == 0) {
super.onBackPressed();
}
else {
pager.setCurrentItem(pager.getCurrentItem() -1);
}
}
// PagerAdapter SubClass
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment;
switch(position) {
case 0:
fragment = new TutorialPage1();
break;
case 1:
fragment = new TutorialPage1();
break;
case 2:
fragment = new TutorialPage1();
break;
default:
fragment = new TutorialPage1();
break;
}
return fragment;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public int getCount() {
return NUM_PAGES;
}
} /* EOC */
public static class TutorialPage1 extends Fragment {
private static int pageIndex;
private static final String TAG = "TAG";
public TutorialPage1() {
}
public void TutorialPage1(int id) {
pageIndex = id;
Log.v(TAG, "Page Index is " + pageIndex); // this logs twice
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.e(TAG, "Res Index is ");
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.myfragment_layout, container, false);
Toast.makeText(getActivity(), "----->", Toast.LENGTH_SHORT).show();
return rootView;
}
}
} /* EOC */
是的,它只是測試,改成其他班後,問題不解決先生 –
哦,我是非常抱歉。請讓我再次測試 –