A
A
afna2015-01-20 01:18:15
Android
afna, 2015-01-20 01:18:15

OnClick on view in item listView?

Started creating a custom ListView. An excerpt from the list:
55732073667c430facf12750e9147c25.png
To display an image, I use Android-Universal-Image-Loader
When you click on the "-" or "+" buttons, the animation for the View in the same item is started.
The problem is this:
If I use the ViewHolder pattern, then the button click processing is slow (either after a couple of seconds or after I start scrolling the list).
If I do not use the ViewHolder pattern and in the getView(......) method I inflate every time, then the button click works as it should. But if the list is relatively large, then when scrolling frequently, the program eventually crashes with a Stackoverflow error.
Thought it was the UIL. But the problem remained after I removed this library.
How to get rid of lethargy when pressing buttons?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
one pavel, 2015-01-20
@onepavel

Without your code, it's hard to figure out what needs to be fixed.

I
itdroid, 2015-01-20
@itdroid

The ListView will not redraw the list until you kick it (adapter.notifyDataSetChanged()).
Without code, it can be assumed that the "sluggishness" appears due to the fact that the data has changed immediately after pressing the button but is redrawn only after a while, maybe someone else kicks the ListView or you scroll the content and the view is redrawn.
It’s not worth abandoning ViewHolder just like that, most likely the problem is not in it, as it was written above without code, it’s hard to say something.

A
afna, 2015-01-20
@sulik93

Thanks everyone! I used the ViewHolder pattern a little differently. I apologize for this misinformation.
Here is an excerpt from the code:

class ViewHolder{
  ImageView image, bt_plus, bt_minus;
  TextView tv;
  .......
  View convertView;
}

In the getView(......) method I do the following check:
public View getView(final int position, View convertView, ViewGroup parent) {
  ViewHolder holder = getItem(position);	
        if(holder.convertView == null) {
                 holder.convertView = mInflator.inflate(R.layout.item_list_image_add, parent, false);
                 holder.image    = (ImageView) holder.convertView.findViewById(R.id.image);
                 holder.bt_plus = (Button) holder.convertView.findViewById(R.id.bt_plus);
                 holder.bt_plus.setTag(holder);
                 ............
        }
return holder.convertView;
}

For bt_plus registered in .xml
......
android:onClick="onClickPlus"
......

and added a method to the Activity class:
public void onClickPlus(View v){
    ViewHolder holder = (ViewHolder)v.getTag();
    RelativeLayout rl = (RelativeLayout)v.getParent();
    TextView circle = (TextView)rl.getChildAt(7);
    Button bt_minus = (Button)rl.getChildAt(3);		
    final Button bt_plus = (Button)v;
                ......
}

I realized that there was no need to store an additional convertView in the holder.
With the correct use of the ViewHolder pattern, everything worked well. No errors or slowdowns. The list scrolls more smoothly.
But the question arose: "Why is there a lag when returning holder.convertView in the getView(......) method?"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question