我已经在我的应用程序中实现了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();
}发布于 2016-08-09 00:28:37
我调整了箭头条件,并将它们移出了各自的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;
}https://stackoverflow.com/questions/38818772
复制相似问题