我有一个与ViewPager绑定的FAB,当单击它时会打开一个片段并隐藏该FAB,但是当我在片段中水平滚动时,FAB会重新出现。因此,我决定通过调用addOnPageChangeListener处理ViewPager的滚动状态,并使用自定义ViewPager设置滚动状态(setSwipeEnabled)。这就是问题。要么我使片段不滚动,但片段上的ViewPager都不会退出,或者我不会重新出现按钮。我要达到的条件是(如果FAB新打开的片段为!null,则不滚动,否则滚动)。任何帮助表示赞赏!代码:
HomeActivity
mViewPager = findViewById(R.id.viewPager);
mViewPager.setSwipeEnabled(true);
...
mFAB = findViewById(R.id.mainFab);
mViewPager.addOnPageChangeListener(new CustomViewPager.OnPageChangeListener() {
Fragment fragment = null;
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(final int position) {
if(position == 2){
mFAB.hide();
}else{
mFAB.show();
}
switch (position){
case 0:
mFAB.setImageResource(R.drawable.ic_handshake_white_54dp);
mFAB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mActionBar.setTitle("New Package");
mFAB.hide();
fragment = new AddPackageFragment();
mFragmentManager.beginTransaction().add(R.id.contentFrame,fragment).addToBackStack("HomeFrag").commit();
//Condition I can't get to work
if(fragment != null){
mViewPager.setSwipeEnabled(false);
}else{
mViewPager.setSwipeEnabled(true);
}
}
});
break;
case 1:
mFAB.setImageResource(R.drawable.ic_mail_outline_24dp);
mFAB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mFAB.hide();
mActionBar.setTitle("New Message");
fragment = new NewMessageFragment();
mFragmentManager.beginTransaction().add(R.id.contentFrame,fragment).addToBackStack(null).commit();
}
});
break;
case 2:
mFAB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fragment = new DialogEditRequestFragment();
mFragmentManager.beginTransaction().replace(R.id.contentFrame,fragment).addToBackStack(null).commit();
}
});
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
//Animate fab
if(state == SCROLL_STATE_DRAGGING){
mFAB.hide();
}
if(state == SCROLL_STATE_SETTLING){
mFAB.show();
}
}
});
}
CustomViewPager
public class CustomViewPager extends ViewPager {
private boolean swipePageEnabled;
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.swipePageEnabled = false; // By default swiping is disabled
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return this.swipePageEnabled && super.onTouchEvent(event);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return this.swipePageEnabled && super.onInterceptTouchEvent(event);
}
@Override
public boolean executeKeyEvent(KeyEvent event) {
return this.swipePageEnabled && super.executeKeyEvent(event);
}
public void setSwipeEnabled(boolean enabled) {
this.swipePageEnabled = enabled;
}
public boolean isSwipeEnabled(){
return swipePageEnabled;
}
答案 0 :(得分:0)
所以我想通了!它所需要的只是一个布尔接口,该接口检查片段是否可见。听起来很简单,但有时您只需要睡觉。
HomeActivity
@Override
public void onFragmentVisible(boolean isVisible) {
if(!isVisible){
mViewPager.setSwipeEnabled(true);
mFAB.show();
}
}
片段
mCloseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mFragmentVisibilityListener.onFragmentVisible(false);
mActionBarListener.onActionBarListener(R.string.app_name);
getFragmentManager().popBackStack();
}
});
@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
mFragmentVisibilityListener = (OnFragmentInteractionListener) context;
mActionBarListener = (OnFragmentInteractionListener) context;
}catch (ClassCastException e){
throw new ClassCastException(context.toString() + "must implement OnFragmentInteraction Listener");
}
}
@Override
public void onDetach() {
super.onDetach();
mActionBarListener = null;
mFragmentVisibilityListener = null;
}
界面
public interface OnFragmentInteractionListener {
void onActionBarListener(int title);
void onFragmentVisible(boolean isVisible);
希望这对其他人有帮助!