Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView onItemClicked callback

I am implementing a Navigation Drawer with a RecyclerView, using this tutorial, but I don't want to apply the whole tutorial, I just want to handle the user's click. I've implemented the onClickListener inside the ViewHolder of my MyAdapter class:

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{
    int Holderid; ImageView imageView, profile;
    TextView textView, Name, email, drawerTitle;

    private ClickListener listener;
    // ViewHolder Constructor with View and viewType as a parameter
    public ViewHolder(View itemView, int ViewType, ClickListener listener) {
        super(itemView);

        if (ViewType == TYPE_HEADER){
            itemView.setClickable(false);
            Name = (TextView) itemView.findViewById(R.id.user_name);
            email = (TextView) itemView.findViewById(R.id.user_email);
            profile = (ImageView) itemView.findViewById(R.id.circleView);

            Holderid = 0;
        }
        if(ViewType == TYPE_ITEM) {
            itemView.setClickable(true);
            textView = (TextView) itemView.findViewById(R.id.rowText);

            imageView = (ImageView) itemView.findViewById(R.id.rowIcon);

            Holderid = 1;

            // Handle item click and set the selection
        }
        if(ViewType == TYPE_SEPARATOR){
            itemView.setClickable(false);
            drawerTitle = (TextView) itemView.findViewById(R.id.drawerTitle);

            Holderid = 2;
        }
        this.listener = listener;
        itemView.setOnClickListener(this);
        itemView.setOnLongClickListener(this);
    }
    int pos;
    @Override
    public void onClick(View v) {
        if (listener != null) {
            listener.onItemClicked(getPosition());
            pos= getPosition();
            Log.d("TAG", "Item clicked at position " + getPosition());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (listener != null) {
            return listener.onItemLongClicked(getPosition());
        }

        return false;
    }
    public interface ClickListener {
        public void onItemClicked(int position);
        public boolean onItemLongClicked(int position);
    }
}

What I can't understand is how to apply a ClickListener to the MyAdapter object:

        mAdapter = new MyAdapter(dataList, mSelectedPositions, ...?);

so that when the user clicks on one item, I can handle the user's click back on the MainActivity.

I've created the Adapter this way (as outlined in the tutorial):

Whole MyAdapter class: http://pastie.org/private/cibcsj3zieorlkcgh2rm1g

and this is my MainActivity class' onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /* Assigning the toolbar object ot the view
    and setting the the Action bar to our toolbar */
    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);
    // enable ActionBar app icon to behave as action to toggle nav drawer
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); //pulsante drawer
    getSupportActionBar().setHomeButtonEnabled(true);      //pulsante dietro

    //Initializing
    mTitle = mDrawerTitle = getTitle();
    SharedPreferences usrData = getSharedPreferences(
                                    usr_loggedin,
                                    MODE_PRIVATE);
    AVATARresID = R.mipmap.aka;

    // Add Drawer Item to dataList
    dataList = new ArrayList<>();
    dataList = prepareDatalist(dataList, NAME, EMAIL, AVATARresID);

    mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView);
    mRecyclerView.setHasFixedSize(true);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    mAdapter = new MyAdapter(dataList, mSelectedPositions); //**ERROR HERE**

    mRecyclerView.setAdapter(mAdapter);
    mLayoutManager = new LinearLayoutManager(this);

    mRecyclerView.setLayoutManager(mLayoutManager);

    // Drawer Listener set to the Drawer toggle
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerToggle.syncState(); // Finally we set the drawer toggle sync State
}

How do I pull the result of the onClick that is present in the MyAdapter class back to my MainActivity,

    int pos;
    @Override
    public void onClick(View v) {
        if (listener != null) {
            listener.onItemClicked(getPosition());
            pos= getPosition();
            Log.d("TAG", "Item clicked at position " + getPosition());
        }
    }

so that I can handle actions depending on the clicked item position?

like image 296
Cris Avatar asked Nov 10 '22 12:11

Cris


1 Answers

The constructor of MyAdapter takes also an object of a class that implements ClickListener. You could, for instance, let your Activity/Fragment implements that Interface and use this as parameter

public class MainActivity extends Activity implements ViewHolder.ClickListener {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     // ....
     mAdapter = new MyAdapter(dataList, mSelectedPositions, this);
     // ....
    }
like image 128
Blackbelt Avatar answered Nov 15 '22 06:11

Blackbelt