本文介绍了Firebase UI Recyclerview OnClick 不工作,尝试了一切.这不是一个重复的问题,请的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 Android 应用中添加 Firebase Recyclerview.当我添加时,所有数据都正常从 Firestore 获取,但是在处理 onClick 事件时,它根本不起作用.

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.

  • 添加了方法接口.
  • 在我的 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");
}
}

这是我的:

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();
    }
}

这里是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>

注意:我可以从 Firestore 获取数据,它可以正确显示数据.请帮忙.我遵循了 Stack Overflow 的所有其他答案.

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

推荐答案

在给定的设置中,OnClickListener 被设置在 ViewHolderitemView,这将是其布局中的根 View,也就是 CardView.但是,在 LinearLayout 上设置的 clickablefocusable 属性会导致它首先获取触摸事件,因此它基本上是在 LinearLayout 之前拦截它们code>CardView 将处理它们以响应点击.但是,LinearLayout 上没有侦听器,所以什么也没有发生.

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.

假设您希望整个项目 View 可点击,只需删除 android:clickable="true"android:focusable="true" 中的 code> 属性.如果没有 clickablefocusable 子项,CardView 将最终注册点击.

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.

如果您可能只想要某个子项可点击——例如,LinearLayout——那么您应该在该子项上设置 OnClickListener,而不是整个 卡片视图.但是,如果要成为唯一可点击的子项或孙项,您仍然不需要在任何地方使用这些属性.这些属性在基本的、相对扁平的布局中通常不是必需的,例如列表项.

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.

这篇关于Firebase UI Recyclerview OnClick 不工作,尝试了一切.这不是一个重复的问题,请的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 10:21