I got to know about the concept of View Holder in ListView, so please check if I understand correctly!

Asked 2 years ago, Updated 2 years ago, 147 views

While searching for keywords related to ListView, I found out about ViewHolder, so I'm going to read related blogs and write down what I understood, so please check if it's right. I would appreciate it if you let me know if there is anything wrong!

First of all, I heard from somewhere that ListView has a lot of items, and I knew that ListView's adapter automatically manages the memory because the device (such as a smartphone) may run out of memory.

The automatic management here is that 1) item view shown on the screen does not need to take up memory if it is not visible on the screen by scrolling, and 2) item view shown by scrolling does not need to take up memory by creating a new one.

However, what I learned while reading the blog this time was that adapter's override method getView (intposition, View convertView, View Group parent) would not be reused unless the convertView of this method was used. (You didn't use convertView at all for what I did with the Toy Project...) -------> I don't know if this is right for now

Now that we're talking about ViewHolder, 1 ViewHolder is not an internal class or interface defined within an adapter or ListView, is it right that developers need to define it separately to optimize ListView?

2 If 1. is correct, I don't understand the principle or structure of why the Viewholder class should be declared static...

Please explain about 3 get, set tag...(I'm looking into this through Google right now, so if you're busy, you can move on!)

If you have any other important keywords related to ListView, please let me know!

The blogs below are the ones I referred to!

http://bellgori.tistory.com/entry/Android-pattern-01-ViewHolder-pattern

http://www.kmshack.kr/2013/09/android-%EC%9C%A0%EC%97%B0%EC%84%B1-%EC%9E%88%EB%8A%94-viewholder-pattern/

http://www.kmshack.kr/2014/08/%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9C-ui%EC%B5%9C%EC%A0%81%ED%99%94-%EB%A6%AC%EC%8A%A4%ED%8A%B8%EB%B7%B0-%EC%84%B1%EB%8A%A5%EC%B5%9C%EC%A0%81%ED%99%94/

android listview

2022-09-22 22:07

1 Answers

1. understanding is correct. In order to draw a list of view without converview that come with the parameters on the getview not use adapter be waste to memory, play each new view. I usually Use the following came when called as getview convertview. They enter at getview convertview has previously drawn over view. Had never be inflate view, to null often be done. check it must be null

public View getView(int position, View convertView, ViewGroup parent)
{
    View customView = convertView;

    if(customView == null)
    {
        LayoutInflater inflater=getLayoutInflater();
        customView  = inflater.inflate(R.layout.noteslist_item, parent, false);
    }
    ...

2. The reason why Viewholder class is static is that if you know Java concept rather than Android concept, you can understand it. The ViewHolder class does not need to be static. ViewHolder classes are usually defined as classes within the class. These classes are called Nested Classes. The reason for using Nested Class is to simply mark code and increase the readability and maintenance of sources. Nested class also uses methods or variables from higher classes. If you attach static, the compiler will fail. No enclosing instance of type error. If you attach static to a nested class, it is called static nested class. Static nested classes are used when it is necessary to logically bundle classes that are used only in one place.
After all, the reason for adding static to the ViewHolder class is to explicitly indicate that you do not want to use member variables or objects in the upper class.

3. The setTag, getTag() function is a method that allows objects to be inserted and imported into View. I will explain the simple code below as an example. Assuming that clickListener is attached to button1, button2 view....

button1.setOnClickListener(new OnClickListener ... );

button2.setOnClickListener(new OnClickListener ... );
 ...

As above, setOnClickListener will be attached to each button. In the onClick method in setOnClickListener, call the action for each view.

public void onClick(View v) {
    DoAction(1); // If you want to put in 1 when button 1 and turn 2 when button 2...
}

In button2 listener, onClick will call doAction(2); However, because calling the method doAction is the same and only the parameter values are different, it seems very inefficient to put the above code in each listener. If you use setTag, you can shorten the code more simply.

button1.setTag(1);
button2.setTag(2);

You can now use the same OnClickListener for all buttons.

listener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        doAction(v.getTag());
    }
};

In this way, if you want to store a value in each view, you can use setTag and getTag to read the value.

4. It's been a while, but have you seen the seminar on Google io's listview? If you haven't, you may want to take a look. (But English is a trap...)

android-world-of-listview-android

world-of-listview-android-video

p.s. If it's a wrong answer or an answer that lacks explanation, please send me a request for editing~


2022-09-22 22:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.