我试图让薮猫活动进入一个滑动视图。
我试过这个教程:在 Android 中使用 ViewPager 和 FragmentPagerAdapter 实现水平视图 滑动
如何使用 Intent 让活动显示在一个部分中,而不是显示该 textView(如教程中所示)?(或任何其他方式)。谢谢!
我试图让薮猫活动进入一个滑动视图。
我试过这个教程:在 Android 中使用 ViewPager 和 FragmentPagerAdapter 实现水平视图 滑动
如何使用 Intent 让活动显示在一个部分中,而不是显示该 textView(如教程中所示)?(或任何其他方式)。谢谢!
在第 8 步和第 9 步中,您需要做的是在 MyFragmentPagerAdapter 的 getItem() 方法中返回您不同的 MyFragment 1.2.3 等(从您的活动转换而来):
/** This method will be invoked when a page is requested to create */
@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;
//a switch returning different types Fragments
switch(position) {
case 0:
//Previous Activity 1, "converted to a Fragment":)
return (mMyFragment1 = new MyFragment1());
case 1:
//Previous Activity 2, "converted to a Fragment":)
return (mMyFragment2 = new MyFragment2());
}
}
注意:有一种更快的方法来实现该教程中的代码:确保您从 Android SDK 管理器中安装了最新的 SDK 工具和平台工具。在您尝试的教程的第 4 步中,“输入 MainActivity 详细信息”选择导航类型:滑动视图 + 标题条,一切顺利。
带有注释的示例片段
public class MyFragment extends Fragment {
//Empty Constructor
public MyFragment () {
}
//Return a new instance with FragmentArguments
public static MyFragment newInstance(Bundle args) {
MyFragment fragment = new MyFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Fragments Inflate the content view here,
//Activities use: setContentView(R.layout.activity_main);
return inflater.inflate(R.layout.main_activity, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//Fragments initialize views here (or you could do it in the onCreateView() method),
//Activities do it in the OnCreate() method
TextView textview = (TextView) view.findViewById(R.id.textView);
textview.setText(
"Implement all your old Activity methods here, " +
"but remember to use getActivity() instead of THIS. !!!" +
"A Fragment is a child of the parent Activity" +
"--One Activity to rule them all--"
);
////let the argument decide what you want to do. here it is the position in the FragmentPagerAdapter
// final int position;
// if (getArguments() != null) {
// position = getArguments().getInt(MyConfig.ADAPTER_POSITION_KEY);
// }
}//END onViewCreated()
// //Let the Activity implement a callback interface to communicate back.
// public interface Callbacks {
// public void onCallback(int adId);
// }
// private static Callbacks sDummyCallbacks = new Callbacks() {
// @Override
// public void onCallback(int adId) {
// }
// };
// private Callbacks mCallbacks = sDummyCallbacks;
// @Override
// public void onAttach(Activity activity) {
// super.onAttach(activity);
// if (!(activity instanceof Callbacks)) {
// throw new ClassCastException(
// "Activity must implement fragment's callbacks.");
// }
// mCallbacks = (Callbacks) activity;
// }
//
// @Override
// public void onDetach() {
// super.onDetach();
// mCallbacks = sDummyCallbacks;
// }
}