Android Listview滚动滞后与视图持有人

时间:2017-07-14 05:42:26

标签: android listview android-volley

我注意到我的应用程序中的ListView突然开始变得非常严重。

我正在使用Volley为我的listview项目加载图像 - 下载和缓存图像很好并且滚动顺畅如黄油。

但是,当我等待图像加载时,我当前有一个位于NetworkImageView顶部的微调器。一旦图像成功加载,就会出现延迟 - 我将微调器设置为不可见,并将图像设置为可见。改变这些项目的可见性似乎是滞后的原因。

上午目前正在使用View Holder模式,我的onResponseLoad如下所示:

            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                if (response.getBitmap() != null){ //Check that the image is not null
                    ProgressBar progress = holder.getProgress(); //Find Spinner - this doesnt cause lag
                    progress.setVisibility(View.GONE); //Hide spinner (This causes lag)
                    img.setVisibility(View.VISIBLE); //Image is a network image from the holder (This causes lag)
                }
            }

(请注意,注释掉这两条违规行会导致黄油再次平滑滚动)

另一个奇怪的事情是我在一段时间内没有触及应用程序的这一部分,而且在我目前的实时版本中,以及之前的提交没有任何延迟。将我当前的代码库与之前的非滞后版本进行比较表明,围绕应用程序这一方面的代码已经发生了0次更改。此外,我使用几乎完全相同的技术实现的其他列表没有遇到过这个问题。

我能想到的唯一不同的是,我现在正在使用最新版本的Gradle - 尽管我认为这不应该在运行时产生影响。

我完全不知道发生了什么,我很欣赏我应该做些什么来实现顺畅的ListView滚动(或者可能导致我的实现和退化)

编辑:按要求发布getView()代码

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View placeSelectorView = convertView;
    PlaceViewHolder placeSelectorHolder = null;

    if(placeSelectorView == null){ //If we are creating the row for the first time
        LayoutInflater inflater = (LayoutInflater) mCtx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //Inflate the view

        placeSelectorView = inflater.inflate(R.layout.place_selector, parent, false); //Get the view

        placeSelectorHolder = new PlaceViewHolder(placeSelectorView); //Create holder object
        placeSelectorView.setTag(placeSelectorHolder); //Attach reference to the view
    }else{
        placeSelectorHolder = (PlaceViewHolder) placeSelectorView.getTag(); //Load holder from memory
        if(!placeSelectorHolder.isHasImage()){ //Need to optimise this
            placeSelectorHolder.getLayout().addView(placeSelectorHolder.getrLayoutThumbnail(), 0);
            placeSelectorHolder.setHasImage(true);
        }

        if(!placeSelectorHolder.isSpinnerVisible()){
            placeSelectorHolder.getProgressBar().setVisibility(View.VISIBLE);
            placeSelectorHolder.getPlaceImg().setVisibility(View.GONE);
            placeSelectorHolder.setSpinnerVisible(true);
        }
    }

    POI place = (values.get(position)); //Get POI object for the place
    POI parentPlace = getParent(place); //Get parent POI for place
    placeSelectorHolder.getPlaceName().setText(place.getName());


    if(parentPlace != null){ //If place has a parent POI
        placeSelectorHolder.getParentPlaceName().setText(parentPlace.getName());
    }else{ //We don't want the parent text in the view
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) placeSelectorHolder.getParentPlaceName().getLayoutParams();
        layoutParams.weight = 0; //Setting weight to 0 will remove it from the LinearLayout
        placeSelectorHolder.getParentPlaceName().setLayoutParams(layoutParams);
    }

    final PlaceViewHolder holder = placeSelectorHolder;
    loadThumbnail(holder, place);

    return placeSelectorView;
}

public void loadThumbnail(final PlaceViewHolder placeSelectorHolder, POI place){
    RealmList<poiPhoto> photos = place.getPhotos();
    String mUrl;

    if(!photos.isEmpty()){
        mUrl = photos.get(0).getSmall();
    }else{
        mUrl = "";
    }

    final NetworkImageView placeImg = placeSelectorHolder.getPlaceImg();
    if(!mUrl.equals("")){ //If there is an Image Available
        ImageLoader imageLoader = ServerSingleton.getInstance(getContext()).getImageLoader(); //Get volley imageloader from Singleton
        imageLoader.get(mUrl, new ImageLoader.ImageListener() { //Custom get so we can use override onResponse and OnErrorResponse

            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                if (response.getBitmap() != null){ //Check that the image is not null
                    ProgressBar progressBar = placeSelectorHolder.getProgressBar(); //Find Spinner
                    placeImg.setVisibility(View.VISIBLE);
                    if(progressBar != null) progressBar.setVisibility(View.GONE); //Make the spinner invisible
                    placeSelectorHolder.setSpinnerVisible(false);
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                //TO-DO: Get an error image
            }
        });

        placeImg.setImageUrl(mUrl, imageLoader); //Send the request
        placeSelectorHolder.setHasImage(true);
    }else{ //There is no image
        LinearLayout layout = placeSelectorHolder.getLayout(); //Find the horizontal layout
        layout.removeView(placeSelectorHolder.getrLayoutThumbnail()); //Remove the Thumbnail layout
        placeSelectorHolder.setHasImage(false);
    }
}

0 个答案:

没有答案