如何在视图持有者中添加onclicklistner

时间:2019-04-06 08:18:41

标签: android firebase android-recyclerview adapter android-viewholder

我想在我的onclicklistner中添加一个viewholder,但是当用户单击该项目时,我应该将其传递值,我不知道如何将其添加到视图持有人中

我想在onclicklisner内修复usersviewholder,我不知道如何,所以我需要一些专家帮助。 我的代码在下面给出

 button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String searchText = editText1.getText().toString();
                firebaseUserSearch(searchText);
            }
        });
    }
    private void firebaseUserSearch(String searchText) {
        // Query firebaseSearchQuery = reference.orderByChild("title").startAt("\uf8ff" + searchText).endAt(searchText + "\uf8ff");
        String searchedText = searchText.trim();
        Query firebaseSearchQuery = reference.orderByChild("title").startAt(searchedText ).endAt(searchedText + "\uf8ff");
        FirebaseRecyclerOptions<Profile> options =
                new FirebaseRecyclerOptions.Builder<Profile>()
                        .setQuery(firebaseSearchQuery, Profile.class)
                        .setLifecycleOwner(this)
                        .build();





        FirebaseRecyclerAdapter firebaseRecyclerAdapter= new FirebaseRecyclerAdapter<Profile, MainActivity.UsersViewHolder>(options) {
            @NonNull
            @Override
            public MainActivity.UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new MainActivity.UsersViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item, parent,false));
            }

            @Override
            protected void onBindViewHolder(@NonNull MainActivity.UsersViewHolder holder, int position, @NonNull Profile model) {
                holder.setDetails(getApplicationContext(), model.getCon(), model.getTitle(), model.getDesc(), model.getImage());
            }
        };recyclerView.setAdapter(firebaseRecyclerAdapter);


    }
    public static class UsersViewHolder extends RecyclerView.ViewHolder{
        View mView;
        TextView title, desc;
        ImageView image;
        public UsersViewHolder(View itemView) {
            super(itemView);

            mView = itemView;
        }
        public void setDetails(Context ctx, String userName, String userStatus, String userImage){
            title = (TextView) mView.findViewById(R.id.title);
            desc = (TextView) mView.findViewById(R.id.desc);
            image = (ImageView) mView.findViewById(R.id.image);
            title.setText(userName);
            desc.setText(userStatus);

            Glide.with(ctx).load(userImage).into(image);
        }


    }

我想要一个类似onclick的意图


            Uri webpage = Uri.parse(profiles.get(getAdapterPosition()).getDesc());
            Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
            PackageManager packageManager = context.getPackageManager();
            List<ResolveInfo> activities = packageManager.queryIntentActivities(webIntent, 0);
            boolean isIntentSafe = activities.size() > 0;
            if (isIntentSafe) {
                context.startActivity(webIntent);
            }
            else
            {
                context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/search?q=google&hl=en")));

            }
        }

2 个答案:

答案 0 :(得分:0)

您必须在要单击的每个项目中分别输入implements View.OnClickListenersetOnClickListener

public static class UsersViewHolder extends RecyclerView.ViewHolder implements
 View.OnClickListener{

        View mView;
        TextView title, desc;
        ImageView image;
        public UsersViewHolder(View itemView) {
            super(itemView);

            mView = itemView;
        }
        public void setDetails(Context ctx, String userName, String userStatus, String userImage){
            title = (TextView) mView.findViewById(R.id.title);
            desc = (TextView) mView.findViewById(R.id.desc);
            image = (ImageView) mView.findViewById(R.id.image);
            title.setText(userName);
            desc.setText(userStatus);

            Glide.with(ctx).load(userImage).into(image);

            mView.setOnClickListener(this);

        }

     @Override
        public void onClick(View v) {

       //you can pass data to everywhere with callback here
        }

}

答案 1 :(得分:0)

public static class UsersViewHolder extends RecyclerView.ViewHolder{
    View mView;
    TextView title, desc;
    ImageView image;
    Model model;
    public UsersViewHolder(View itemView) {
        super(itemView);

        mView = itemView;
        itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String searchText = editText1.getText().toString();
            firebaseUserSearch(searchText);
        }
     });
    }

    public void setDetails(Context ctx, String userName, String userStatus, String userImage,Model _model){
        title = (TextView) mView.findViewById(R.id.title);
        desc = (TextView) mView.findViewById(R.id.desc);
        image = (ImageView) mView.findViewById(R.id.image);
        title.setText(userName);
        desc.setText(userStatus);
        model = _model;

        Glide.with(ctx).load(userImage).into(image);

    }


}