首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Textview没有显示在正确的片段上

Textview没有显示在正确的片段上
EN

Stack Overflow用户
提问于 2016-08-08 05:32:05
回答 1查看 56关注 0票数 0

我已经在我的应用程序中实现了avslide功能,它会显示一个箭头,告诉你可以向哪个方向滑动。我有5个可以滑动的片段,我不想在第一个片段上有左箭头,也不想在最后一个片段上有右箭头。然而,左箭头出现在第一个片段上,但在第二个片段上消失了,并且出于某种原因,它添加了6个片段,尽管我只需要5个。我的理论是,它认为第二个片段不知何故是第一个片段。我已经在处理箭头的代码中进行了注释。

任何帮助都是非常感谢的。

代码语言:javascript
复制
@Override
        public boolean onFling(MotionEvent event1, MotionEvent event2,
                               float velocityX, float velocityY) {


            if(event2.getX() < event1.getX()){
                if(currentFragment == 0){ //I HANDLE THE ARROWS HERE
                    leftarrow.setVisibility(View.INVISIBLE);
                } else if (currentFragment ==4){
                    rightarrow.setVisibility(View.INVISIBLE);
                }

                if(currentFragment == 0) {
                    removeFragment();
                    Bottomsection1 bottom1 = new Bottomsection1();
                    addDynamicFragment(bottom1);
                    currentFragment = 1;
                }else if(currentFragment ==1) {
                    removeFragment();
                    Bottomsection2 bottom2 = new Bottomsection2();
                    addDynamicFragment(bottom2);
                    currentFragment = 2;
                }else if(currentFragment ==2) {
                    removeFragment();
                    Bottomsection3 bottom3 = new Bottomsection3();
                    addDynamicFragment(bottom3);
                    currentFragment = 3;
                }else if(currentFragment ==3) {
                    removeFragment();
                    Bottomsection4 bottom4 = new Bottomsection4();
                    addDynamicFragment(bottom4);
                    currentFragment = 4;
                }
            }
            else{
                if(event2.getX() > event1.getX()) {
                    if(currentFragment==1) {
                        removeFragment();
                        Bottomsection bottom = new Bottomsection();
                        addDynamicFragment(bottom);
                        currentFragment = 0;
                    } else if(currentFragment==2) {
                        removeFragment();
                        Bottomsection1 bottom1 = new Bottomsection1();
                        addDynamicFragment(bottom1);
                        currentFragment = 1;
                    }else if(currentFragment==3) {
                        removeFragment();
                        Bottomsection2 bottom2 = new Bottomsection2();
                        addDynamicFragment(bottom2);
                        currentFragment = 2;
                    }else if(currentFragment==4) {
                        removeFragment();
                        Bottomsection3 bottom3 = new Bottomsection3();
                        addDynamicFragment(bottom3);
                        currentFragment = 3;
                    }
                }
            }
            return true;
        }

这是addDynamicFragment:

代码语言:javascript
复制
 private void addDynamicFragment(Fragment bottomsection) {
        FragmentManager FRAGMENTMANAGER=getFragmentManager();
        FragmentTransaction FRAGMENTTRANSACTION = FRAGMENTMANAGER.beginTransaction();

        activeCenterFragments.add(bottomsection);

        FRAGMENTTRANSACTION.add(R.id.Buttons, bottomsection);
        FRAGMENTTRANSACTION.commit();
    }

这是removeFragment方法:

代码语言:javascript
复制
 private void removeFragment()
    {
        FragmentManager FRAGMENTMANAGER=getFragmentManager();
        FragmentTransaction FRAGMENTTRANSACTION = FRAGMENTMANAGER.beginTransaction();

        if (activeCenterFragments.size() > 0) {
            FRAGMENTTRANSACTION = FRAGMENTMANAGER.beginTransaction();
            for (Fragment activeFragment : activeCenterFragments) {
                FRAGMENTTRANSACTION.remove(activeFragment);
            }
            activeCenterFragments.clear();
            FRAGMENTTRANSACTION.commit();
        }
EN

回答 1

Stack Overflow用户

发布于 2016-08-09 00:28:37

我调整了箭头条件,并将它们移出了各自的if语句。这应该可以解决箭头问题。

代码语言:javascript
复制
    Override
    public boolean onFling(MotionEvent event1, MotionEvent event2,
                           float velocityX, float velocityY) {

        if(currentFragment == 0){ //I HANDLE THE ARROWS HERE
                leftarrow.setVisibility(View.INVISIBLE);
                rightarrow.setVisibility(View.VISIBLE);
            } else if (currentFragment ==4){
                rightarrow.setVisibility(View.INVISIBLE);
                leftarrow.setVisibility(View.VISIBLE);
            } else {
                rightarrow.setVisibility(View.VISIBLE);
                leftarrow.setVisibility(View.VISIBLE);
            }

        if(event2.getX() < event1.getX()){


            if(currentFragment == 0) {
                removeFragment();
                Bottomsection1 bottom1 = new Bottomsection1();
                addDynamicFragment(bottom1);
                currentFragment = 1;
            }else if(currentFragment ==1) {
                removeFragment();
                Bottomsection2 bottom2 = new Bottomsection2();
                addDynamicFragment(bottom2);
                currentFragment = 2;
            }else if(currentFragment ==2) {
                removeFragment();
                Bottomsection3 bottom3 = new Bottomsection3();
                addDynamicFragment(bottom3);
                currentFragment = 3;
            }else if(currentFragment ==3) {
                removeFragment();
                Bottomsection4 bottom4 = new Bottomsection4();
                addDynamicFragment(bottom4);
                currentFragment = 4;
            }
        }
        else{
            if(event2.getX() > event1.getX()) {

                if(currentFragment==1) {
                    removeFragment();
                    Bottomsection bottom = new Bottomsection();
                    addDynamicFragment(bottom);
                    currentFragment = 0;
                } else if(currentFragment==2) {
                    removeFragment();
                    Bottomsection1 bottom1 = new Bottomsection1();
                    addDynamicFragment(bottom1);
                    currentFragment = 1;
                }else if(currentFragment==3) {
                    removeFragment();
                    Bottomsection2 bottom2 = new Bottomsection2();
                    addDynamicFragment(bottom2);
                    currentFragment = 2;
                }else if(currentFragment==4) {
                    removeFragment();
                    Bottomsection3 bottom3 = new Bottomsection3();
                    addDynamicFragment(bottom3);
                    currentFragment = 3;
                }
            }
        }
        return true;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38818772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档