其他分享
首页 > 其他分享> > android-使用ViewPager更改片段

android-使用ViewPager更改片段

作者:互联网

我正在将PagerSlidingTab库用于ViewPager.我想在滚动标签时更改“片段”.一切正常.查看我的代码.

我在每个片段上使用AsynTask().
当应用程序通过MainActivity打开时,第一个片段将附加到该活动,但是它显示两个AsynTask()对话框消息,一个来自第一个,另一个来自第二个片段.当我滚动到第二个选项卡时,它将显示“第三片段”的对话框消息.

因此,如果我在选项卡中从左向右滚动,则显示当前片段的右边的片段,如果我从右向左滚动,则显示当前片段的左边的片段.

请帮我解决问题.

我的代码:

public class PageSlidingTabStripFragment extends Fragment {

    public static final String TAG = PageSlidingTabStripFragment.class
            .getSimpleName();

    public static PageSlidingTabStripFragment newInstance() {
        return new PageSlidingTabStripFragment();
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.pager, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) view
                .findViewById(R.id.tabs);
        ViewPager pager = (ViewPager) view.findViewById(R.id.pager);

        MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager());

        pager.setAdapter(adapter);
        tabs.setViewPager(pager);
    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(android.support.v4.app.FragmentManager fm) {
            super(fm);
        }

        private final String[] TITLES = { "Instant Opportunity", "Events",
                "Experts" };

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
            case 0:
                return new InstantOpportunity();
            case 1:
                return new Events();
            case 2:
                return new Experts();
            default:
                break;
            }

            return null;
        }
    }
}

解决方法:

说明:

事实证明,不涉及另一个库的可滚动选项卡有一个更简单的实现.您可以直接从默认的SDK使用普通的Android代码轻松地在您的应用中实现标签页.

编码

主类:

public class PageSlidingTabStripFragment extends Fragment {

    //Variables
    private ViewPager viewPager;
    private PagerTitleStrip pagerTitleStrip;

    public PageSlidingTabStripFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);

        //Find your pager declared in XML
        viewPager = (ViewPager) getView().findViewById(R.id.pager);

        //Set the viewPager to a new adapter (see below)
        viewPager.setAdapter(new MyAdapter(getFragmentManager()));

        //If your doing scrollable tabs as opposed to fix tabs,
        //you need to find a pagerTitleStrip that is declared in XML
        //just like the pager
        pagerTitleStrip = (PagerTitleStrip)
            getView().findViewById(R.id.pager_title_strip);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.[your layout name here], container, false);
    }

}

适配器:

//Note: this can go below all of the previous code. Just make sure it's
//below the last curly bracket in your file!
class MyAdapter extends FragmentStatePagerAdapter {

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

    @Override
    public Fragment getItem(int arg0) {
        Fragment fragment = null;
        if (arg0 == 0) {
            fragment = new InstantOpportunity();
        }
        if (arg0 == 1) {
            fragment = new Events();
        }
        if (arg0 == 2) {
            fragment = new Experts();
        }
        return fragment;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        if (position == 0) {
            return "Instant Opportunity";
        }
        if (position == 1) {
            return "Events";
        }
        if (position == 2) {
            return "Experts";
        }
        return null;
    }
}

结论:

我希望这可以帮助您了解制作滚动标签的另一种方法!我在Github页面上有一些示例,说明如何制作每种类型(固定或可滚动).

链接:

>固定标签示例-Click Here
>滚动选项卡示例-Click Here

希望这可以帮助!

编辑:

当被问及要导入什么时,请确保选择了V4支持片段.

标签:pagerslidingtabstrip,android,android-fragments
来源: https://codeday.me/bug/20191013/1908824.html