[英]How to open AlertDialog by clicking on RecyclerView item?
I have a little problem. 我有一点问题。 I cant open AlertDialog
by clicking on RecyclerView
item.. Here is my AlertDialog
code: 我无法通过单击RecyclerView
项来打开AlertDialog
。这是我的AlertDialog
代码:
public class LoginDialog extends AppCompatDialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_fragment, null);
builder.setView(view)
.setTitle("Login")
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
return builder.create();
}
}
Now I want to display it by clicking on RecyclerView
item. 现在,我想通过单击RecyclerView
项目来显示它。 Here is the Adapter code: 这是适配器代码:
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserViewHolder> {
private List<UserModel> list;
Context context;
public UserAdapter(List<UserModel>list, Context context){
this.context = context;
this.list = list;
}
@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new UserViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.view_item, parent, false));
}
@Override
public void onBindViewHolder(final UserViewHolder holder, int position) {
UserModel user = list.get(position);
holder.textName.setText(user.name + " " + user.surname);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LoginDialog loginDialog = new LoginDialog();
loginDialog.show()
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class UserViewHolder extends RecyclerView.ViewHolder{
TextView textName;
public UserViewHolder(View itemView) {
super(itemView);
textName = itemView.findViewById(R.id.textName);
}
}
}
Sadly, I cannot do nothing with loginDialog.show()
. 可悲的是,我无法使用loginDialog.show()
做任何事情。 I can't resolve getSupportFragmentManager
or getFragmentManager
. 我无法解析getSupportFragmentManager
或getFragmentManager
。 The only option is to create new function called show
in LoginDialog
class. 唯一的选择是在LoginDialog
类中创建名为show
新函数。 But then I get NPE all the time.. 但是后来我一直得到NPE。
I would suggest you modify your LoginDialog
like the following. 我建议您像下面那样修改LoginDialog
。
public class LoginDialog extends AppCompatDialogFragment {
private Context context;
public LoginDialog(Context context) {
this.context = context;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = ((AppCompatActivity)context)).getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_fragment, null);
builder.setView(view)
.setTitle("Login")
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
return builder.create();
}
}
The idea is to create a constructor and pass the context to your LoginDialog
each time you are creating it from your adapter. 想法是创建一个构造函数,并在每次从适配器创建它时将上下文传递给LoginDialog
。 Then while creating the Dialog
you need to pass the context every time. 然后,在创建Dialog
您需要每次传递上下文。
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LoginDialog loginDialog = new LoginDialog(context);
loginDialog.show()
}
});
Hope that helps. 希望能有所帮助。
You already have Context
in Adapter
. 您已经在Adapter
具有Context
。 So you can directly use it . 这样就可以直接使用它。
LoginDialog dialogFragment = new LoginDialog ();
dialogFragment.show(((AppCompatActivity)context).getSupportFragmentManager(), "Fragment");
If you want to make things better you need to understand responsibility of Adapter and Activity. 如果您想使事情变得更好,则需要了解适配器和活动的职责。 Adapter should bind data to views and that's it. 适配器应该将数据绑定到视图,仅此而已。 If you want to show Dialog on item click it's better to do it like: 如果要在项目上显示对话框,最好这样做:
public UserAdapter(List<UserModel>list, Context context, View.OnClickListener itemClickListener){
this.list = list;
this.context = context;
this.itemClickListener = itemClickListener;
}
......
@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new UserViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.view_item, parent, false), itemClickListener);
}
......
public class UserViewHolder extends RecyclerView.ViewHolder{
TextView textName;
public UserViewHolder(View itemView, View.OnClickListener itemClickListener) {
super(itemView);
textName = itemView.findViewById(R.id.textName);
itemView.setOnClickListener(itemClickListener);
}
}
....
In Your fragment/activity: 在您的片段/活动中:
adapter = new UserAdapter(List<UserModel>list, Context context, new View.OnClickListener(){
new LoginDialog().show(supportFragmentManager, LoginDialog.TAG)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.