在将recycleView添加到片段中时,未滑动BottomNavigationView

时间:2019-10-26 04:47:01

标签: java android android-recyclerview android-bottomnavigationview

我与import cv2 # load image as HSV and select saturation img = cv2.imread("finger.png") sat = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)[:,:,1] # threshold the saturation channel ret, thresh = cv2.threshold(sat,25,255,0) # apply morphology open to smooth the outline kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (19,19)) smoothed = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel) # write result to disk cv2.imwrite("finger_smoothed.png", smoothed) cv2.imshow("SAT", sat) cv2.imshow("THRESH", thresh) cv2.imshow("SMOOTHED", smoothed) cv2.waitKey(0) cv2.destroyAllWindows() 有一起活动。在我的BottomNavigationView中,我有2个项目,它们可以正确滑动,但是当我将RecycleView添加到片段中时。他们不再刷卡了。当我单击它们时,它们就会起作用。

这是我的活动:

BottomNavigationView

和我的touchListner:

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;
    }



}

这是我的布局:

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() {
    }

当我向片段添加回收视图时,导航不会滑动。

0 个答案:

没有答案