开发者

Changing Activity classes to Fragment (using FragmentPagerAdapter)

开发者 https://www.devze.com 2023-04-12 22:09 出处:网络
I have two activities which I want to combine with a FragmentPagerAdapter. I followed a tutorial and everything works great with the test classes. However, as mentioned I want to use two previously ma

I have two activities which I want to combine with a FragmentPagerAdapter. I followed a tutorial and everything works great with the test classes. However, as mentioned I want to use two previously made Activities in that horizontal slider.

At the moment it looks like this:

public class CheatPagerFragmentActivity extends FragmentActivity {
...
/**
 * Initialise the fragments to be paged
 */
private void initialisePaging() {

    List<Fragment> fragments = new Vector<Fragment>();
    fragments.add(Fragment.instantiate(this, CheatView.class.getName()));
    fragments.add(Fragment.instantiate(this, CheatMetaView.class.getName()));


    this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);

    ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager);
    pager.setAdapter(this.mPagerAdapter);
}
}

CheatView and CheatMetaView used to be Activities but now are Fragments ("extends Fragment"). I don't need to know how I can still pass information to those (new) Fragments as I can not use "intent" anymore how I used to pass information between Activities.

Also creating new elements in the code of these classes is being marked as an error now. Like this line of code:

TableRow trTh = new TableRow(this);

How can I pass information to those classes and how do I have to change the code 开发者_高级运维to create a new TableRow in the code of the former Activity class?

Thanks for any help.


Re your first question: Instead of using Fragment.instantiate, create a newInstance method for each fragment that can take the information you wish to pass as a parameter. See the page below for an example (search for newInstance):

http://developer.android.com/reference/android/app/Fragment.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号