Android Wear:使用FragmentPagerAdapter的WearableActivity

时间:2015-10-13 10:21:03

标签: android android-fragments android-viewpager wear-os

使用WearableActivity时,您无法触及getSupportFragmentManager()所需的FragmentPagerAdapter,因此您需要使用getFragmentManager()

活动

public MyWearActivity extends WearableActivity {

    public ArrayList<Fragment> mFragments;
    public ArrayList<String> mTitles;
    public ViewPager mViewPager;
    public PageFragmentPagerAdapter mPagerAdapter;

    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.main);
        fragments = new ArrayList<>();
        mTitles = new ArrayList<>();
        mViewPager = (ViewPager) findViewById(R.id.viewpager);

        mPagerAdapter = new PageFragmentPagerAdapter(getFragmentManager()); <-- PROBLEM

        // More code
    }
}

寻呼机适配器

public class PageFragmentPagerAdapter extends FragmentPagerAdapter {

    public PageFragmentPagerAdapter(FragmentManager fm) {

        super(fm); <-- PROBLEM

    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Override
    public PageFragment getItem(int position) {
        return mFragments.get(position);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mTitles.get(position);
    }
}

如何在FragmentPagerAdapter内使用WearableActivity

1 个答案:

答案 0 :(得分:5)

刚遇到同样的问题。

我已添加

了解它
compile 'com.android.support:support-v13:23.1+'

到build.gradle文件中的依赖项。然后你在哪里使用FragmentPagerAdapter add

import android.support.v13.app.FragmentPagerAdapter;

不确定是否还有更好的方法。