使用ViewPager Library滑动带有视图页面滑块(图像+点)的选项卡

时间:2014-08-21 08:01:33

标签: android android-fragments android-viewpager

我正在尝试使用ViewPager(Images + Dots)编写Swipe Tabs,但我面临两个小问题,请阅读以下内容:

1)。在 TabPagerAdapter 类中的这一行return new TabFragmentA();获取:

Type mismatch: cannot convert from TabFragmentA to Fragment

2)。在 TabFragmentA 类中的此行new ImagesFragmentAdapter(getSupportFragmentManager());获取:

The method getSupportFragmentManager() is undefined for the type TabFragmentA

我正在使用AppCompat库

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~

public class MainActivity extends FragmentActivity {
    ViewPager Tab;
    TabPagerAdapter TabAdapter;
    ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TabAdapter = new TabPagerAdapter(getSupportFragmentManager());

        Tab = (ViewPager)findViewById(R.id.pager);
        Tab.setOnPageChangeListener(
                new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {

                        actionBar = getActionBar();
                        actionBar.setSelectedNavigationItem(position);                    }
                });
        Tab.setAdapter(TabAdapter);

        actionBar = getActionBar();
        //Enable Tabs on Action Bar
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.TabListener tabListener = new ActionBar.TabListener(){

            @Override
            public void onTabReselected(android.app.ActionBar.Tab tab,
                    FragmentTransaction ft) {
                // TODO Auto-generated method stub

            }

            @Override
             public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

                Tab.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(android.app.ActionBar.Tab tab,
                    FragmentTransaction ft) {
                // TODO Auto-generated method stub

            }};
            //Add New Tab
            actionBar.addTab(actionBar.newTab().setText("Tab - A").setTabListener(tabListener));
            actionBar.addTab(actionBar.newTab().setText("Tab - B").setTabListener(tabListener));
            actionBar.addTab(actionBar.newTab().setText("Tab - C").setTabListener(tabListener));
    }

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~

public class TabPagerAdapter extends FragmentStatePagerAdapter {
    public TabPagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int i) {
        switch (i) {
        case 0:
            //Fragement for Android Tab
            return new TabFragmentA();
        case 1:
           //Fragment for Ios Tab
            return new TabFragmentB();
        case 2:
            //Fragment for Windows Tab
            return new TabFragmentC();
        }
        return null;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 3; //No of Tabs
    }

}

获得:

Type mismatch: cannot convert from TabFragmentA to Fragment

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~

public abstract class BaseImagesFragment extends Fragment {
    private static final Random RANDOM = new Random();

    TestFragmentAdapter mAdapter;
    ViewPager mPager;
    PageIndicator mIndicator;

    public boolean onCreateOptionsMenu(Menu menu)
    {
        getActivity().getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.random:
                final int page = RANDOM.nextInt(mAdapter.getCount());
                Toast.makeText(getActivity(), "Changing to page " + page, Toast.LENGTH_SHORT);
                mPager.setCurrentItem(page);
                return true;

            case R.id.add_page:
                if (mAdapter.getCount() < 10) {
                    mAdapter.setCount(mAdapter.getCount() + 1);
                    mIndicator.notifyDataSetChanged();
                }
                return true;

            case R.id.remove_page:
                if (mAdapter.getCount() > 1) {
                    mAdapter.setCount(mAdapter.getCount() - 1);
                    mIndicator.notifyDataSetChanged();
                }
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~

public class TabFragmentA extends BaseImagesFragment {

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

         View android = inflater.inflate(R.layout.android_frag, container, false);

            mAdapter = new TestFragmentAdapter(getSupportFragmentManager());

            mPager = (ViewPager)android.findViewById(R.id.pager);
            mPager.setAdapter(mAdapter);

            mIndicator = (CirclePageIndicator)android.findViewById(R.id.indicator);
            mIndicator.setViewPager(mPager);

            return android;
    }
}

获取:

The method getSupportFragmentManager() is undefined for the type TabFragmentA

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~

public abstract class ImagesFragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter {

    private int[] Images = new int[] { 
            R.drawable.flower_01, R.drawable.flower_02,
            R.drawable.flower_03, R.drawable.flower_04  };   

    private int mCount = Images.length;

    public ImagesFragmentAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return null;
    }

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

    public void setCount(int count) {
        if (count > 0 && count <= 10) {
            mCount = count;
            notifyDataSetChanged();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个:

1)来自SupportLibrary(appcompat项目)的getSupportFragmentManager()中提供了android.support.v4.app.FragmentActivity,该文件位于android-root\sdk\extras\android\support\v7\appcompat。如果它在那里不可用,那么可以从Android SDK Manager轻松下载 - “Extras / Android Support Library”)

2)您正在扩展错误的Fragment类。导入android.support.v4.app.Fragment而不是android.app.Fragment

答案 1 :(得分:0)

更改TabFragmentA

   mAdapter = new TestFragmentAdapter(getSupportFragmentManager());

mAdapter = new TestFragmentAdapter( getChildFragmentManager());