简体   繁体   English

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

[英]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 . 我无法解析getSupportFragmentManagergetFragmentManager 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.

相关问题 单击 RecyclerView 的项目时显示 AlertDialog - Show AlertDialog when clicking an item of RecyclerView 通过单击recyclerview项打开片段 - open a Fragment by clicking on a recyclerview item 如何通过单击来自recyclerview的项目来打开新活动 - How to open new activity from clicking an item from recyclerview 单击RecyclerView中的特定项目后如何打开URL? - How to open URL after clicking at a particular item in RecyclerView? 如何使用AlertDialog在RecyclerView中添加项目? - How to use an AlertDialog to add an item in a RecyclerView? 通过单击recyclerview项打开片段,然后移至该片段 - open a Fragment by clicking on a recyclerview item and move to this fragment 如何在Alertdialog中单击ListView项以实现意图? - How to make an Intent in Alertdialog clicking ListView item? Android | RecyclerView项目上的AlertDialog单击 - Android | AlertDialog on RecyclerView Item Click 如何在单击RecyclerView项时打开一个片段,该片段在另一个片段中实现了,可以再次使用ViewPager进行滑动? - How to open a fragment on clicking to RecyclerView item which is implemented in another fragment which is again can be swiped by using ViewPager? 单击 RecyclerView 中的项目时如何获取详细信息? - How to get details when clicking on an item in a RecyclerView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM