0

So I have a recyclerview loaded with Firebase Firestore Documents and I want to see if it is beeing swiped up or down. Depending on the direction I want to make the adapter.startListening(); again and stop if he swipes down. I also want to hide a button if the user scrolls down. I just need to figure out how can I see if he does that ?

This piece of code was working with left and right but for some reason won´t with up and down ?


        new ItemTouchHelper( new ItemTouchHelper.SimpleCallback(0,
                ItemTouchHelper.UP | ItemTouchHelper.DOWN) {
            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                return false;
            }

            @Override
            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
                if(direction == ItemTouchHelper.UP){
                    Toast.makeText(getActivity(), "You swiped up", Toast.LENGTH_LONG).show();
                }
            }
        });

Here the complete code if it some reason interfers with smth don´t know

public class HomeFragment extends Fragment {

    View myFragment;
    private ImageButton postButton;
    CollectionReference postsRef;
    FirebaseFirestore db;
    private PostsAdapter adapter;
    FirebaseAuth mAuth;
    RecyclerView recyclerView;

    public static ProfileFragment getInstance() {
        return new ProfileFragment();
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myFragment = inflater.inflate(R.layout.fragment_home, container, false);
        postButton = myFragment.findViewById( R.id.postButton);
        recyclerView = myFragment.findViewById(R.id.postlist);
        mAuth = FirebaseAuth.getInstance();
        db = FirebaseFirestore.getInstance();
        postsRef = db.collection("posts");
        SetupRecyclerView();
        postButton.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SendUserToPostActivity();
            }
        });



        new ItemTouchHelper( new ItemTouchHelper.SimpleCallback(0,
                ItemTouchHelper.UP | ItemTouchHelper.DOWN) {
            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                return false;
            }

            @Override
            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
                if(direction == ItemTouchHelper.UP){
                    Toast.makeText(getActivity(), "You swiped up", Toast.LENGTH_LONG).show();
                }
            }
        });

        adapter.setOnConfirmClickListener( new PostsAdapter.onConfirmClickListener() {
            @Override
            public void onConfirmClick(DocumentSnapshot documentSnapshot, int position) {
                Posts post = documentSnapshot.toObject(Posts.class);
                String id = documentSnapshot.getId();
                if(post.getUid().equals(mAuth.getCurrentUser().getUid())){
                    adapter.deleteItem(position);
                    Toast.makeText(getActivity(), "Post deleted", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getActivity(), "You cannot delete this", Toast.LENGTH_LONG).show();
                }
            }
        });
        return myFragment;
    }

    @Override
    public void onStart(){
        super.onStart();
        adapter.startListening();
    }

    @Override
    public void onStop() {
        super.onStop();
        adapter.stopListening();
    }

    private void SetupRecyclerView() {
        Query query = postsRef.orderBy("time", Query.Direction.DESCENDING);
        FirestoreRecyclerOptions<Posts> options = new FirestoreRecyclerOptions.Builder<Posts>().setQuery(query, Posts.class).build();
        adapter = new PostsAdapter(options);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter);
    }


    private void SendUserToPostActivity() {
        Intent postIntent = new Intent(getActivity(), PostActivity.class);
        startActivity(postIntent);
    }
}
Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645
Lucas Goldner
  • 347
  • 2
  • 11

1 Answers1

1

You're using the wrong listener. An ItemTouchHelper applies to the individual items within the recyclerview, rather than the view as whole.

What you're looking for is OnScrollListener:

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //This is where you can track a scroll change
            }
        });

dy in the onScrolled method refers to the change in y position on a scroll event, likewise for dx.

In that method, let's say you wanted to check if the recyclerview reached the limit of scrolling either up or down, then within that onScroll method (or wherever you're checking that), you can add a call to recyclerView.canScrollVertically(int). The int will be a value of -1 for UP, 1 for DOWN, and 0 for FALSE, i.e. cannot scroll.

Checking if the recyclerview reached the bottom:

if(!recyclerView.canScrollVertically(1)){
    //The recyclerview can no longer scroll downwards, you have reached the bottom
}

Editing to add: Your itemTouchHelper also didn't work correctly because vertical directions are to be specified in the first parameter of the callback, and horizontal directions in the second parameter. You passed 0 for the first parameter, so it sees no vertical direction as an option

Amin
  • 600
  • 1
  • 4
  • 17
  • Yo thx a bunch helped me a lot but my only question is now how can I check if the last scroll was a downscroll ? – Lucas Goldner Jun 01 '20 at 13:38
  • 1
    No prob, and you can check the direction through the value of ```dy```: ```dy``` > 0 for scrolls down, ```dy``` < 0 for scrolls up – Amin Jun 01 '20 at 13:48