Firebase UI Recyclerview OnClick Not Working, tried everything. And it is not a duplicate question please

Viewed 448

I'm trying to add a Firebase Recyclerview in my Android App. When I add, all the data is getting fetched from Firestore normally, but when it comes to handle onClick event, it is not working at all.

Things I followed:

  • Added Interface with method.
  • Implemented interface in my TipsActivity.java

Here is the code:

TipsActivity.java

import androidx.appcompat.app.AppCompatActivity;
import androidx.paging.PagedList;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.widget.Toast;

import android.util.Log;
import com.firebase.ui.firestore.paging.FirestorePagingOptions;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;



public class TipsActivity extends AppCompatActivity implements FirestoreTipsAdapter.OnListItemClick {

   FirestoreTipsAdapter firestoreTipsAdapter;
    FirebaseFirestore firebaseFirestore;
    RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tips);

    firebaseFirestore = FirebaseFirestore.getInstance();
    recyclerView = findViewById(R.id.list);

    Query query = firebaseFirestore.collection("DailyTips").document("MyTips").collection("Tips");

    PagedList.Config config = new PagedList.Config.Builder()
            .setInitialLoadSizeHint(10)
            .setPageSize(5)
            .build();

    FirestorePagingOptions<TipsModel> firestorePagingOptions = new FirestorePagingOptions.Builder<TipsModel>()
            .setLifecycleOwner(this)
            .setQuery(query,config,TipsModel.class)
            .build();

    firestoreTipsAdapter = new FirestoreTipsAdapter(firestorePagingOptions,this,this);

       recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(firestoreTipsAdapter);


}

@Override
public void onItemClick() {
    Toast.makeText(this, "Show up bruh!", Toast.LENGTH_SHORT).show();
Log.d("AT_LEAST","You should work");
}
}

And here goes my:

FirestoreTipsAdapter.java

package com.mycompany.company;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.firebase.ui.firestore.paging.FirestorePagingAdapter;
import com.firebase.ui.firestore.paging.FirestorePagingOptions;

public class FirestoreTipsAdapter extends FirestorePagingAdapter<TipsModel, FirestoreTipsAdapter.TipsViewHolder> {

    private OnListItemClick onListItemClick;
    Context context;

    public FirestoreTipsAdapter(@NonNull FirestorePagingOptions<TipsModel> options,OnListItemClick onListItemClick,Context context) {
        super(options);
        this.onListItemClick = onListItemClick;
        this.context = context;
    }

    @Override
    protected void onBindViewHolder(@NonNull TipsViewHolder holder, int position, @NonNull TipsModel model) {
        holder.title.setText(model.getTitle());
        holder.description.setText(model.getDescription());

    }

    @NonNull
    @Override
    public TipsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
        return new TipsViewHolder(view);
    }

    public class TipsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView title,description;


        public TipsViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.list_title);
            description = itemView.findViewById(R.id.list_desc);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "Are you working bro?", Toast.LENGTH_SHORT).show();
                }
            });

        }

        @Override
        public void onClick(View v) {
            onListItemClick.onItemClick();
        }
    }
    public interface OnListItemClick{
        void onItemClick();
    }
}

Here is the code of list_item.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.cardview.widget.CardView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="10dp"
    android:id="@+id/tipCardView"
    app:cardElevation="5dp"
    app:cardBackgroundColor="#E2E0EE"
    app:cardCornerRadius="5dp"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:id="@+id/list_root"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:orientation="vertical"
        android:background="?attr/selectableItemBackground"
        android:padding="16dp">

    <TextView
        android:id="@+id/list_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Title"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/list_desc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:text="Description" />

    </LinearLayout>

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/curveshape"
        android:layout_gravity="end|bottom"
        android:layout_marginBottom="-30dp"
        android:alpha="0.2"
        />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/tips"
        android:layout_gravity="end|bottom"
        android:layout_marginBottom="-10dp"
        android:layout_marginRight="25dp"
        android:alpha="0.2"
        />


</androidx.cardview.widget.CardView>

Note: I'm able to fetch data from Firestore, it is showing data properly. enter image description here Please help. I followed all other answers from Stack Overflow.

1 Answers

In the given setup, the OnClickListener is being set on the ViewHolder's itemView, which will be the root View in its layout, which is the CardView. However, the clickable and focusable attributes set on the LinearLayout cause it to get first grabs on touch events, so it's basically intercepting them before the CardView would handle them to respond to a click. There's no listener on the LinearLayout, though, so nothing happens.

Assuming that you want the entire item View clickable, simply remove the android:clickable="true" and android:focusable="true" attributes from the <LinearLayout>. With no clickable or focusable children, the CardView will then end up registering the click.

If instead you might want only a certain child clickable – e.g., the LinearLayout – then you would set the OnClickListener on that child, rather than the whole CardView. You still wouldn't need those attributes anywhere, though, if that's to be the only clickable child or grandchild. Those attributes usually aren't necessary in basic, relatively flat layouts, like that for your list items.

Related