0

I'm asking this because all works good, but I don't understand why is this implemented this way.

I have read how to use convertView here:

What is the purpose of `convertView` in ListView adapter?

But then I read this code: ( link: https://github.com/paraches/ListViewCellDeleteAnimation )

  • ViewHolder - view's tag information object

In deleteCell function, we set needInflate boolean of (ViewHolder) to true, so we provide information that this view can be reused. When we create new one we are setting view tag to false...

Why in second if statement, let say needInflate to be true (that view is open to reuse), in curly braces, we are in new view ? Whethere there should not have been conversely, in second be third (view = convertView;) and vice versa? getView function:

@Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ...

            if (convertView==null) {
                view = mInflater.inflate(R.layout.chain_cell, parent, false);
                setViewHolder(view);
            }
            else if (((ViewHolder)convertView.getTag()).needInflate) {
                view = mInflater.inflate(R.layout.chain_cell, parent, false);
                setViewHolder(view);
            }
            else {
                view = convertView;
            }

        vh = (ViewHolder) view.getTag();
        vh.text.setText(cell.name);

            vh.imageButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                        deleteCell(view, position);
            }
        });

        return view;
    }

EDIT When I do what I will explain above change second and third statement, there is creating some padding or somewhat margin I don't know...

Community
  • 1
  • 1
Jeste
  • 37
  • 10

1 Answers1

0

ListViews can be large. Furthermore, usually a listview's item only changes its content from one another. ie: Each entry in a listview usually has the same UI elements. Consider the following image as a reference.

example listview

As you can see, each cell has

  1. An ImageView
  2. A title TextView (Berlin)
  3. A subtitle TextView (Snowing)
  4. Temperature TextView (0)
  5. Arrow ImageView

From one cell to another, only the values of the cell changes. Even more, at once we don't need to display all the cells. We only need to display whatever should be visible on the screen at the given time.

Creating a view does take some time. When you add have a big list of items (Say 100 items) and you try to create all of them at once, you'll start to notice unresponsiveness of the app.

Considering all these factors, we use a method called "Reusing" views. What we simply do is just as the name suggests. We simply reuse existing view objects. We change only the display values and do not create new views if we can reuse an existing view.

As you know, an adapter's getView method gets called when the listview is drawn. As you should know, a listView's getView method gets called whenever you scroll down the list in addition to the creation time.

So, suppose you scroll down the view. As you scroll down, the first cell (Berlin) disappears from the view. Now, it becomes ready to be reused.

So, whenever you get such a reusable cell, the Android OS would call the getView method and pass that reusable view as the parameter "convertView". So, if you basically get a non-null "convertView", that implies you don't need to inflate a view and you can use and existing view.

Ruchira Randana
  • 4,021
  • 1
  • 27
  • 24
  • Ya man, you are all allright, nice post, and explanation. I have read this in link attached in question... But my question was like u say we need to reuse that "converView" argument, so why in second if statement, we inflate new view? – Jeste Feb 13 '16 at 10:51
  • Ahh. Good point @Jeste. It's because at first, there are no views to reuse. ie: The first time the listview loads, there isn't any views which are reusable. Therefore, you must initiate it the first time. Therefore upon first loading of the listView, you would see that converView is NULL. From then onwards(theoretically), you should be able to see that converView will not be NULL. However, we cannot predict that it will NOT be NULL *always* afterwards. It depends on how the Android OS behaves. Why don't you simply put a Log to see how the converView parameter behaves! – Ruchira Randana Feb 13 '16 at 11:33
  • Can u advice me what to put in Log? BTW there is just one class - activity not big in project, if u have time, try it... – Jeste Feb 13 '16 at 11:45