Viewpager标签重现? [英] Viewpager tabs recreated?

查看:249
本文介绍了Viewpager标签重现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个 NavigationDrawer 在这几个项目。再$ P $每个项目psents不同的片段键,其中一个有标签。我的问题是,每次我打开这个片段标签重新加载!并加入到previous标签。这是怎么回事,我该如何解决呢?

这是片段的标签:

 公共类fragment_profilo_tabs扩展片段实现ActionBar.TabListener {    私人ViewPager viewPager;
    私人TabsPagerAdapter mAdapter;
    私人动作条动作条;
    //标签标题
    的String []选项卡= {Profilo公司aziendale,Credenziali};    / *(非Javadoc中)
     * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,android.view.ViewGroup,android.os.Bundle)
     * /
    @覆盖
    公共查看onCreateView(LayoutInflater气筒,
            @Nullable的ViewGroup容器,@Nullable捆绑savedInstanceState){
        // TODO自动生成方法存根        //动初始化
                查看查看= View.inflate(getActivity(),R.layout.profilo_tabs,NULL);                viewPager =(ViewPager)view.findViewById(R.id.pager);
                动作条= getActivity()getActionBar()。
                mAdapter =新TabsPagerAdapter(getFragmentManager());                viewPager.setAdapter(mAdapter);
                actionBar.setHomeButtonEnabled(假);
                actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);                //添加标签
                对于(字符串TAB_NAME:标签){
                    actionBar.addTab(actionBar.newTab()的setText(TAB_NAME)
                            .setTabListener(本));}        / **
         *上刷卡viewpager做出选择相应的标签
         * * /
        viewPager.setOnPageChangeListener(新ViewPager.OnPageChangeListener(){            @覆盖
            公共无效使用onPageSelected(INT位置){
                //上改变页面
                //使受人尊敬的选项卡中选择
                actionBar.setSelectedNavigationItem(位置);
            }            @覆盖
            公共无效onPageScrolled(INT为arg0,ARG1浮球,诠释ARG2){
            }            @覆盖
            公共无效onPageScrollStateChanged(INT为arg0){
            }
        });
        返回视图。    }    @覆盖
    公共无效onTabReselected(标签选项卡,FragmentTransaction英尺){
    }    @覆盖
    公共无效onTabSelected(标签选项卡,FragmentTransaction英尺){
        //在选项卡中选择
        //秀推崇片段视图
        viewPager.setCurrentItem(tab.getPosition());
    }    @覆盖
    公共无效onTabUnselected(标签选项卡,FragmentTransaction英尺){
    }
}

这是我的 ViewPagerAdapter

 公共类TabsPagerAdapter扩展FragmentPagerAdapter {    / *(非Javadoc中)
     * @see android.support.v4.view.PagerAdapter#getItemPosition(java.lang.Object)中的
     * /    @覆盖
    公众诠释getItemPosition(Object对象){
        // TODO自动生成方法存根
         返回POSITION_NONE;
    }    公共TabsPagerAdapter(FragmentManager FM){
        超(FM);
    }    @覆盖
    公共片段的getItem(INT指数){        开关(指数){
        情况下0:
            //顶级片段活动
            返回新fragment_profilo_modificaProfilo();
        情况1:
            //游戏片段活动
            返回新fragment_profilo_credenzialiAccesso();
        }
        返回null;
    }    @覆盖
    公众诠释的getCount(){
        //获取项目计数 - 等于标签数
        返回2;
    }}


解决方案

确定有多个问题。首先让我们来谈谈你的 ViewPagerAdapter

这引起了我的眼睛的第一件事情是这样的:

  @覆盖
公众诠释getItemPosition(Object对象){
    // TODO自动生成方法存根
     返回POSITION_NONE;
}

这是对于性能非常糟糕。我知道,在Stack Overflow上一些帖子建议使用这种修正了一些东西,但这不是要走的路。除去这一点,你不需要它。


但主要的问题是你的片段。首先这样的:

 为(字符串TAB_NAME:标签){
    actionBar.addTab(actionBar.newTab()
        .setText(TAB_NAME)
        .setTabListener(本));
}

您添加标签到动作条。和动作条不是你片段部分。每次显示该片段你同样的标签添加到动作条,但你永远不会再删除或任何东西。

我的建议是:不要做这个。打开一个新的活动的标签片段。这应该显然不是在同一个活动。我无法想象任何情况下突然加标签,以当前的活动将满足Android平台的指导方针或提供良好的可用性。


一般的规则是,有一切与活动做本身 - 像有关的东西动作条或标签在动作条 - 应该在活动来处理。 code这增加了标签到动作条有一个片段里没有立足之地。因此,无论永久添加标签在的onCreate()你的活动的。或创建一个新的活动与标签尤其是对 fragment_profilo_tabs

<子>顺便说一句:类名永远不应该 蛇案例 。以大写字母开头的类名和使用 驼峰 。一切只会混淆其他程序员看你的code

I have implemented a NavigationDrawer with a few items in it. Every item represents a different Fragment and one of them has tabs. My problem is that every time I open this Fragment the tabs reloaded! and added to the previous tabs. Why is this happening and how can I solve it?

This is the Fragment with the tabs:

public class fragment_profilo_tabs extends Fragment implements ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    String[] tabs = { "Profilo aziendale", "Credenziali" };

    /* (non-Javadoc)
     * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
     */
    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        // Initilization
                View view = View.inflate(getActivity(), R.layout.profilo_tabs, null);

                viewPager = (ViewPager) view.findViewById(R.id.pager);
                actionBar = getActivity().getActionBar();
                mAdapter = new TabsPagerAdapter(getFragmentManager());

                viewPager.setAdapter(mAdapter);
                actionBar.setHomeButtonEnabled(false);
                actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

                // Adding Tabs
                for (String tab_name : tabs) {
                    actionBar.addTab(actionBar.newTab().setText(tab_name)
                            .setTabListener(this));}

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
        return view;

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }
}

And this is my ViewPagerAdapter:

public class TabsPagerAdapter extends FragmentPagerAdapter {

    /* (non-Javadoc)
     * @see android.support.v4.view.PagerAdapter#getItemPosition(java.lang.Object)
     */

    @Override
    public int getItemPosition(Object object) {
        // TODO Auto-generated method stub
         return POSITION_NONE;
    }

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

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:
            // Top Rated fragment activity
            return new fragment_profilo_modificaProfilo();
        case 1:
            // Games fragment activity
            return new fragment_profilo_credenzialiAccesso();
        }
        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 2;
    }

}

解决方案

Ok there are multiple issues. First and foremost lets talk about your ViewPagerAdapter:

The first thing that caught my eye is this:

@Override
public int getItemPosition(Object object) {
    // TODO Auto-generated method stub
     return POSITION_NONE;
}

This is very bad for performance. I know that some posts on Stack Overflow suggest using this to "fix" a few things, but that is not the way to go. Remove this, you don't need it.


But the main issue is in your Fragment. First and foremost this:

for (String tab_name : tabs) {
    actionBar.addTab(actionBar.newTab()
        .setText(tab_name)
        .setTabListener(this));
}

You are adding tabs to the ActionBar. And the ActionBar is not part of you Fragment. Each time you display this Fragment you add the same tabs to the ActionBar but you never remove them again or anything.

My advice: Don't do this. Open a new Activity for the tabs Fragment. This should clearly not be in the same Activity. I cannot imagine any situation where suddenly adding tabs to the current Activity would satisfy the Android Platform Guidelines or provide good usability.


The general rule is that everything that has to do with the Activity itself - like things concerning the ActionBar or tabs in the ActionBar - should be handled in the Activity. Code which adds tabs to the ActionBar has no place inside a Fragment. So either add the tabs permanently in onCreate() of your Activity. Or create a new Activity with those tabs especially for the fragment_profilo_tabs.

As an aside: class names should never be Snake Case. Start class names with an uppercase letter and use Camel Case. Everything else will just confuse other programmers looking at your code

这篇关于Viewpager标签重现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆