如何通过单击RecyclerView项目打开AlertDialog?

时间:2018-03-09 17:11:13

标签: android android-recyclerview alertdialog

我有点问题。我无法通过点击AlertDialog项打开RecyclerView ..这是我的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();
    }
}

现在我想点击RecyclerView项目来显示它。这是适配器代码:

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

可悲的是,loginDialog.show()我无能为力。我无法解决getSupportFragmentManagergetFragmentManager。唯一的选择是在show类中创建名为LoginDialog的新函数。但后来我一直得到NPE ..

3 个答案:

答案 0 :(得分:0)

我建议您修改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();
    }
}

我们的想法是创建一个构造函数,并在每次从适配器创建时将上下文传递给LoginDialog。然后在创建Dialog时,您需要每次都传递上下文。

holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       LoginDialog loginDialog = new LoginDialog(context);
       loginDialog.show()
    }
});

希望有所帮助。

答案 1 :(得分:0)

Context中已有Adapter。所以你可以直接使用它。

LoginDialog dialogFragment = new LoginDialog ();
 dialogFragment.show(((AppCompatActivity)context).getSupportFragmentManager(), "Fragment");

答案 2 :(得分:0)

如果您想要做得更好,您需要了解适配器和活动的责任。适配器应该将数据绑定到视图和它的视图。如果您想在项目上显示对话框,请点击它,例如:

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

在您的片段/活动中:

adapter = new UserAdapter(List<UserModel>list, Context context, new View.OnClickListener(){
   new LoginDialog().show(supportFragmentManager, LoginDialog.TAG)
})