我是编程新手,我正在基于BottomNavigationView创建一个应用程序,而我正在使用的BottomNavigation是BottomNavigationViewEx。我想在用户上下滑动时自动隐藏它。然后,当他向相反方向滚动时,再次显示它。
这是我设置了OnItemSelectListener的Java类
public class Welcome extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private Context mContext = Welcome.this;
private HomeFragment homeFragment;
private ChatsFragment chatsFragment;
private PostsFragment postsFragment;
private FavouritesFragment favouritesFragment;
private ProfileFragment profileFragment;
private Location lastLocation;
private LocationRequest locationRequest;
private GoogleApiClient googleApiClient;
private static final String TAG = "Welcome";
BottomNavigationViewEx bottomNav;
private List<ListItems> listItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
Log.d(TAG, "onCreate: starting");
checkLocationPermission();
setupBottomNavigationView();
enableNavigation();
HomeFragment homeFragment = new HomeFragment();
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.container, homeFragment).commit();
}
private void setupBottomNavigationView() {
Log.d(TAG, "setupBottomNavigationView: setting bottomnavigationview");
bottomNav = findViewById(R.id.nav_bottom);
BottomNavigationViewHelper.setUpNavigationView(bottomNav);
}
private void enableNavigation() {
bottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.btnHome:
if (homeFragment == null) homeFragment = new HomeFragment();
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.container, homeFragment).commit();
return true;
case R.id.btnChats:
if (chatsFragment == null) chatsFragment = new ChatsFragment();
android.support.v4.app.FragmentManager manager1 = getSupportFragmentManager();
manager1.beginTransaction().replace(R.id.container, chatsFragment).commit();
return true;
case R.id.btnPost:
if (postsFragment == null) postsFragment = new PostsFragment();
android.support.v4.app.FragmentManager manager2 = getSupportFragmentManager();
manager2.beginTransaction().replace(R.id.container, postsFragment).commit();
return true;
case R.id.btnFavourites:
if (favouritesFragment == null) favouritesFragment = new FavouritesFragment();
android.support.v4.app.FragmentManager manager3 = getSupportFragmentManager();
manager3.beginTransaction().replace(R.id.container, favouritesFragment).commit();
return true;
case R.id.btnProfile:
if (profileFragment == null) profileFragment = new ProfileFragment();
android.support.v4.app.FragmentManager manager4 = getSupportFragmentManager();
manager4.beginTransaction().replace(R.id.container, profileFragment).commit();
return true;
}
return false;
}
});
bottomNav.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() {
@Override
public void onNavigationItemReselected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.btnHome:
break;
case R.id.btnChats:
break;
case R.id.btnPost:
break;
case R.id.btnFavourites:
;
break;
case R.id.btnProfile:
break;
}
}
});
}
public void checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 98);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 98);
}
}
}
private void createLocationRequest() {
locationRequest = LocationRequest.create().setInterval(15000000).setFastestInterval(10000000).setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY).setSmallestDisplacement(0);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 98:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (checkPlayServices()) {
buildGoogleApiClient();
createLocationRequest();
}
}
}
}
protected synchronized void buildGoogleApiClient() {
googleApiClient = new GoogleApiClient.Builder(this).
addConnectionCallbacks(this).
addOnConnectionFailedListener(this).
addApi(LocationServices.API).build();
googleApiClient.connect();
}
private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode))
GooglePlayServicesUtil.getErrorDialog(resultCode, this, 97).show();
else {
Toast.makeText(this, "This Device Is Not Supported", Toast.LENGTH_SHORT).show();
finish();
}
return false;
}
return true;
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
这是我的BottomNavigationViewEx帮助程序类
public class BottomNavigationViewHelper {
private static final String TAG = "BottomNavigationViewHel";
public static void setUpNavigationView(BottomNavigationViewEx bottomNavigationViewEx){
Log.d(TAG, "setUpNavigationView: setting BottomNavigation");
bottomNavigationViewEx.enableAnimation(false);
bottomNavigationViewEx.enableItemShiftingMode(false);
bottomNavigationViewEx.enableShiftingMode(false);
bottomNavigationViewEx.setTextVisibility(false);
}
}
任何有经验的人请帮助我!