0

I have researched and tried to learn, but I am unclear and would appreciate help. I have played with a few different strategies, but cannot figure this out though this is really basic stuff.

I have a FragmentPagerAdapter that returns the tab name and the content text from a string. Those views or fragments--I'm so new I'm unclear on the terminology--are swipeable, which is great! (I followed numerous tutorials and examples.)

Now I want to add a ListView to scroll through the thirty or so fixed content texts that are displayed as users swipe.

Here, I think, is the relevant code so far:

public MyFragmentPagerAdapter(FragmentManager fm) {
    super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
    Log.v(TAG, "getPageTitle");

    String tabTitle = null;
    switch(position) {
        case 0:
            tabTitle = "FAQs";
            break;
        case 1:
            tabTitle = "Content";
            break;  return tabTitle;
}

/** 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;
}


/** Returns the number of pages */
@Override
public int getCount() {     
    return PAGE_COUNT;
}

}

and the ViewPager:

public class Content extends FragmentActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contentpage);

    /** Getting a reference to the ViewPager defined the layout file */        
    ViewPager pager = (ViewPager) findViewById(R.id.pager);

    /** Getting fragment manager */
    FragmentManager fm = getSupportFragmentManager();

    /** Instantiating FragmentPagerAdapter */
    MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);

    /** Setting the pagerAdapter to the pager object */
    pager.setAdapter(pagerAdapter);


}

}

and

public class MyFragment extends Fragment{

int mCurrentPage;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /** Getting the arguments to the Bundle object */
    Bundle data = getArguments();

    /** Getting integer data of the key current_page from the bundle */
    mCurrentPage = data.getInt("current_page", 0);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {     
    View v = inflater.inflate(R.layout.myfragment_layout, container,false);
    TextView tv = (TextView ) v.findViewById(R.id.tv);
    tv.setMovementMethod(new ScrollingMovementMethod());
    switch(mCurrentPage){
    case 0:
       tv.setText("  ");
    break;
    case 1:
        tv.setText(R.string.faqs);
    break;
    case 2:

        }
        return tv;

}

What I want to do is create a ListView that is a shortcut to the items returned in the content fragments.The ListView should display the tabTitles and when they are clicked, the user should be taken to the content page that is swipeable.

What do I put in ListView.java?

public class ListView   {

}

Thank you in advance for any help you can offer.

1 Answers1

0

From your question, it is not clear if you want your ListView to be onscreen with your ViewPager at the same time, or on a separate page. Here are the options I see:

  1. ListView uses entire screen, shows items -> click item in ListView -> takes you to a new screen and sets the ViewPager to the item you selected
  2. ViewPager takes up top half of screen, ListView takes up bottom half -> click item in ListView -> sets the ViewPager to the item you selected

As for your question, you don't need to create a custom ListView, just like you didn't need to create a custom ViewPager. ListViews work (like ViewPagers) with adapters that bind data (in your case pages of content) to them.

I would suggest that you create an array of all of your pages of content (say make a ContentPage object), and have your ListView and ViewPager bind to that ContentPage array via their adapters. This will require modifying your MyFragmentPagerAdapter along with other things.

However, since you are newer to these concepts, I recommend that you first do an example app using a ListView and learn how to create your own adapter or use a built in one. These examples should help cover those things:

http://www.vogella.com/articles/AndroidListView/article.html

Steven Byle
  • 13,149
  • 4
  • 45
  • 57
  • Thank you for the advice, Steven. You are very kind to try to help me. I'm going through the vogella.com tutorials--thorough as they may be, they are not always clear. Nonetheless, I must learn the basics--hacking things together only gets me so far. I agree about creating a ContentPage object. I'll work on that angle. Wish me luck... – user2005304 Jan 30 '13 at 20:54
  • Oh, and I wanted what you wrote in number 1, for the ListView to be a separate page that finds the desired content page in the context of the ViewPager -- like a table of contents in a book. I had hoped there would be an easy way to do that, to identify the cases that are switched in the ViewPager. – user2005304 Feb 01 '13 at 20:13
  • Yes that is a very common case. You can have your `ListView` live in its own `Activity`, and upon selection, start your `Content` (`FragmentActivity`) and pass the index of the selected item using a `Bundle`. See http://stackoverflow.com/questions/2965109/passing-data-between-activities-in-android. – Steven Byle Feb 01 '13 at 21:42
  • I'm still working on this, but I'm back at work and super busy with my own students. Thanks for the help so far. I'll post how I did it when I figure it out. – user2005304 Feb 06 '13 at 07:33
  • I have done the tutorials and I can make a ListView and bind it to some data, but I do not know how to make the ListView entries bind to the pages in the ViewPager. (Are these the fragments or is it the fragment that holds views)? If the user is presented with choosing the ViewPager or the ListView which connects to the ViewPager, do I need to start the ViewPager if the ListView is begun first? – user2005304 Feb 11 '13 at 06:23
  • Both your `ListView` and `Fragments` in your `ViewPager` will bind to the same set of data, not each other. The List and Pager know nothing of each other (and shouldn't - more reusable code that way). The only "binding" you need is the index of the selected item/page (or its unique id if you are keeping that in your page objects, and your lists are in different orders). If you selected page 3 (index 2) in your `ListView`, pass that index (via `Bundle`) to your `Activity` holding your `ViewPager` and have it set the current page to page 3 (index 2), `pager.setCurrentItem(index)`. – Steven Byle Feb 11 '13 at 15:47