سؤال

After some extensive research on the topic, I've reached an impasse. I'm trying to implement the ViewPager from Android's 'Using ViewPager for Screen Slides' tutorial to have different text on each page, like you are swiping through pages in a book. The way it's set up, they have two XML files (one that implements the ViewPager element in a Fragment Activity class, one that has a textview in a Fragment class) that shows Lorem Ipsum on every single page. I'm trying to find a way to change the textview in the Fragment class so that when I go to page 2, 3, 4, etc. there is different text on each page.

In the ScreenSlideActivity class, I've tried using a switch inside the onPageScrollStateChanged(int position) inner event handler, setting certain text views to visible/invisible. I've also tried using a switch inside the ScreenSlidePageFragment class in the onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) method. Is this the right approach or am I way off? I'm definitely overlooking something, could some please point me in the right direction?

This is a build targeting APIs 14 to 17, using Eclipse. Below please find some code from the two classes, as well as the XML file with the textview:

//Fragment Activity class //

public class ScreenSlideActivity extends FragmentActivity {
/**
 * The number of pages (wizard steps) to show in this demo.
 */
private static final int NUM_PAGES = 5;

/**
 * The pager widget, which handles animation and allows swiping horizontally to access previous
 * and next wizard steps.
 */
private ViewPager mPager;

/**
 * The pager adapter, which provides the pages to the view pager widget.
 */
private PagerAdapter mPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_screen_slide);
    getActionBar().setDisplayHomeAsUpEnabled(true);

 // Instantiate a ViewPager and a PagerAdapter.
    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
    mPager.setAdapter(mPagerAdapter);
    mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            // When changing pages, reset the action bar actions since they are dependent
            // on which page is currently active. An alternative approach is to have each
            // fragment expose actions itself (rather than the activity exposing actions),
            // but for simplicity, the activity provides the actions in this sample.

            invalidateOptionsMenu();
        }
    });
}

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        return ScreenSlidePageFragment.create(position);
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }
}

// Fragment class //

public class ScreenSlidePageFragment extends Fragment {
/**
 * The argument key for the page number this fragment represents.
 */
public static final String ARG_PAGE = "page";

/**
 * The fragment's page number, which is set to the argument value for {@link #ARG_PAGE}.
 */
private int mPageNumber;

/**
 * Factory method for this fragment class. Constructs a new fragment for the given page number.
 */
public static ScreenSlidePageFragment create(int pageNumber) {
    ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_PAGE, pageNumber);
    fragment.setArguments(args);
    return fragment;
}

public ScreenSlidePageFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPageNumber = getArguments().getInt(ARG_PAGE);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    ViewGroup rootView = (ViewGroup) inflater
            .inflate(R.layout.fragment_screen_slide_page, container, false);

    // Set the title view to show the page number.
    ((TextView) rootView.findViewById(android.R.id.text1)).setText(
            getString(R.string.title_template_step, mPageNumber + 1));

    return rootView;
}

/**
 * Returns the page number represented by this fragment object.
 */
public int getPageNumber() {
    return mPageNumber;
}

}

// XML layout //

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Dummy content. -->
<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView android:id="@android:id/text1"
        style="?android:textAppearanceLarge"
        android:textStyle="bold"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp" />

    <TextView
        android:id="@+id/lorem_ipsum1"
        style="?android:textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lineSpacingMultiplier="1.2"
        android:text="@string/lorem_ipsum" />

</LinearLayout>

هل كانت مفيدة؟

المحلول

Issue resolved, I had to create a switch statement inside the ScreenSlidePageFragment, turning off/on the visibility (VISIBILE or GONE) depending on the mPageNumber variable (i.e. the page number determined the string value set for that page). Is this a hack job, inefficient, or just fine? Bottom line, it works. All strings are saved in strings.xml. Again, not sure if this is good practice for larger strings.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    ViewGroup rootView = (ViewGroup) inflater
            .inflate(R.layout.fragment_screen_slide_page, container, false);

    // Set the title view to show the page number.
    ((TextView) rootView.findViewById(android.R.id.text1)).setText(
            getString(R.string.title_template_step, mPageNumber + 1));

    switch(mPageNumber){
        case 0:
            rootView.findViewById(R.id.lorem_ipsum1).setVisibility(View.VISIBLE);
            rootView.findViewById(R.id.lorem_ipsum2).setVisibility(View.GONE);
            rootView.findViewById(R.id.lorem_ipsum3).setVisibility(View.GONE);
            break;
        case 1:
            rootView.findViewById(R.id.lorem_ipsum1).setVisibility(View.GONE);
            rootView.findViewById(R.id.lorem_ipsum2).setVisibility(View.VISIBLE);
            rootView.findViewById(R.id.lorem_ipsum3).setVisibility(View.GONE);
            break;
        case 2:
          rootView.findViewById(R.id.lorem_ipsum1).setVisibility(View.GONE);
          rootView.findViewById(R.id.lorem_ipsum2).setVisibility(View.GONE);
          rootView.findViewById(R.id.lorem_ipsum3).setVisibility(View.VISIBLE);
          break;
        }

    return rootView;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top