BottomNavigationView не свайпает при добавлении recycleView во фрагменты
У меня есть деятельность с BottomNavigationView
. В моемBottomNavigationView
, У меня есть 2 элемента, они смахивают правильно Но когда я добавляю свой RecycleView к фрагменту. они больше не смахивают. Просто когда я нажимаю на них, они работают.
Это моя деятельность:
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadFragment(new SongsFragment());
final BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(this);
findViewById(R.id.fragment_container).setOnTouchListener(new OnSwipeTouchListener(this) {
public void onSwipeRight() {
switch (getCheckedItem(navigation)) {
case 1:
loadFragment(new SongsFragment());
navigation.getMenu().getItem(0).setChecked(true);
break;
case 0:
loadFragment(new FavFragment());
navigation.getMenu().getItem(1).setChecked(true);
break;
};
}
public void onSwipeLeft() {
switch (getCheckedItem(navigation)) {
case 0:
loadFragment(new FavFragment());
navigation.getMenu().getItem(1).setChecked(true);
break;
case 1:
loadFragment(new SongsFragment());
navigation.getMenu().getItem(0).setChecked(true);
break;
};
}
});
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = new SongsFragment();
break;
case R.id.navigation_dashboard:
fragment = new FavFragment();
break;
}
return loadFragment(fragment);
}
private boolean loadFragment(Fragment fragment) {
//switching fragment
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
return true;
}
return false;
}
private int getCheckedItem(BottomNavigationView navigationView) {
Menu menu = navigationView.getMenu();
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
if (item.isChecked()) {
return i;
}
}
return -1;
}
}
и мой touchListner:
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener (Context ctx){
gestureDetector = new GestureDetector(ctx, new GestureListener());
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
result = true;
}
}
else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
result = true;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
а это мой макет:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back"
tools:context="com.nooshindroid.yastashir.activities.MainActivity">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/margin_main"
android:layout_marginLeft="@dimen/margin_main"
android:layout_marginRight="@dimen/margin_main"
android:background="@drawable/border1"
app:layout_constraintBottom_toTopOf="@id/navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
</FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:foregroundGravity="center"
android:layoutDirection="rtl"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@color/colorNavText"
app:itemTextColor="@color/colorNavText"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
когда я добавляю в свой фрагмент вид корзины, навигация не проводится.