[英]Detecting up and down swipes on a Recyclerview in a fragment how can I do that?
So I have a recyclerview loaded with Firebase Firestore Documents and I want to see if it is beeing swiped up or down.所以我有一个加载了 Firebase Firestore 文档的 recyclerview,我想看看它是向上还是向下滑动。 Depending on the direction I want to make the adapter.startListening();
根据我要制作的方向 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这里是完整的代码,如果它出于某种原因干扰了 smth 不知道
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);
}
}
You're using the wrong listener.您使用了错误的侦听器。 An
ItemTouchHelper
applies to the individual items within the recyclerview, rather than the view as whole. ItemTouchHelper
适用于 recyclerview 中的单个项目,而不是整个视图。
What you're looking for is OnScrollListener
:您正在寻找的是
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
. onScrolled
方法中的dy
指的是滚动事件中 y position 的变化,对于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)
.在该方法中,假设您想检查 recyclerview 是否达到向上或向下滚动的限制,然后在该
onScroll
方法(或您正在检查的任何地方)中,您可以添加对recyclerView.canScrollVertically(int)
的调用. The int
will be a value of -1 for UP, 1 for DOWN, and 0 for FALSE, ie cannot scroll. int
的值为 -1 表示 UP,1 表示 DOWN,0 表示 FALSE,即不能滚动。
Checking if the recyclerview reached the bottom:检查recyclerview是否到达底部:
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.编辑添加:您的
itemTouchHelper
也无法正常工作,因为要在回调的第一个参数中指定垂直方向,而在第二个参数中指定水平方向。 You passed 0 for the first parameter, so it sees no vertical direction as an option您为第一个参数传递了 0,因此它认为没有垂直方向作为选项
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.