Answer the question
In order to leave comments, you need to log in
Why is OnClick called when the space between the buttons is clicked?
I am a beginner writing my first android application in Java language. (Android Studio 4.0)
Working with RecyclerView. Inside the adapter, in the class, the holder added an OnClickListener and described onClick. Faced with the problem that when you click on the button, the actions contained in onClick should be performed, but this happens when you click on the space between the buttons. The button press is ignored. If you click on the space above the second button, then when you request a position, it returns one. I tried to remove the space between the buttons, but in this case, pressing the button is ignored. I'm more than sure that I made a stupid mistake.
I would be grateful for help with my shitcode.
ListAdapter.java
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListViewHolder>{
ArrayList<Catalogue> catalogue;
public ListAdapter(ArrayList<Catalogue> catalogue){
this.catalogue = catalogue;
}
class ListViewHolder extends RecyclerView.ViewHolder{
Button button;
public ListViewHolder(@NonNull View itemView) {
super(itemView);
button = (Button) itemView.findViewById(R.id.button_of_list);
itemView.setOnClickListener(new View.OnClickListener() { // добавляю слушателя
@Override
public void onClick(View v) {
CreateNewListOfButton.CreateButton( "154/", "2/"); // действие
}
});
}
}
@NonNull
@Override
public ListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.button_list, parent, false);
return new ListViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ListViewHolder holder, int position) {
holder.button.setText(catalogue.get(position).getTextForButton());//Добавление текста в кнопку
}
@Override
public int getItemCount() {
return catalogue.size();
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</androidx.recyclerview.widget.RecyclerView>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button_of_list"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#1f4b99"
android:textColor="#f1fcff"
android:focusable="false"
android:focusableInTouchMode="false"
></Button>
</LinearLayout>
Answer the question
In order to leave comments, you need to log in
Because you put the listener not on the button, but on the itemView. Put it on a button.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question