Itemclicklistener not available

Asked

Viewed 29 times

0

I’m creating a project that shows a list of equipment through a Recyclerview. When clicking on an item of this Recyclerview, you would need to open a new screen with details of the clicked item. The problem is that the itemClickListener option does not appear, even though it is declared in viewHolder.

Viewholder

public class InventarioViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public TextView txtHd, txtHostName, txtMarca, txtMemoria, txtModelo, txtPcId, txtProcessador, txtSetor, txtSo, txtTipo, txtUsuario;
    public ImageView img_user, menu_options;

    private ItemClickListener itemClickListener;

    public InventarioViewHolder(View itemView) {
        super(itemView);

         txtPcId = itemView.findViewById(R.id.pc_nome);
         txtUsuario = itemView.findViewById(R.id.pc_usuario);
         txtSetor = itemView.findViewById(R.id.pc_setor);

         img_user = itemView.findViewById(R.id.img_user);
         menu_options = itemView.findViewById(R.id.menu_options);



         itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        itemClickListener.onClick(view, getAdapterPosition(), false);

    }
}

Home class.

private void loadInventario() {
        adapter = new FirebaseRecyclerAdapter<Inventario, InventarioViewHolder>(Inventario.class,
                R.layout.inventario_item, InventarioViewHolder.class, categories) {
            @Override
            protected void populateViewHolder(InventarioViewHolder viewHolder, Inventario model, int position) {
                viewHolder.txtPcId.setText(model.getPcId());
                viewHolder.txtUsuario.setText(model.getUsuario());
                viewHolder.txtSetor.setText(model.getSetor());

                Picasso.with(Home.this).load(model.getImage()).into(viewHolder.img_user);


    //***********************  Após o viewHolder. deveria aparecer a opção setItemClickListener  *******************
                viewHolder.setItemClickListener(new ItemClickListener()){

                }




            }
        };
        adapter.notifyDataSetChanged();
        recycler_inventario.setAdapter(adapter);
    }

Recyclerview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Home">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_inventario"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    </android.support.v7.widget.RecyclerView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="16dp"
        app:backgroundTint="@android:color/white"
        app:srcCompat="@drawable/ic_playlist_add_black_24dp"/>



</RelativeLayout>

Layout of the Recyclerview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp">

    <View
        android:background="@color/colorPrimaryDark"
        android:layout_width="match_parent"
        android:layout_marginTop="60dp"
        android:layout_height="0.6dp"/>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white">


        <LinearLayout
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:orientation="horizontal">

            <com.pkmmte.view.CircularImageView
                android:id="@+id/img_user"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/ic_account_circle_black_24dp" />


        </LinearLayout>



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="12dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/pc_nome"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:background="@color/overlayBackground"
                android:text="000000"
                android:textColor="@color/colorPrimaryDark"
                android:textSize="16sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/pc_usuario"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:background="@color/overlayBackground"
                android:text="Nome do usuário"
                android:textColor="@color/colorPrimaryDark"
                android:textSize="14sp" />

            <TextView
                android:id="@+id/pc_setor"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:background="@color/overlayBackground"
                android:text="Setor"
                android:textColor="@color/colorPrimaryDark"
                android:textSize="12sp"
                android:textStyle="italic" />



        </LinearLayout>



    </LinearLayout>



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="350dp"
        android:layout_marginTop="12dp">

        <ImageView
            android:id="@+id/menu_options"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_more_vert_black_24dp" />

    </RelativeLayout>




</RelativeLayout>
  • He doesn’t show up because his class InventarioViewHolder does not have the method. At least in the code you posted only the variable is declared itemClickListener. Where is the Setter?

  • Thank you Piovezan.. That was it.!

  • @vsousa took my "I accept" ;-;

1 answer

0


Create this class in your project:

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        public void onItemClick(View view, int position);

        public void onLongItemClick(View view, int position);
    }

    GestureDetector mGestureDetector;

    public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
        mListener = listener;
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null && mListener != null) {
                    mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
                }
            }
        });
    }

    @Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
        View childView = view.findChildViewUnder(e.getX(), e.getY());
        if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
            mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
            return true;
        }
        return false;
    }

    @Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }

    @Override
    public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}
}

After that go to your Activity where the Recycler view is and do (After you have already applied the Adapter, of course):

mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(Main.this, mRecyclerView ,new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int i) {
                     //Na view você pode recuperar os componentes em cada item 
                     //Do recycler view, tipo:  view.findviewById(R.id.minhaImagem);

                  //int i é igual a posição da lista, se você sabe a posição 
                //pode recuperar todos os itens da sua lista de objetos
                }

                @Override
                public void onLongItemClick(View view, int i) {
                     //O mesmo pra o onLong
                }
            })
        );

So you have a better control of your Recyclerview

Browser other questions tagged

You are not signed in. Login or sign up in order to post.