我的头等课程:
enter code here public class VIEwPagerMainActivity extends FragmentActivity implements OnClickListener,OnPagechangelistener { // all variable declare here@Suppresslint("NewAPI")@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getwindow().setFlags( WindowManager.LayoutParams.FLAG_FulLSCREEN | WindowManager.LayoutParams.FLAG_disMISS_KEyguard,WindowManager.LayoutParams.FLAG_FulLSCREEN | WindowManager.LayoutParams.FLAG_disMISS_KEyguard ); settheme(androID.R.style.theme_light_NoTitlebar); setContentVIEw(R.layout.vIEw_pager_main); // other code here}private class MyPagerAdapter extends FragmentPagerAdapter { public MyPagerAdapter( androID.support.v4.app.FragmentManager fragmentManager) { super(fragmentManager); } @OverrIDe public Fragment getItem(int pos) { switch (pos) { case 0: return FirstFragment.newInstance(""); case 1: return SecondFragment.newInstance(""); default: return SecondFragment.newInstance("Default"); } } @OverrIDe public int getCount() { return 2; // return no of fragment created by us }}@OverrIDepublic voID onClick(VIEw v) {}@OverrIDepublic voID onPageScrollStateChanged(int arg0) { // Todo auto-generated method stub}@OverrIDepublic voID onPageScrolled(int arg0,float arg1,int arg2) { // Todo auto-generated method stub}@OverrIDepublic voID onPageSelected(int arg0) { // Todo auto-generated method stub}@OverrIDepublic voID onBackpressed() { // Todo auto-generated method stub}
}
enter code here SECOND file public class FirstFragment extends Fragment implements OntouchListener { // all variable decalre here@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) { getActivity().settheme(androID.R.style.theme_light_NoTitlebar); v = inflater.inflate(R.layout.slIDing_lock_pager,container,false); // other code here return v;}public static FirstFragment newInstance(String text) { FirstFragment f = new FirstFragment(); Bundle b = new Bundle(); b.putString("msg",text); f.setArguments(b); return f;}@OverrIDepublic boolean ontouch(VIEw v,MotionEvent event) { return false;}
}
第三档
在此处输入代码
公共类SecondFragment extends Fragment实现OnClickListener {
// all varible decalare here@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater,Bundle savedInstanceState) { getActivity().settheme(androID.R.style.theme_light_NoTitlebar); VIEw v = inflater.inflate(R.layout.password,false); // other code here // this method move to main pager txtBack.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { final androID.support.v4.app.FragmentTransaction ft = getFragmentManager() .beginTransaction(); ft.replace(R.ID.layputMainPager,new FirstFragment(),"NewFragmentTag"); ft.addToBackStack(null); ft.commit(); } }); return v;}public static SecondFragment newInstance(String text) { SecondFragment f = new SecondFragment(); Bundle b = new Bundle(); b.putString("msg",text); f.setArguments(b); return f;}@OverrIDepublic voID onClick(VIEw v) { // Todo auto-generated method stub}
}
See in secondFragment class when i back through back button then i want to scroll again to that page i not do that. i move again to that page though page scroll. and in ft.replace(R.ID.layputMainPager,new FirstFragment()) R.ID.layputMainPager is `class VIEwPagerMainActivity` class xml files a
主要布局ID.
解决方法 几天前我遇到了同样的问题,我认为你正在混合两种不同的实现:>如果您在VIEwPager中使用片段.您可以使用VIEwPager的setCurrentItem()方法移动到next或prev片段.
>如果你只使用片段然后在片段之间移动,你可以使用FragmentTransaction类方法,如replace(),add()等.
希望这会清除事情.如果仍然不清楚,请分享代码.
编辑
你可以重载onBackpressed()方法,例如:
@OverrIDepublic voID onBackpressed() { // you can also check for current vIEwpager item by // mVIEwpager.getCurrentItem() mVIEwPager.setCurrentItem(precFragment,true);}
如果您在片段中添加了button,则可以执行以下 *** 作:
button back = (button) rootVIEw.findVIEwByID(R.ID.back_button);back.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { ((YourActivity)getActivity()).mVIEwPager.setCurrentItem(prevFragment); } });
编辑:添加代码
public class MainActivity extends Activity {/** * The {@link androID.support.v4.vIEw.PagerAdapter} that will provIDe * fragments for each of the sections. We use a {@link FragmentPagerAdapter} * derivative,which will keep every loaded fragment in memory. If this * becomes too memory intensive,it may be best to switch to a * {@link androID.support.v13.app.FragmentStatePagerAdapter}. */SectionsPagerAdapter mSectionsPagerAdapter;/** * The {@link VIEwPager} that will host the section contents. */VIEwPager mVIEwPager;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); // 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.pager); mVIEwPager.setAdapter(mSectionsPagerAdapter);}@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main,menu); return true;}@OverrIDepublic voID onBackpressed() { mVIEwPager.setCurrentItem(0,true);}@OverrIDepublic boolean onoptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button,so long // as you specify a parent activity in AndroIDManifest.xml. int ID = item.getItemID(); if (ID == R.ID.action_settings) { return true; } return super.onoptionsItemSelected(item);}/** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @OverrIDe public Fragment getItem(int position) { switch (position) { case 0: return new FirstFragment(); case 1: return new SecondFragment(); default: break; } return null; } @OverrIDe public int getCount() { return 2; } @OverrIDe public CharSequence getPageTitle(int position) { Locale l = Locale.getDefault(); switch (position) { case 0: return getString(R.string.Title_section1).toupperCase(l); case 1: return getString(R.string.Title_section2).toupperCase(l); } return null; }} }
FirstFragment文件
public class FirstFragment extends Fragment {/** * The fragment argument representing the section number for this * fragment. */private static final String ARG_SECTION_NUMBER = "section_number";/** * Returns a new instance of this fragment for the given section number. */public static FirstFragment newInstance(int sectionNumber) { FirstFragment fragment = new FirstFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER,sectionNumber); fragment.setArguments(args); return fragment;}public FirstFragment() {}@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater,Bundle savedInstanceState) { VIEw rootVIEw = inflater.inflate(R.layout.fragment_first,false); button next = (button) rootVIEw.findVIEwByID(R.ID.button1); next.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { ((MainActivity)getActivity()).mVIEwPager.setCurrentItem(1); } }); return rootVIEw;}}
SecondFragment文件
public class SecondFragment extends Fragment {/** * The fragment argument representing the section number for this * fragment. */private static final String ARG_SECTION_NUMBER = "section_number";/** * Returns a new instance of this fragment for the given section number. */public static SecondFragment newInstance(int sectionNumber) { SecondFragment fragment = new SecondFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER,sectionNumber); fragment.setArguments(args); return fragment;}public SecondFragment() {}@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater,Bundle savedInstanceState) { VIEw rootVIEw = inflater.inflate(R.layout.second_fragment,false); button back = (button) rootVIEw.findVIEwByID(R.ID.button1); back.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { ((MainActivity)getActivity()).mVIEwPager.setCurrentItem(0); } }); return rootVIEw;} }
请参阅SecondFragment文件中的onBackpressed()方法和back.setonClickListener().希望这会有所帮助
总结以上是内存溢出为你收集整理的Android在视图寻呼机中点击转到第一个片段后返回相同的片段全部内容,希望文章能够帮你解决Android在视图寻呼机中点击转到第一个片段后返回相同的片段所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)