2013-03-11 56 views
0

我有一个全屏活动,片段显示3个TextView,用户可以通过它浏览。问题出在操作栏上,在模拟器上工作正常,点击活动时显示系统UI,但使用设备时系统UI在单击时不显示。在全屏活动中有一个片段活动

随着SWIPE_MIN_DISTANCE的锻炼,我注意到onFling方法没有被使用,当我第一次只编写了它默认使用的刷卡片段时,但仍点击或单击时系统UI没有显示。所以我认为通过实施onFling方法我可以锻炼与滑动。

以下是代码。

public class FullscreenActivity extends FragmentActivity implements OnGestureListener { 

private static final int SWIPE_MIN_DISTANCE = 120; 
private static final int SWIPE_MAX_OFF_PATH = 250; 
private static final int SWIPE_THRESHOLD_VELOCITY = 200; 
private static final boolean AUTO_HIDE = true; 
private static final int AUTO_HIDE_DELAY_MILLIS = 3000; 
private static final boolean TOGGLE_ON_CLICK = true; 
private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION; 
private SystemUiHider mSystemUiHider; 
SectionsPagerAdapter mSectionsPagerAdapter; 
ViewPager mViewPager; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 


    setContentView(R.layout.activity_fullscreen); 

    final View controlsView = findViewById(R.id.fullscreen_content_controls); 
    final View contentView = findViewById(R.id.pager); 


    mSectionsPagerAdapter = new SectionsPagerAdapter(
      getSupportFragmentManager()); 

    mViewPager = (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 
    // Set up an instance of SystemUiHider to control the system UI for 
    // this activity. 
    mSystemUiHider = SystemUiHider.getInstance(this, contentView, 
      HIDER_FLAGS); 
    mSystemUiHider.setup(); 
    mSystemUiHider 
      .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() { 
       // Cached values. 
       int mControlsHeight; 
       int mShortAnimTime; 

       @Override 
       @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) 
       public void onVisibilityChange(boolean visible) { 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { 
         // If the ViewPropertyAnimator API is available 
         // (Honeycomb MR2 and later), use it to animate the 
         // in-layout UI controls at the bottom of the 
         // screen. 
         if (mControlsHeight == 0) { 
          mControlsHeight = controlsView.getHeight(); 
         } 
         if (mShortAnimTime == 0) { 
          mShortAnimTime = getResources().getInteger(
            android.R.integer.config_shortAnimTime); 
         } 
         controlsView 
           .animate() 
           .translationY(visible ? 0 : mControlsHeight) 
           .setDuration(mShortAnimTime); 
        } else { 
         // If the ViewPropertyAnimator APIs aren't 
         // available, simply show or hide the in-layout UI 
         // controls. 
         controlsView.setVisibility(visible ? View.VISIBLE 
           : View.GONE); 
        } 

        if (visible && AUTO_HIDE) { 
         // Schedule a hide(). 
         delayedHide(AUTO_HIDE_DELAY_MILLIS); 
        } 
       } 
      }); 

    // Set up the user interaction to manually show or hide the system UI. 
    contentView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (TOGGLE_ON_CLICK) { 
       mSystemUiHider.toggle(); 
      } else { 
       mSystemUiHider.show(); 
      } 
     } 
    }); 




    // Upon interacting with UI controls, delay any scheduled hide() 
    // operations to prevent the jarring behavior of controls going away 
    // while interacting with the UI. 
    findViewById(R.id.dummy_button).setOnTouchListener(
      mDelayHideTouchListener); 
} 



public class SectionsPagerAdapter extends FragmentPagerAdapter { 

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

    @Override 
    public Fragment getItem(int position) { 
     Fragment fragment = new DummySectionFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public int getCount() { 
     return 3; 
    } 


} 


public static class DummySectionFragment extends Fragment { 
    public static final String ARG_SECTION_NUMBER = "section_number"; 

    public DummySectionFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     TextView textView = new TextView(getActivity()); 

     textView.setGravity(Gravity.LEFT); 

     textView.setText(Integer.toString(getArguments().getInt(
       ARG_SECTION_NUMBER))); 
     return textView; 
    } 
} 








@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 

    // Trigger the initial hide() shortly after the activity has been 
    // created, to briefly hint to the user that UI controls 
    // are available. 
    delayedHide(100); 
} 


View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     if (AUTO_HIDE) { 
      delayedHide(AUTO_HIDE_DELAY_MILLIS); 
     } 
     return false; 
    } 
}; 

Handler mHideHandler = new Handler(); 
Runnable mHideRunnable = new Runnable() { 
    @Override 
    public void run() { 
     mSystemUiHider.hide(); 
    } 
}; 

private void delayedHide(int delayMillis) { 
    mHideHandler.removeCallbacks(mHideRunnable); 
    mHideHandler.postDelayed(mHideRunnable, delayMillis); 
} 


@Override 
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 
{ 
try { 
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) 
return false; 
// right to left swipe 
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
Toast.makeText(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show(); 
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show(); 
} 
else if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
Toast.makeText(getApplicationContext(), "Swipe up", Toast.LENGTH_SHORT).show(); 
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
Toast.makeText(getApplicationContext(), "Swipe down", Toast.LENGTH_SHORT).show(); 
} 
} catch (Exception e) { 
// nothing 
} 

return true; 
} 


@Override 
public boolean onDown(MotionEvent arg0) { 
    // TODO Auto-generated method stub 
    return false; 
} 


@Override 
public void onLongPress(MotionEvent e) { 
    // TODO Auto-generated method stub 

} 


@Override 
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 
     float distanceY) { 
    // TODO Auto-generated method stub 
    return false; 
} 


@Override 
public void onShowPress(MotionEvent e) { 
    // TODO Auto-generated method stub 

} 


@Override 
public boolean onSingleTapUp(MotionEvent e) { 
    // TODO Auto-generated method stub 
    return false; 
} 

回答

1

尝试以下方法,在你的onCreate()方法中添加以下行,

this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

以上线路必须经过super.onCreate(savedInstanceState);线

像下面,

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    ... 
}