当有if语句时,自定义Listview滞后

时间:2016-11-26 04:01:47

标签: java android listview

我有自定义列表视图。 如果来到条件陈述部分

,那就太迟了
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;



    }`

这是静态viewHolder

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

2 个答案:

答案 0 :(得分:1)

你错过了使用ViewHolder的重点,重点是尽量减少新布局的膨胀,并在布局中查找耗时且视图滞后的视图,但每次都在调用findViewById()

尝试使用此代码:

            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);
            }

并重新格式化代码的其余部分

答案 1 :(得分:0)

尝试将此行添加到RecyclerView布局xml:

set( 1, 2, 3, 4)

它可能不会影响旧版Android,但在较新版本中,滞后已完全消失。

编辑:通过RecyclerView,我的意思是ListView