Używam domyślnego kodu Fragmentu karty Swipe, który uruchamia Android po uruchomieniu nowej aplikacji dla systemu Android. Zorientowałem się, jak modyfikować moje dane i korzystać z kart, ale nie mam pojęcia, jak dodać prosty fragment do tego, co już tam jest. Chciałem dodać pasek informacyjny na górze kart lub tuż pod nimi. Jestem pewien, że to proste, po prostu nie mogę zrozumieć, jak lol. Uczę się na przykładach.Jak mogę dodać fragment do ViewPagera? addView powoduje awarię mojej aplikacji
Próbowałem dodać widok do strony podglądu, ale moja aplikacja ulega awarii i uważam, że robię to źle
W moim główną działalność: ...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
SubBarFragment infobar = new SubBarFragment();
mViewPager.addView(infobar.getView());
.....[extra code for menus and things]
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
....[code to show specific tabs and return the tab's fragment]
infobarfragment.java
public class InfobarFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.infobar, container, false);
return view;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />
infobar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_wifi" />
<TextView
android:id="@+id/statusText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Wifi Status Information" />
</LinearLayout>
masz adapter dla ViewPager, użyj metody getItem/instanciateItem (w zależności od tego, z czego korzystasz: fragmentów lub prostych widoków). ta metoda daje obiektowi viewpager wszystkie potrzebne widoki. – Andreas
możesz zwrócić więcej niż jeden fragment w adapterze? Pomyślałem, jak dodać fragment za pomocą Santhosha, ale traktuję to jako kolejną "stronę", którą mogę przesuwać. Chcę, aby była ona trwała niezależnie od strony, na której się znajduję. Chciałem, żeby to było ponad zakładkami, ale nie sądzę, żeby to było możliwe. Więc myślę, że posiadanie go poniżej zakładek jest w porządku. Myśli? – CREW