如何在视图持有者中添加一个 onclicklistener
how to add a onclicklistner in a view holder
我想在我的 viewholder
中添加一个 onclicklistner
但我不知道如何将它添加到视图持有者中,当用户单击该项目时它应该传递值
我想修复 usersviewholder
里面的 onclicklisner
,我不知道怎么做,所以我需要一些专家的帮助。
我的代码如下
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")));
}
}
您必须 implements View.OnClickListener
和 setOnClickListener
在您想要点击的每个项目中:
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
}
}
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);
}
}
我想在我的 viewholder
中添加一个 onclicklistner
但我不知道如何将它添加到视图持有者中,当用户单击该项目时它应该传递值
我想修复 usersviewholder
里面的 onclicklisner
,我不知道怎么做,所以我需要一些专家的帮助。
我的代码如下
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")));
}
}
您必须 implements View.OnClickListener
和 setOnClickListener
在您想要点击的每个项目中:
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
}
}
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);
}
}