235 RecyclerView OnItemClick Part 1 |

preview_player
Показать описание
"Learn How To Design + Code A Complete App From Scratch To Playstore"
-~-~~-~~~-~~-~-

This Android Tutorial shows you how to start an Activity from your Navigation Drawer using the RecyclerView's onClick. We demonstrate 2 ways of handling the item click of the RecyclerView in this video 1)Implement the View.OnClickListener in your class that implements RecyclerView.ViewHolder and override the onClick method to perform the action you want. 2) Use the same technique described in 1) but add an interface to handle the click event inside the Fragment instead of the Adapter and let the Fragment implement that interface.

OUR WEBSITE

How to LEARN ANDROID from slidenerd?
Follow this-

How to learn JAVA from slidenerd?
Follow this-

How to LEARN JAVASCRIPT from slidenerd?
Follow this-

How to LEARN PYTHON from slidenerd?
Follow this-

OUR SOCIAL LINKS
Follow us on

WHERE TO GET CODE?
Рекомендации по теме
Комментарии
Автор

Great start to my Monday! Thanks for getting this posted! :)

opheliadesign
Автор

Hey, I got featured in this video in the part where you showed the comments! Awesome :) Thanks for the fast reply, Vivz, now I can get back to work on my app.

BadNeighborsMusic
Автор

Thanks man, I found what I was looking for

brunod
Автор

thanks man you are really very helpfull... just because of your methods i always find a way to solve my own coding problems....

abmuneeb
Автор

Vivek Ramesh Sir you're great....

faizakhan
Автор

We can avoid creating interface inside Adapter class. Instead we can directly define ClickListener method & avoid implementing the ClickListener in NavigationDrawerFragment class.

This would further require following changes in Adapter Class:
public void navigationDrawerFragment){
this.navigationDrawerFragment = navigationDrawerFragment;
}

and
@Override
public void onClick(View view) {
navigationDrawerFragment.AdapterItemCLicked(view, getAdapterPosition());
}
}

Though it will tightly couple Adapter Class with NavigationDrawerFragment class.

As someone mentioned in comments technique 2 is similar to the technique you showed for Inter Fragment communication using interfaces.

gauravch
Автор

I see 2nd way same same the way fragment communicate with the host activity in your Android Fragments Tutorial, nice teacher sir 

phamnhat
Автор

Useful method, thank you! Waiting first method to look how you implement this.

user
Автор

Hi, I started from scratch using your tutorials. Awesome work :)

getPosition is deprecated and is replaced getAdapterPosition()

HirakChhatbar
Автор

Once again an outstanding video..

Technique 2 is similar to the technique you showed for Inter Fragment communication using interfaces..

dhavalchheda
Автор

Thanks that clearly explains.but what if i have a image as bitmap and i would like to show the selected image in the recyclerview into another activity? so how do i get the bitmap and where do i pass it as bundle please if you can explain that it will be a big help. Thanks.

mohammadsayyaf
Автор

Thanks for sharing...
I really like your videos
BTW, I went get codes and downloaded as a zip files but I can't open it on Android Studio. It gives me errors...

nathans
Автор

What I want is When you click on an item in navigation drawer on activity opens and in that activity we find the same navigation drawer so that we can navigate to other activity. basically I want to navigate activities instead of fragments.

ghimanshuss
Автор

Very useful tutorial but it has come a day later. :-) Last night I developed the same as described here.

Dorax
Автор

Nice tutorials. Just little optimisation -  you can create an anonymous object which implements your interface. This will allow you to skip the check whether clickListener is null.

clickListener = new ClickListener() {
@Override
            public void itemClicked(Wiev view, int position) {

            }
}

hraseq
Автор

Vivz, great video! But one question, at 8:09, if you look at your public void onClick(View v) method, shouldn't you have deleted the original context.startactivity call? Because if it is there, then is the interface really being used to start the sub activity?

hydeaway
Автор

There is no need to create a seprate method for setting click listener. This can be done in while contructing the object of adapter class. since our activity implements listener its context can be cast to Clicklistener adapter. so we can save the clickListener in the constructor itself.

hitec
Автор

slidenerd I've got a suggestion for you, your're better off using the inbuilt it allows you to have long click etc.. by a less complex way.

This way revolves around the Gesture Detector, for which you already have a great tutorial.

So you implement your item click like this :

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

    new RecyclerItemClickListener(context, new {
        // do whatever
      }
    })
);

You would notice that you have an error ... the RecyclerItemClickListener actually doesn't exist..so make a new class with that name and copy the following to it.

public class RecyclerItemClickListener implements {
  private OnItemClickListener mListener;

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

  GestureDetector mGestureDetector;

  public context, OnItemClickListener listener) {
    mListener = listener;
    mGestureDetector = new GestureDetector(context, new {
        return true;
      }
    });
  }

    View childView = view.findChildViewUnder(e.getX(), e.getY());
    if (childView != null && mListener != null && {
      mListener.onItemClick(childView,
      return true;
    }
    return false;
  }

}



So here you notice, the single tap up method ... from gesture detector, like this you can implement long press too and you have a wide variety of choices and a lot of control ! 

- Developer of Pc Constructor on Google Play. (Not in India Currently)

Also Slidenerd i need a little help, it would be great if you could reply with your email .

blezzarusstudios
Автор

will you be able to open the navigation drawer if you were on subactivity?coz a friend of mine told me to use fragments instead of an activity on each row, is that the reason why we  should use fragments?sorry im a beginner

edmeralarte
Автор

i was fighting with the onclick events too. then i freed my head and yea it is simpler as you guys think...


setting the Cursor (can also be a array or vector list or what you guys perform.) to final will give us access to him in the onclick event. so we can access data we need.

setting the values final again will protect you from getting wrong data ...



public void onBindViewHolder(final viewHolder, final Cursor cursor) {
...
View.OnClickListener() {
final int id = cursor.getInt(13);
final String season = cursor.getString(3);
final String episode = cursor.getString(2);

@Override
public void onClick(View v) {
Log.i("upcoming_onclick", String.format("id:%s -- season:%s --- Episode:%s ", String.valueOf(id), season, episode));
}
});
...
}

in my case i dont need to check for Null Pointer, becuz the cursor is filled!

alexf