我使用viewPager和fragment创建了一个应用程序。我希望如果应用程序有3个片段从3传递到1和1到3.我一直在阅读很多帖子但我没有在我的应用程序中设法做到。我能做什么?我把我的代码。谢谢。
MyFragment.java
public class MyFragment extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
public static final MyFragment newInstance(String message)
{
MyFragment f = new MyFragment();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String message = getArguments().getString(EXTRA_MESSAGE);
View v = inflater.inflate(R.layout.myfragment_layout, container, false);
TextView messageTextView = (TextView)v.findViewById(R.id.textView1);
messageTextView.setText(message);
cargar(v);
return v;
}
public void cargar(View v) {
final TabHost tabs;
tabs=(TabHost)v.findViewById(android.R.id.tabhost);
tabs.setup();
RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams
(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rllp.addRule(RelativeLayout.CENTER_IN_PARENT);
TabHost.TabSpec spec = tabs.newTabSpec("mitab1");
spec.setContent(R.id.tab1);
spec.setIndicator("TAB 1");
tabs.addTab(spec);
spec = tabs.newTabSpec("mitab2");
spec.setContent(R.id.tab2);
spec.setIndicator("TAB 2");
tabs.addTab(spec);
tabs.setCurrentTab(0);
tabs.getTabWidget().getChildAt(0).findViewById(android.R.id.title).setLayoutParams(rllp);
tabs.getTabWidget().getChildAt(1).findViewById(android.R.id.title).setLayoutParams(rllp);
TabWidget tw = (TabWidget)tabs.findViewById(android.R.id.tabs);
View tabView = tw.getChildTabViewAt(0);
final TextView tv = (TextView)tabView.findViewById(android.R.id.title);
tv.setTextSize(16);
tv.setTypeface(null, Typeface.BOLD);
View tabView2 = tw.getChildTabViewAt(1);
final TextView tv2 = (TextView)tabView2.findViewById(android.R.id.title);
tv2.setTextSize(16);
tv2.setTypeface(null, Typeface.BOLD);
tabs.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
if(tabId.equals("mitab1")) {
} else {
}
}
});
}
}
pageViewActivity.java
public class PageViewActivity extends FragmentActivity {
MyPageAdapter pageAdapter;
ViewPager pager;
private int previousState, currentState;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_view);
List<Fragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
}
public void onPageScrollStateChanged(int state) {
int currentPage = pager.getCurrentItem(); //ViewPager Type
if (currentPage == 3 || currentPage == 0){
previousState = currentState;
currentState = state;
if (previousState == 1 && currentState == 0){
pager.setCurrentItem(currentPage == 0 ? 3 : 0);
}
}
}
private List<Fragment> getFragments(){
List<Fragment> fList = new ArrayList<Fragment>();
for(int i=0;i<3;i++) {
fList.add(MyFragment.newInstance("Monumento " + (i+1)));
}
return fList;
}
private class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
}
答案 0 :(得分:1)
为此您需要获取2个虚拟片段,并且在onpagechangelistner中您需要检测页数是否为4然后只需调用setcurrentpage以1相同的方式为1到3查看此代码
public class MyPageChangeListener implements OnPageChangeListener {
@SuppressWarnings("static-access")
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixel) {
}
@Override
public void onPageSelected(int arg0) {
if (viewPager.getCurrentItem() == 4) {
viewPager.setCurrentItem(1, false);
}
if (viewPager.getCurrentItem() == 0) {
viewPager.setCurrentItem(3, false); // false will prevent sliding animation of view pager
}
}
}
答案 1 :(得分:1)
您可以使用pager.setCurrentItem(num)
功能设置当前页面。我认为这在您的情况下也很有用。pager.getCurrentItem()
函数用于获取当前页面。