滚动时阻止RecyclerView更新项目

时间:2018-03-13 08:28:37

标签: android android-recyclerview androiddesignsupport

当我将列表向上或向下移动时,我无法停止更新,RecyclerView是重新加载项目..我怎么能阻止它?

示例图片: image 1

滚动时: image 2

i want to stop update and save item state like image 1

<android.support.v7.widget.RecyclerView
    android:id="@+id/list_chat"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/chat_kit"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:descendantFocusability="afterDescendants"
    android:divider="@null"
    android:dividerHeight="0dp"
    android:listSelector="@android:color/transparent"
    android:scrollbars="none"
    android:scrollingCache="false"
    android:animationCache="false"
    android:transcriptMode="normal" />

3 个答案:

答案 0 :(得分:0)

您只需要在RecyclerView对象上调用此方法。

minmax_element()

setMaxRecycledViews(itemViewType,max)第一个参数是您的recyclerView的适配器itemViewType,默认为0,第二个参数是必须回收的视图数量)

答案 1 :(得分:0)

这里是类型:

private static final int VIEW_TYPE_MESSAGE_SENT = 1;
private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;
private static final int VIEW_TYPE_AUDIO_SEND = 3;
private static final int VIEW_TYPE_AUDIO_RECEIVED = 4;
private static final int VIEW_TYPE_VIDEO_SEND = 5;
private static final int VIEW_TYPE_VIDEO_RECEIVED = 6;
private static final int VIEW_TYPE_IMAGE_SEND = 7;
private static final int VIEW_TYPE_IMAGE_RECEIVED = 8;
private static final int VIEW_TYPE_MAP_RECEIVED = 9;
private static final int VIEW_TYPE_MAP_SEND = 10;

这里是适配器代码getViewType

@Override
public int getItemViewType(int position) {
    Object object = mMessageList.get(position);
    if(object instanceof Messages){
        Messages messages = (Messages)object;
        if (messages.getType().equals("send")) {
            // If the current user is the sender of the message
            return VIEW_TYPE_MESSAGE_SENT;
        } else {
            // If some other user sent the message
            return VIEW_TYPE_MESSAGE_RECEIVED;
        }
    }else if(object instanceof AudioMessage){
        AudioMessage audioMessage = (AudioMessage)object;
        if(audioMessage.getType().equals("send")){
            return VIEW_TYPE_AUDIO_SEND;
        }else{
            return VIEW_TYPE_AUDIO_RECEIVED;
        }
    }else if(object instanceof VideoMessage){
        VideoMessage videoMessage = (VideoMessage)object;
        if(videoMessage.getType().equals("send")){
            return VIEW_TYPE_VIDEO_SEND;
        }else{
            return VIEW_TYPE_VIDEO_RECEIVED;
        }
    }else if(object instanceof ImageMessage){
        ImageMessage imageMessage = (ImageMessage)object;
        if(imageMessage.getType().equals("send")){
            return VIEW_TYPE_IMAGE_SEND;
        }else{
            return VIEW_TYPE_IMAGE_RECEIVED;
        }
    }
    else if(object instanceof MapMessage){
        MapMessage mapMessage = (MapMessage)object;
        if(mapMessage.getType().equals("send")){
            return VIEW_TYPE_MAP_SEND;
        }else{
            return VIEW_TYPE_MAP_RECEIVED;
        }
    }else{
        throw new IllegalArgumentException("Error get item type on ChatRecyclerViewAdapter");
    }
}

我用过:

recyclerView.getRecycledViewPool().setMaxRecycledViews(0, 0);

这是地图类:

public class MapMessage implements GoogleMap.OnMapClickListener{
private Context context;
private String type;
private Double a;
private Double b;
private MapView mapView;
private ProgressBar progressBar;
private String name;
private boolean isLoaded = false;
public MapMessage(Double a, Double b, String type){
    this.a = a;
    this.b = b;
    this.type = type;
}

public String getType() {
    return type;
}

public void setContext(Context context) {
    this.context = context;
}

public void setName(String name) {
    this.name = name;
}

public void setProgressBar(ProgressBar progressBar) {
    this.progressBar = progressBar;
}

public MapMessage(){}

public void setMapView(MapView mapView) {
    this.mapView = mapView;

}

public void setA(Double a) {
    this.a = a;
}

public void setB(Double b) {
    this.b = b;
}

public Double getA() {
    return a;
}

public Double getB() {
    return b;
}

public void load(){
    progressBar.setVisibility(View.VISIBLE);
    mapView.onCreate(null);
    mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
            googleMap.getUiSettings().setZoomControlsEnabled(false);
            googleMap.getUiSettings().setZoomGesturesEnabled(false);
            googleMap.getUiSettings().setScrollGesturesEnabled(false);
            googleMap.getUiSettings().setMyLocationButtonEnabled(false);
            googleMap.getUiSettings().setAllGesturesEnabled(false);
            googleMap.getUiSettings().setMapToolbarEnabled(false);
            googleMap.getUiSettings().setRotateGesturesEnabled(false);
            googleMap.setOnMapClickListener(MapMessage.this);
            LatLng bind = new LatLng(a, b);
            googleMap.addMarker(new MarkerOptions().position(bind).title(""));
            progressBar.setVisibility(View.GONE);
            googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(bind, 13.0f));
            if(!isLoaded){
                mapView.onResume();
                isLoaded = !isLoaded;
            }
            mapView.postInvalidate();
        }
    });
    //mapView.postInvalidate();

}


@Override
public void onMapClick(LatLng latLng) {
    if(context == null)return;
    name = name == null? "": name;
    String strUri = "http://maps.google.com/maps?q=loc:" + a + "," + b  + " ("+name+")";
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(strUri));
    intent.setPackage("com.google.android.apps.maps");
    context.startActivity(intent);
}

}

这里绑定地图视图时:

private class MapHolder extends RecyclerView.ViewHolder {
    private MapView mapView;
    private ProgressBar progressBar;
    MapHolder(View view){
        super(view);
        this.mapView = view.findViewById(R.id.map_location);
        this.progressBar = view.findViewById(R.id.load_map);
    }
    public void bind(MapMessage mapMessage){
        mapMessage.setProgressBar(progressBar);
        mapMessage.setMapView(mapView);
        mapMessage.setContext(mContext);
        mapMessage.load();

    }
}

 else if(object instanceof MapMessage){
        MapMessage mapMessage = (MapMessage)object;
        ((MapHolder)holder).bind(mapMessage);
    }

答案 2 :(得分:0)

一种方法是将图像的Alpha值设置为0.9999f(基本上为1),然后添加if语句以仅在图像不是Alpha值为0.9999f时加载图像。

 if (holder.profileImage.getAlpha() != 0.9999f) {
        holder.profileImage.setAlpha(0.9999f);
        //set image
       
    }