使用 Firebase RecyclerAdapter 时如何获得单击列表项的位置

How can I get the position of the list item clicked while using Firebase RecyclerAdapter

我使用的是 Firebase RecyclerAdapter 而不是普通的 RecyclerAdapter。我想存储被点击的列表项的位置。 HomeFragment 的代码如下


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.FirebaseDatabase;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */

public class HomeFragment extends Fragment {
    private static final String TAG = "HomeFragment";
    private RecyclerView recyclerView;
    private ProductAdapter adapter;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public HomeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v=  inflater.inflate(R.layout.fragment_home, container, false);
        recyclerView = v.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        String u = FirebaseAuth.getInstance().getCurrentUser().getUid();
        FirebaseRecyclerOptions<Product> options = new FirebaseRecyclerOptions.Builder<Product>()
                .setLifecycleOwner(this)
                .setQuery(FirebaseDatabase.getInstance().getReference().child(u).child("Products"), Product.class)
                .build();

        adapter = new ProductAdapter(options);
        recyclerView.setAdapter(adapter);
        return v;
    }

    @Override
    public void onStart() {
        super.onStart();


    }

    @Override
    public void onStop() {
        super.onStop();

    }
}

ProductAdapter class 的代码如下

package com.example.prj;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

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

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.squareup.picasso.Picasso;

public class ProductAdapter extends FirebaseRecyclerAdapter<Product, ProductAdapter.ProductViewHolder> {

    private static final String TAG = "ProductAdapter";
    ImageView btn_stock;
    public ProductAdapter(@NonNull FirebaseRecyclerOptions<Product> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull ProductViewHolder holder, int position, @NonNull Product model) {
        holder.pname.setText(model.getPname());
        holder.price.setText(model.getPrice());
        holder.description.setText(model.getDescription());
        Picasso.get().load(model.getImage()).into(holder.image);
           }

    @NonNull
    @Override
    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_row, parent, false);

        return new ProductViewHolder(view);
    }



    class ProductViewHolder extends RecyclerView.ViewHolder{
        TextView pname, price, description;
        ImageView image ;
        public ProductViewHolder(@NonNull final View itemView) {
            super(itemView);
            pname = itemView.findViewById(R.id.product_list_name);
            price = itemView.findViewById(R.id.product_list_price);
            description = itemView.findViewById(R.id.product_list_description);
            image = itemView.findViewById(R.id.product_list_image);




        }
    }

使用被点击的列表项的位置我想制作列表项expandable.Like用户点击列表项并且列表项展开并显示关于该项目的更多细节..如果有人有解决方案请帮助我..

onBindViewHolder()设置item点击监听:

@Override
protected void onBindViewHolder(@NonNull ProductViewHolder holder, int position, @NonNull Product model) {

.....
.....
....

//here set the click of the item

holder.view.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View view){

//here do something with position that is passed in the onBindViewHolder
//if you clicked item 1 the position is 0
//if you clicked item 2 the position is 1
..............

Log.d("POSITION" , String.valueOf(position));


}


});

在您的 Viewholder 中添加 view 参数

class ProductViewHolder extends RecyclerView.ViewHolder{

TextView pname, price, description;
ImageView image ;

//here
View view;

public ProductViewHolder(@NonNull final View itemView) {
    super(itemView);
    //set it here
    view = itemView;
    ......
    ......