Android RecyclerView Step by Step Tutorial (Part 3) Custom Adapter - 2016

Android RecyclerView Step by Step Tutorial (Part 3) Custom Adapter - 2016 - Hello friends Android tutorial and tips trik, The article you are reading this time with the title Android RecyclerView Step by Step Tutorial (Part 3) Custom Adapter - 2016, we have prepared this article for you to read and download the information therein. hopefully fill posts article -, article (Part, article #3, article 2016, article Adapter, article android, article by, article custom, article RecyclerView, article Step, article Tutorial, we can write this you understand. Well, happy reading.

Title : Android RecyclerView Step by Step Tutorial (Part 3) Custom Adapter - 2016
link : Android RecyclerView Step by Step Tutorial (Part 3) Custom Adapter - 2016

Baca juga


Android RecyclerView Step by Step Tutorial (Part 3) Custom Adapter - 2016

Hello everyone, my name is Oum Saokosal, sothis is the part three of the RecyclerView.

This is the most important part.

We talked about the custom adapter which isthe core part of the RecyclerView and if you just come to this field, you know, for thefirst time, I would like to suggest you to go to my channel to watch the first and secondand please don't forget to subscribe my YouTube channel and also like my Facebook, followmy Twitter and, you know, follow my Github if you have the Github.

So in the first and second video, I was talkingabout this one and this part.

Ok one, I talked about the card view and therecycler and part 3 I talked about 1 2 3 and 6th, so leave the part 5 here.

So just go to the java file, but before wewant to, you know, like load the image from internet, on the website.

So, we have to be careful here.

Anytime that you see this, it means that you'regoing to connect to internet network, so you have to request the internet mission, youhave to go to android manifest here and then here it says user permission and internet,just type internet like that and enter, ok! So that means that you can use internet now,ok! So we're going to make the adapter here.

This is the example here, so we have to createanother class.

Usually if you used to work with ListView,you can create an internal class for the adapter, but it is going to have some difficulty becausesome code is going to be messy.

I forget which one, but maybe the ViewHolderhas some problems.

Yeah, Yeah! This cannot be declared static; you declarethe internal class, so we have to create it outside.

So, right click new and so now you want tocall something that meaningful to in your project, so this one is going to be ItemAdapter,so we're going to make something for the item and then you have to extend the RecyclerViewadapter and here is the ViewHolder.

Ok, so we have to create a ViewHolder first,so, Public static class_ViewHolder extends RecycleView.

ViewHolder You just click on this one, it is going tobe red light there and you're going to create here, so this is like mandatory for that.

It is a must to have the instructor insidethe ViewHolder here and then you can use the ViewHolder here now, but, be careful! There are two names for the ViewHolder here.

This one is ViewHolder 1st.

So we have to make sure that it is insidethe data ItemAdapter.

If you afraid to be confusing, I think youhave to declare different name, maybe item ViewHolder like this.

When you type for example, item ViewHolder,it is going to add something like that, class for you like this.

Now, click on this and click the red lightagain, implement, ok.

So you need another three messages and thisone is like the name must be the same to the ViewHolder.

So, this one is the ViewHolder class, ok,and this one is the other mention for the ItemAdapter.

So, you can create your own constructor here.

It is optional.

You can create one parameter or one or twoparameters, whatever you want, but typically you're going to create two constructors withtwo parameters, ok.

So a constructor must have the name exactlyto the class name ItemAdapter.

So the 1st one needs context.

Context means the class here belongs to whichactivity.

So you are going to pass in the main activityinto the ItemAdapter here.

So context here and then the data.

Now the data is ArrayList, so it is goingto be ArrayList of item and then just call ItemList.

So, we have to declare a field outside andthis time it is just usual, you know, ______ for java.

So it is going to be context and so contexthere is and like the dark red here is the old here okay, and then Items, so privatearray item and then ItemList.

If you want to, you know, some like in Androiddocument, you are going to put like �M� contact like that, but it is okay, just useit like that, it is fine.

Now, you can now get the data from outside;just mean that now you can declare array ItemAdapter like this okay and just call Adapter new ItemAdapterand you are passing this and then the ItemList from outside.

Now, this is the way you pass in the contexthere.

If you are using in maybe in another internalmethod or internal class, it's better to use like this.

Get application context is going to help you.

If you use this, then sometimes it is goingto have some error because you are going to use it inside the inner class.

Now, it's okay, you can just put into theadapter here okay, but it's not going to work because you didn't buy it, okay, like whichone to, something like this, yeah� You have the data here, the text here andyou want to put into where, okay! It's not going to magically happen to you,you have to tell it okay.

It's time to go to something over again andimage and go to this one, and maybe I don't want to use that.

So just like, maybe some light or maybe likea flashlight or whatever light you say, like something that you have to wire internallyand then plug into the user interface okay.

So you're going to tell them, so yeah, thisis where you want to tell them, okay.

First you need to get the count here, likeyou put it like 50 items, but how many item you want to display.

You want to display all of it? Ok, so then just call item size.

You use all the items here, but here if youdidn't put in the data, maybe you put the null here, you're going to have some errorover here, so you have to figure it out before it happen, so it good practice to check itif ItemList is, you know like, is not equal to null, then you're going to return somethinglike this, okay.

But if null, it is going to return 0.

If you don't do that, you're going to returnnull okay, because the item here is null, then there is no way to tell, so we're goingto make your ______.

If you want to say okay, I want to use halfof it, may be just like this, okay.

Let's say we want to use all of it.

Now, the item okay, and we have to take careof the ViewHolder here, so where is the ViewHolder class? Where is it? This one is not a good example because itis just one tag, but let me check with my previous one.

I got it from another blog as well, but there'sa quick way.

Just checking my previous example.

I always forget the ViewHolder.

You have to get it from the layout here.

So use find your either by ID or here, okay,let's remember that.

I hope that i will not forget it.

Ha ha �.

So, let's declare it as public okay, becausenow we use that the statics always ok to use public and then now we have to match the Itemhere to the layout.

So where is the layout here? Here!.

Item card, okay so now you're going to usethis and this.

You want to use the CardView.

It is okay, you can use that.

So, let's declare it.

CardView and we call it cvItem, right! Okay cvItem, IvMain.

So now:public ImageView IvMainImage; public TextView tvTitle; The name here it does matter, it doesn't haveto be the same out there, you can just say to any name, okay, but a good practice isto you know have a similar name or maybe exactly the same name so that you would not forgetit.

So let's change the text and IMG, so let'sdo it.

Okay here IvImg and this one to tvText.

This one IvImg and tvText.

Okay here, you call this cvItem and use theCardView and then use the ItemView, findViewByID and then cvItem, and you will need to justcopy and paste it, and this one is IvImg and this one is tvText, okay.

It is good now! Now you finish the ViewHolder.

If you don't understand the ViewHolder here,let me explain it just a little bit.

When you have a lot of items here, when youscroll down, what you're going to do with the item that disappear, okay.

Let's say you have like 50 items and one itemyou're going to have like three images or maybe four or five images, so maybe a windowinside, like on Facebook you know, so it's going to have a lot of memory to store alldata here and so the ViewHolder here is like a design pattern that Android created liketrying to protect the memory leak for any type item that you use it just exactly thesame item even though you have 1,000 items, but the class here just only one, that's whyit is static okay, you know, static class happen only one in the memory.

That's why when you have like 1,000 items,it doesn't matter, you're not going to crush any application.

This is the idea behind it and I think youmay want to look at the Android ViewHolder pattern.

It's just maybe the name title is different now, so this one is for the old, but the document here is stillrelevant.

You might want to take a look at it.

So basically, just like try to protect thememory leak, you know, if you just want to retain a document, that's all! Ok now you have the item ViewHolder, it islike a holder and now you want to link from the adapter here to the Item Card view layout,okay, because now you don't know which layout.

You can link to any layout.

You can link to this, you can link to that,but if you link it to the wrong one, you're going to have some error and also in the futureyou can link to different CardView, you know, like many CardViews can happen in the sameItem ID Adapter.

Like I said, you can have multiple layouts,for the first one you're going to have like three and one or two, whatever we want.

That is the beauty of it, but before you makeit beauty, a little bit dirty, you know.

So let's go back to the document again.

Here is the code.

You need only this, but first I want to usethis only.

This inside layout here and this one you'regoing to put into maybe layout _____ something.

Is it current, yes! The reason that I used this, I don't do likethis because in the future, we're going to inflate different views, different layouts.

Like I said you can have multiple layoutshere.

So, I just reserved the code for the future.

Also you can download the source code in thisvideo below, okay, and then that is the code, we can use just inflate.

So get context and we can just copy, but changesthis one to this, okay and this one is the layout, so you want to change to the ItemCard,okay that's all.

That's all you need to do.

Now you can link to that or maybe you caninflate the layout to the adapter now and then the last code here, you call the ViewHolderhere okay.

So now, our ViewHolder is ItemViewHolder andshould keep the name ItemViewHolder and then put the view inside that and then we can returnthat.

The whole idea is like you going to inflatethe item current view layout that has two items inside or maybe two views if you wantyou may view and the text okay and you inflate it and then this one is kind of like the specialclass to hold in and we are going to call it.

Then the ViewHolder here, when you see thatinside the ItemViewHolder constructor, we have this as the default parameter, so nowyou put the view that has the layout.

For the layout, go to hear now.

It knows that this one belonged to this layout.

Three items here belong to the item_cardview_layoutokay.

That is the whole story and when you returnthem, this will go to here okay.

Return this to this okay, so this link together.

You know, it is like an observable pattern.

Now, this place is the class that you wantto bind.

You want to bind like, okay, this one willgo to this, this one will go to that, this one will go to that�okay! It is the final step.

So let's see the document again.

Now the holder.

This one is the holder.

Now the holder you call the IvImg okay andthen IvImg view set the image, but the image now is not from the internal text, it's fromoutside, so you need another library and usually I use Picasso.

So, let's use Picasso, okay.

square.

Github.

Io/PicassoPicasso is very simple.

What you have to do 1st is just to add thiscode, the GRADLE, okay and go to the second GRADLE here like this and you need to syncwith the internet also and then the code�.

Let me explain a little bit, you need thecontext, you need the URL and you need the ImageView.

That's all, but if you want something betterlike you have the placeholder, like something error, so I suggest you use this one and ifyou want to do cropping, you know, use this one more but this one �.

So, you go back and that's it like this.

Now this one is going to be something thatwe have inside here.

Remember we had the picture you asked beforeone, so I am going to use that and error, maybe I use the default from Android, so whenwe call Android like that, it means that we are going to use the internal image.

Maybe stat error.

Okay, you can see a little icon here in errorand the ImageView now is the holder.

IvImg, okay.

This is the image that you get from here,right! So where is the URL.

So let's see this one.

The URL is that you get from the dataset,so where is our dataset? Here is our dataset, the ItemList okay.

So, there are many items right, so I wantto get only one item, so I call just maybe item and then ItemList and get from whichposition.

This is going to come with this one, okay.

So it's very beautiful, ha ha �Yeah, it is.

It sound crazy but actually if you used itwith ListView, this kind of thing you have to create by yourself, you know, but thisone is gonna like okay, I know the position and you're going to use the position to getwhich item okay.

Okay, now the item here is the image, theIMG.

Where is the IMG came from? The IMG came from here and the data is fromhere.

So in each item, like item 0, 1, 2, till 49okay.

We are going to add 1, 1, 1, and 2 hear.

This one for image and for the holder againand for the text right.

It is going to be very easy, just set thetext because there is no special library for that, just call item and text, okay.

That's all and if you want to use the ID,you can use that.

So, let's run it but because we didn't buildit yet, so I suggest you to rebuilt okay.

This is a very important step for the androidstudio too because it has the new feature to like a lightning running but sometime whenyou change a lot of code, like change everywhere like us, you're going to have some problem.

It is going to use the previous cache okay.

So, we have to rebuild it and when we runit, it is going to take some time and I use Genymotion and use the Note 2 okay.

It is going built it and hopefully going tosee the image from the internet from here and the image is going to be different likethe rest of the item, you are going to see this image and the dummy text like that, likethe 3, 6, 9, going to see some here, 7 and 11 we are going to see different images okay.

Okay, it is almost done.

What is the time now? So 27 minutes, so 30 minutes is going to befine for this video.

Oh, maybe we have done something Adapter;no we have the Adapter, but this one, why you see the Hello World? Yeah, oh did I? What! Oh, is it the real one? I forget which one! This one is from the previous one, right? Okay, so okay, it is the correct one, butwhy you see�.

there is no hello world because i deleted,yeah, I deleted so I should not see this one� Maybe, I have to run it again! One more time.

You take so long; let me pause the video alittle bit.

Ok, now you see that.

So this is the, you know, the default one.

Now this is the real image from the internet.

So, this is the, you know, like the rest ofthe images I think, whatever but you got to see like four images.

Yes! I want to see the #7, yeah!.

No, this one is before one, yes.

7, see that? I want to see another one, 11, yes, okay.

Now you will see all the images, okay, andif you want to change, you know, like here, you can change the padding; you can changeanything inside you content main, you know, like here.

This is the radius.

You want to change to #0, then have the roundedrectangle, just change to zero, you are going to see the rectangle, okay, but I leave itlike this for you, okay, because it is not that difficult.

You can change the image, the text, but thecool part now is that we have done it.

This is the very very core, okay, the adapter.

Let me summarize then.

First you need to create a class, a separateclass for that and then you need to extend the adapter from the RecyclerView and youneed to use the generic, you know, to get the ViewHolder and then inside that you canuse the ViewHolder.

It doesn't mean that you can create outside,but it's very good practice that usually people create the ViewHolder inside, but you canfreely create the ViewHolder outside, okay, if you want to, but some data is going tobe different and the code is going to be different for me or mine actually.

So, this one is a constructor.

You're going to have like a context, but somepeople don't like the constructor.

They are going to have, you know, like theywant to like this.

Like adapter and then you got to set the contextoutside and then the setdata outside because you want to change the data on the fly, okay,like running, then you can change code.

That's why the adapter gives you the freedomto do that.

You can have a constructor for yourself ormaybe you get to set and get the data, okay, but there are three methods that you haveto implement.

The first one is the item count and the datafrom here is going to use for the counting, how many items, and then this one is goingto be something like you want to get the layout, okay, you want to inflate the layout and thisone for binding, you pluck which data to which data, which data to which data okay.

That's all okay and I will see you in thevideo about how to get data from the, you know, like from database server using maybe______ because many many people request that, so if you are looking for the video aboutthat, please subscribe my channel again okay, and do not forget to like my Facebook.

Thank you.



Thus Article Android RecyclerView Step by Step Tutorial (Part 3) Custom Adapter - 2016

That article Android RecyclerView Step by Step Tutorial (Part 3) Custom Adapter - 2016 This time, hopefully we can give you all of the benefits. Well, I'll see you in another article post.

You now read the article Android RecyclerView Step by Step Tutorial (Part 3) Custom Adapter - 2016 the link address http://svedda79.blogspot.com/2016/05/android-recyclerview-step-by-step.html

0 Response to "Android RecyclerView Step by Step Tutorial (Part 3) Custom Adapter - 2016"

Post a Comment