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

时间:2016-08-07 21:32:05

标签: android android-fragments textview

我在我的应用中实现了avslide功能,该功能显示一个箭头,告诉您可以滑动的方式。我可以在其间滑动5个片段,我不想在第一个片段上留下左箭头,也不想在最后一个片段上使用右箭头。然而,左箭头出现在第一个片段上,但在第二个片段上消失,由于某种原因,它增加了6个片段,即使我只需要5.我的理论是它认为第二个片段是第一个片段。我在处理箭头的代码中发表了评论。

非常感谢任何帮助。

@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:

 private void addDynamicFragment(Fragment bottomsection) {
        FragmentManager FRAGMENTMANAGER=getFragmentManager();
        FragmentTransaction FRAGMENTTRANSACTION = FRAGMENTMANAGER.beginTransaction();

        activeCenterFragments.add(bottomsection);

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

这是removeFragment方法:

 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();
        }

1 个答案:

答案 0 :(得分:0)

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

    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;
    }