我使用FragmentPagerAdapter在单个活动中创建了三个片段。在第一个片段中,我使用按钮单击移动到第二个片段。首先调试它被触发到第3个片段然后只返回到第2个片段。
public class Main_Activity extends AppCompatActivity {
DrawerLayout mDrawerLayout;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
/**
*Setup the DrawerLayout and NavigationView
*/
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationView = (NavigationView) findViewById(R.id.shitstuff);
/**
* Lets inflate the very first fragment
* Here , we are inflating the TabFragment as the first Fragment
*/
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView, new TabFragment()).commit();
/**
* Setup click events on the Navigation View Items.
*/
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name,
R.string.app_name);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
}
public class HomeFragment extends Fragment {
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
ViewPager viewPager;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home_layout, container, false);
FragmentManager fm = getActivity().getSupportFragmentManager();
viewPager = (ViewPager) getActivity().findViewById(R.id.viewpager);
viewPager.setCurrentItem(0);
final Button bt_view = (Button) rootView.findViewById(R.id.BT_view);
bt_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(), BookList_Activity.class));
}
});
final Button bt_add = (Button) rootView.findViewById(R.id.BT_AddBook);
bt_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager = (ViewPager) getActivity().findViewById(R.id.viewpager);
viewPager.setCurrentItem(1);
}
});
return rootView;
}
}
public class SecondFragment extends Fragment implements View.OnClickListener {
Button bt_next;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.selectlocation_layout, container, false);
// Inflate the layout for this fragment
bt_next = (Button) rootView.findViewById(R.id.BT_continue);
// Implement OnClick,OnTouch and OnItemSelected Events
//OnItemSelected Listner
// OnClick Listner
bt_next.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.BT_continue:
viewPager = (ViewPager) getActivity().findViewById(R.id.viewpager);
viewPager.setCurrentItem(2);
break;
}
}
}
public class ThirdFragment extends Fragment implements View.OnClickListener, View.OnTouchListener {
查看rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.addbook_layout, container, false);
return rootView;
}
}
答案 0 :(得分:0)
在viewPager.setCurrentItem(0);
方法中设置onCreateView()
。