-1

I am passing position and view in my OnItemClicked(int position, View v) method of OnItemClickListener interface. Now in my ViewReceptionistActivity I want to check which item and which sub part is clicked. If the user clicks an image then perform task A els if user clicks name perform task B and so on..

Here is my layout file - viewreceptionist_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    cardElevation="5dp"
    contentPadding="10dp"
    app:cardCornerRadius="20dp"
    app:cardUseCompatPadding="true"
    xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="12"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:background="@drawable/card_background">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_weight="2">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/viewReceptionistImage"
            android:layout_width="70dp"
            android:layout_height="70dp"
            app:civ_border_width="2dp"
            android:layout_gravity="center"
            app:civ_border_color="@color/black" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_marginLeft="10dp"
        android:layout_weight="9">

        <TextView
            android:id="@+id/textViewViewReceptionistName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name"
            android:textSize="18dp"
            android:layout_marginTop="5dp"
            android:textColor="@color/black"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/textViewViewReceptionistEmail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Email"
            android:textSize="15dp"
            android:layout_marginTop="5dp"
            android:textColor="@color/black"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/textViewViewReceptionistMobile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mobile No."
            android:textSize="15dp"
            android:layout_marginTop="5dp"
            android:textColor="@color/black"
            android:textStyle="bold"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_weight="1">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/viewReceptionistGender"
            android:layout_width="40dp"
            android:layout_height="40dp"
            app:civ_border_width="2dp"
            android:layout_gravity="center"
            app:civ_border_color="@color/black" />

    </LinearLayout>

</LinearLayout>
</android.support.v7.widget.CardView>

And here is my Adapter - ViewReceptionistAdapter

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

public class ViewReceptionistAdapter extends RecyclerView.Adapter<ViewReceptionistAdapter.ViewReceptionistHolder>
{
    Context context;
    int resource;
    ArrayList<Receptionist> objects;

    OnItemClickListener listener;

    public interface OnItemClickListener
    {
        void OnItemClicked(int position, View v);
    }

    public void setOnItemClickListener(OnItemClickListener listener)
    {
        this.listener = listener;
    }

    public ViewReceptionistAdapter(Context context, int resource, ArrayList<Receptionist> objects)
    {
        this.context = context;
        this.resource = resource;
        this.objects = objects;
    }

    @Override
    public ViewReceptionistHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View view = LayoutInflater.from(context).inflate(resource, parent, false);

        ViewReceptionistHolder viewReceptionistHolder = new ViewReceptionistHolder(view);

        return viewReceptionistHolder;
    }

    @Override
    public void onBindViewHolder(ViewReceptionistHolder holder, int position)
    {
        Receptionist receptionist = objects.get(position);

        holder.tvViewReceptionistName.setText(receptionist.name);
        holder.tvViewReceptionistEmail.setText(receptionist.email);
        holder.tvViewReceptionistMobile.setText(receptionist.mobile);

        String url = receptionist.imageUrl;
        Picasso.get().load(url).into(holder.viewReceptionistImage);


        String gender;

        gender = receptionist.gender;

        if(gender.equals("Male"))
        {
            holder.viewReceptionistgender.setImageResource(R.drawable.male_icon);
        }
        else
        {
            holder.viewReceptionistgender.setImageResource(R.drawable.female_icon);
        }
    }

    @Override
    public int getItemCount()
    {
        return objects.size();
    }

    public class ViewReceptionistHolder extends RecyclerView.ViewHolder
    {

        public TextView tvViewReceptionistName;
        public TextView tvViewReceptionistEmail;
        public TextView tvViewReceptionistMobile;
        public CircleImageView viewReceptionistImage;
        public CircleImageView viewReceptionistgender;

        public ViewReceptionistHolder(final View itemView)
        {
            super(itemView);

            tvViewReceptionistName = itemView.findViewById(R.id.textViewViewReceptionistName);
            tvViewReceptionistEmail = itemView.findViewById(R.id.textViewViewReceptionistEmail);
            tvViewReceptionistMobile = itemView.findViewById(R.id.textViewViewReceptionistMobile);

            viewReceptionistImage = itemView.findViewById(R.id.viewReceptionistImage);
            viewReceptionistgender = itemView.findViewById(R.id.viewReceptionistGender);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    if(listener != null)
                    {
                        int position = getAdapterPosition();

                        if(position != RecyclerView.NO_POSITION)
                        {
                            listener.OnItemClicked(position, v);
                        }
                    }

                }
            });
        }
    }
}

Here is ViewReceptionistActivity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.ArrayList;

public class ViewReceptionistActivity extends AppCompatActivity
{
    RecyclerView recyclerView;
    ArrayList<Receptionist> receptionistsList;

    ViewReceptionistAdapter viewReceptionistAdapter;

    FirebaseFirestore firestore;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_receptionist);

        getSupportActionBar().setTitle("View Receptionist");

        recyclerView = (RecyclerView)findViewById(R.id.recyclerView);

        firestore = FirebaseFirestore.getInstance();
        receptionistsList = new ArrayList<Receptionist>();


        Query query = firestore.collection("Receptionist").orderBy("name", Query.Direction.ASCENDING);

        query.addSnapshotListener(this, new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e)
            {
                for(DocumentSnapshot documentSnapshot: documentSnapshots)
                {
                    Receptionist receptionist = documentSnapshot.toObject(Receptionist.class);
                    receptionistsList.add(receptionist);

                    viewReceptionistAdapter.notifyDataSetChanged();
                }
            }
        });

        viewReceptionistAdapter = new ViewReceptionistAdapter(this, R.layout.viewreceptionist_item, receptionistsList);

        viewReceptionistAdapter.setOnItemClickListener(new ViewReceptionistAdapter.OnItemClickListener() {
            @Override
            public void OnItemClicked(int position, View v)
            {

                Toast.makeText(ViewReceptionistActivity.this, "You Clicked " + position + " " + v, Toast.LENGTH_SHORT).show();

//HERE I WANT TO CHECK IF USER CLICKS AN IMAGE OR NAME OR EMAIL...
//IF USER CLICKS IMAGE - PERFORM TASK A
//IF USSER CLICKS NAME - PERFORM TASK B...AND SO ON


            }
        });

        LinearLayoutManager manager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);


        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(viewReceptionistAdapter);

    }
}

I want to get the specific item clicked from the view...

Arshad
  • 1,262
  • 16
  • 25
Raj
  • 2,997
  • 2
  • 12
  • 30
  • did you manage to make this work @Raj? – Levi Moreira May 14 '18 at 17:50
  • Yup, that method works fine.. But its too lengthy if we have 5-6 views in a recycler view item.But i am trying if we are passing the view along with the position in the method then we must have an method to get an item from that view so that we can work on that instead of setting on click listener again and again.. I have also tried by passing itemView from holder but nothing happened.. – Raj May 15 '18 at 13:46
  • There's no way of detecting clicks without using click listener. You won't be able to check if a subview was clicked just by detecting a click in the parent view. – Levi Moreira May 15 '18 at 14:21
  • Ok thanks i will go with this method only... – Raj May 15 '18 at 15:47

1 Answers1

3

You're better off extending your click interface with methods for each view you want to add a reaction to a click event:

public interface OnItemClickListener
    {
        void OnItemImageClicked(int position, View v);
        void OnItemNameClicked(int position, View v);
    }

And in your viewHolder:

viewReceptionistImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    if(listener != null)
                    {
                        int position = getAdapterPosition();

                        if(position != RecyclerView.NO_POSITION)
                        {
                            listener.OnItemImageClicked(position, v);
                        }
                    }

                }
            });


tvViewReceptionistName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    if(listener != null)
                    {
                        int position = getAdapterPosition();

                        if(position != RecyclerView.NO_POSITION)
                        {
                            listener.OnItemNameClicked(position, v);
                        }
                    }

                }
            });

Then in your ViewReceptionistActivity you simply implement the interface and override the methods (as they will be many). Then you set the listener in your adapter like so:

viewReceptionistAdapter.setOnItemClickListener(this);

Or you can use the old way:

viewReceptionistAdapter.setOnItemClickListener(new ViewReceptionistAdapter.OnItemClickListener() {
            @Override
            public void OnItemImageClicked(int position, View v)
            {
             Toast.makeText(ViewReceptionistActivity.this, "You Clicked " + position + "Image View: " + v, Toast.LENGTH_SHORT).show();

            }

            @Override
            public void OnItemNameClicked(int position, View v)
            {
              Toast.makeText(ViewReceptionistActivity.this, "You Clicked " + position + " TextView: " + v, Toast.LENGTH_SHORT).show();

            }
        });
Levi Moreira
  • 11,917
  • 4
  • 32
  • 46