[英]Snackbar in recyclerview
how can I show the snackbar at the bottom of page?如何在页面底部显示小吃店? I want to show snackbar when clicked on like.
我想在点击喜欢时显示小吃店。 I used snackbar in an item of recycleview (crdLayout) so It has been shown at bottom of each item here is the code:
我在recycleview(crdLayout)的一个项目中使用了snackbar,所以它已经显示在每个项目的底部,这里是代码:
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
itemHolder.like.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar snackbar = Snackbar
.make(itemHolder.crdLayout, "liked", Snackbar.LENGTH_LONG)
snackbar1.show();
}
});
the snackbar each time dont pop up at the bottom of page and instead it comes at bottom of each cardview小吃栏每次都不会在页面底部弹出,而是出现在每个卡片视图的底部
In your Snackbar
, you are giving the parameter as itemholder.cardLayout
.在您的
Snackbar
,您将参数指定为itemholder.cardLayout
。 So the snackbar will appear below the card layout.因此,小吃栏将出现在卡片布局下方。 You should give the root view of the page to make it appear on the bottom of the page
您应该提供页面的根视图以使其显示在页面底部
There are two ways you can achieve this.有两种方法可以实现这一点。 One is to pass the
rootview
in the constructor of the RecyclerViewAdapter
class and pass the argument to the Snackbar
.一种是在
RecyclerViewAdapter
类的构造函数中传递rootview
并将参数传递给Snackbar
。 Another way is to use a callback method in your Activity
or Fragment
class that calls your RecyclerViewAdapter
class.另一种方法是在
Activity
或Fragment
类中使用回调方法来调用RecyclerViewAdapter
类。
For the first method, You just pass the rootView
of the page as a constructor parameter and pass the argument in the Snackbar
.对于第一种方法,您只需将页面的
rootView
作为构造函数参数传递并在Snackbar
传递参数。
For the second method, You can create an interface in the RecyclerViewAdapter
class, pass the interface instance as a parameter in the constructor, creating a method in the interface and notifying the method when the item has been clicked.对于第二种方法,您可以在
RecyclerViewAdapter
类中创建一个接口,将接口实例作为参数传递给构造函数,在接口中创建一个方法并在项目被点击时通知该方法。 Then you can show the Snackbar
in your Activity
class itself.然后,您可以在
Activity
类本身中显示Snackbar
。 See the code below,看下面的代码,
public class YourActivity implements RecyclerViewAdapter.CallbackListener{
View rootView;
@Override
protected void onCreate(Bundle savedInstanceState){
//
rootView = findViewById(R.id.my_root_view);
//
//
RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(this);
//
//
}
@Override
public void onItemClicked(){
Snackbar snackbar = Snackbar.make(rootView, "liked", Snackbar.LENGTH_LONG);
snackbar.show();
}
}
And your RecyclerViewAdapter class will be,你的 RecyclerViewAdapter 类将是,
public class RecyclerViewAdapter {
CallbackListener listener;
public RecyclerViewAdapter (CallbackListener listener){
this.listener = listener;
}
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
itemHolder.like.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onItemClicked();
}
});
public interface CallbackListener{
void onItemClicked();
}
}
Though first method is so simple, second method gives you more encapsulation, more cleaner code and more control over code.虽然第一种方法非常简单,但第二种方法为您提供了更多的封装、更清晰的代码和对代码的更多控制。
CoordinatorLayout
within your existing Activity
layout.Activity
布局中设置CoordinatorLayout
。CoordinatorLayout
as the first argument of the Snackbar.make()
command.CoordinatorLayout
作为Snackbar.make()
命令的第一个参数传递。 Thus in the end it should look this:因此,最终它应该是这样的:
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
itemHolder.like.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final View my_new_view = findViewById(R.id.myCoordinatorLayout); //define your CoordinatorLayout
Snackbar snackbar = Snackbar
.make(myCoordinatorLayout, "liked", Snackbar.LENGTH_LONG); //set CoordinatorLayout as first argument
snackbar1.show();
}
});
Your Snackbar
being shown at the bottom of the CoordinatorLayout
.您的
Snackbar
显示在CoordinatorLayout
的底部。
If you are trying to show Snackbar for whole screen, don't pass View/ViewGroup/Layout
of parent Activity/Fragment
to Adapter.如果您尝试在整个屏幕上显示 Snackbar,请不要将父
Activity/Fragment
View/ViewGroup/Layout
传递给 Adapter。
Instead pass Callback/Listener
to RecyclerView Adapter
from Activity/Fragment
.而是将
Callback/Listener
从Activity/Fragment
传递给 RecyclerView Adapter
。 This way when item is clicked, you trigger callback.这样当项目被点击时,你会触发回调。
Activity/Fragment
listening to that callback will get triggered and you can now show Snackbar/Toast/CustomView...侦听该回调的
Activity/Fragment
将被触发,您现在可以显示 Snackbar/Toast/CustomView...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.