Как заставить сервальные действия отображаться в Swipe View с помощью Intent?
Я пытался объединить действия сервалов в один Swipe View.
Я пробовал этот учебник: реализация смахивания горизонтального вида с помощью ViewPager и FragmentPagerAdapter в Android_
Как я могу сделать так, чтобы действие отображалось в разделе вместо того, чтобы показывать это textView (как в учебнике) с помощью Intent? (или любым другим способом). Спасибо!
1 ответ
На шагах 8. и 9. Вам нужно вернуть свой другой MyFragment 1.2.3 и т. Д. (Преобразованный из ваших действий) в метод getItem() MyFragmentPagerAdapter:
/** 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());
}
}
Примечание. Существует более быстрый способ реализации кода из этого руководства: убедитесь, что у вас установлены последние инструменты SDK и инструменты платформы из Android SDK Manager. На шаге 4. учебника, который вы попробовали, "Ввести сведения о MainActivity". Выберите тип навигации: Swipe Views + Title Strips и все готово.
Пример фрагмента с комментариями
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;
// }
}