MTC MTC
MTC MTC

Reputation: 11

Custom Listview laggy when there's an if statement

I have an custom list view. it is laggy when if comes to the conditional statements part

public View getView(int position, View convertView, ViewGroup parent) {
        View caloocanView = convertView;
        ViewHolder holder = new ViewHolder();
        if (caloocanView == null)

            caloocanView = getLayoutInflater().inflate(R.layout.caloocan_list_view, parent, false);
        restauInfoDB restaurant = Restau.get(position);
        //ImageView from URL
        holder.restauIcon = (ImageView) caloocanView.findViewById(R.id.imageView);
        Glide.with(getContext()).load(restaurant.getUrl()).centerCrop()
                .placeholder(R.drawable.six)
                .crossFade()
                .into(holder.restauIcon);


        String x = "N";
        Double hotCTR = 15.0;
        String New = "N";


        holder.healthyIcon = (ImageView) caloocanView.findViewById(R.id.healthyIcon);
        //HEALTHY
        if (x.equals(restaurant.getHealthy())) {

            holder.healthyIcon.setVisibility(View.INVISIBLE);
        }
        else
        {
            holder.healthyIcon.setVisibility(View.VISIBLE);
        }

        //NEW
        if (New.equals(restaurant.getNew())){
            holder.newLabel = (ImageView) caloocanView.findViewById(R.id.newLabel);
            holder.newLabel.setVisibility(View.INVISIBLE);
        }
        else {
            holder.newLabel = (ImageView) caloocanView.findViewById(R.id.newLabel);
            holder.newLabel.setVisibility(View.VISIBLE);
        }
        //HOT
        if (hotCTR <= Double.valueOf(restaurant.getRating())) {
            holder.hotIcon = (ImageView) caloocanView.findViewById(R.id.iconHot);
            holder.hotIcon.setVisibility(View.VISIBLE);
        }
        else
        {
            holder.hotIcon = (ImageView) caloocanView.findViewById(R.id.iconHot);
            holder.hotIcon.setVisibility(View.INVISIBLE);
        }



        String serving = "Serving: ";
        // RESTAU NAME
        holder.restauName = (TextView) caloocanView.findViewById(R.id.resnameTxt);
        holder.restauName.setText(restaurant.getResname());
        //FOOD TYPE

        holder.oh = (TextView) caloocanView.findViewById(R.id.ophrTxt);
        holder.oh.setText("Operating Hours: " +restaurant.getOh());



        holder.resloc = (TextView) caloocanView.findViewById(R.id.reslocTxt);
        holder.resloc.setText(restaurant.getResloc());
        return caloocanView;



    }`

and this is the static viewHolder

static class ViewHolder
{
    ImageView restauIcon;
    ImageView healthyIcon;
    ImageView newLabel;
    ImageView hotIcon;
    TextView restauName;
    TextView oh;
    TextView resloc;
}

Upvotes: 0

Views: 68

Answers (2)

koceeng
koceeng

Reputation: 2163

Try adding this line to your RecyclerView layout xml:

android:nestedScrollingEnabled="false"

It may be not affect older versions of Android, but in newer versions the lag is completely gone.

EDIT:by RecyclerView, I mean ListView

Upvotes: 0

Farid
Farid

Reputation: 1096

you are missing the point of using ViewHolder,the point is to minimize inflating new layouts and finding views in layout which are time consuming and make view laggy,but you are calling findViewById() every time.

try this code instead:

            View caloocanView = convertView;
            ViewHolder holder = new ViewHolder();
            if (caloocanView == null) {
                caloocanView = getLayoutInflater().inflate(R.layout.caloocan_list_view, parent, false);
                holder.restauIcon = (ImageView) caloocanView.findViewById(R.id.imageView);
                holder.healthyIcon = (ImageView) caloocanView.findViewById(R.id.healthyIcon);
                holder.newLabel = (ImageView) caloocanView.findViewById(R.id.newLabel);
                ...
                caloocanView.setTag(holder);
            }else{
                holder = (ViewHolder) caloocanView.getTag();
            }
            ...

            //HEALTHY
            if (x.equals(restaurant.getHealthy())) {

                holder.healthyIcon.setVisibility(View.INVISIBLE);
            }
            else
            {
                holder.healthyIcon.setVisibility(View.VISIBLE);
            }

and reformat rest of your code

Upvotes: 1

Related Questions