假设这是用户选择他想查看的页面的起始页面
lv.setOnItemClickListener(new OnItemClickListener(
) {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
SharedPreferences preferences = getSharedPreferences("myprefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("Position_content",position);
editor.commit();
Intent newpage = new Intent(getApplicationContext(),MainActivity.class);
startActivity(newpage);
}});
这是我的 Mainactivity.class
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);
pager.setAdapter(pagerAdapter);
sharedPref = getSharedPreferences("myprefs", Context.MODE_PRIVATE);
content_selection_page = sharedPref.getInt("Position_content",0);
SharedPreferences preferences = getSharedPreferences("myprefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("final_Position_content",content_selection_page);
editor.commit(); }
这是 MyFragmentAdapter.java
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
public static final String PREFS_NAME = "MyPrefsFile";
final int PAGE_COUNT = 52;
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm); }
@Override
public Fragment getItem(int arg0) {
MyFragment myFragment = new MyFragment();
Bundle data = new Bundle();
data.putInt("current_page", arg0+1);
myFragment.setArguments(data);
return myFragment;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
return "Page " + ( position + 1 );
}
}
这是 Fragment.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle data = getArguments();
//** Getting integer data of the key current_page from the bundle *//*
mCurrentPage = data.getInt("current_page", 0);
sharedPref = this.getActivity().getSharedPreferences("myprefs", Context.MODE_PRIVATE);
content_selection_page = sharedPref.getInt("final_Position_content",0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfragment_layout, container,false);
TextView sub = (TextView ) v.findViewById(R.id.sub1);
TextView sub1 = (TextView ) v.findViewById(R.id.sub2);
TextView hd = (TextView ) v.findViewById(R.id.head);
content_selection_page++; //doesnt work
switch(content_selection_page){
switch stmts.....
}
return v;
}
真正的问题现在的问题是用户通过列表选择一个选项,并且该位置已到达 fragment.java 中的开关盒,现在 viewpager 似乎不起作用。假设如果我单击列表项 4,它会将我带到第 3 页,当我滑动它时它会滑动,但它仍然在同一页面中,我该如何做到这一点,如果我滑动一次它必须每次都增加页面。 而不是内容 2 我不断获得内容 1