不同布局的RecyclerView正在改变立场

时间:2017-07-28 16:08:37

标签: android android-recyclerview android-adapter

好吧,我有一个带有两种布局类型的RecyclerView,它看起来如下:

public class SearchKeywordRecycler extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

public static final int TYPE = 1, SERVICE = 2;
private ServicesViewHolder serviceViewHolder;
private ServiceTypeViewHolder serviceTypeViewHolder;
private Services services;
private Context context;
private OnSearchItemClickListener OnSearchItemClickListener;

public SearchKeywordRecycler(Services services, OnSearchItemClickListener OnSearchItemClickListener) {
    this.services = services;
    this.OnSearchItemClickListener = OnSearchItemClickListener;
    setHasStableIds(true);
}

@Override
public int getItemViewType(int position) {
    if (services.get(position).getSerficeType() == 1) {
        return TYPE;
    } else if (services.get(position).getSerficeType() == 0) {
        return SERVICE;
    }
    return position;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);
    if (viewType == TYPE) {
        View v = inflater.inflate(R.layout.item_search_service_type, parent, false);
        serviceTypeViewHolder = new ServiceTypeViewHolder(v);
        return serviceTypeViewHolder;
    } else if ((viewType == SERVICE)) {
        View v2 = inflater.inflate(R.layout.item_search_sevice, parent, false);
        serviceViewHolder = new ServicesViewHolder(v2);
        return serviceViewHolder;
    }

    return null;
}

@Override
public long getItemId(int position) {
    return services.get(position).hashCode();
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    Service service = services.get(position);

    switch (getItemViewType(position)) {
        case TYPE:
            serviceTypeViewHolder.setData(service);
            break;
        case SERVICE:
            serviceViewHolder.setData(service);
            break;
        default:
    }
}

@Override
public int getItemCount() {
    return services.size();
}

public interface OnSearchItemClickListener {
    void onClickListener(int position);
}

class ServicesViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView txtViewService;

    public ServicesViewHolder(View itemView) {
        super(itemView);
        txtViewService = ButterKnife.findById(itemView, R.id.service_name);
    }

    public void setData(Service service) {
        txtViewService.setText(service.getService_name());
        if (service.getType_name() != null) {
            txtViewService.setText(service.getType_name());

        }
    }

    @Override
    public void onClick(View v) {
        OnSearchItemClickListener.onClickListener(getAdapterPosition());
    }
}


class ServiceTypeViewHolder extends RecyclerView.ViewHolder {
    TextView txtViewType;

    public ServiceTypeViewHolder(View itemView) {
        super(itemView);
        txtViewType = ButterKnife.findById(itemView, R.id.serviceType);
    }

    public void setData(Service service) {
        String name = service.getType_name();
        txtViewType.setText(service.getService_name());
    }
    }
}

问题是项目&#39;滚动recyclerView时,位置保持改组,我需要在视图中保持其顺序固定。

你可以注意到,我曾经使用过, 1- hasStableIds(true)函数, 2-使用了大多数Satck答案和帖子中建议的getItemId()。

我做错了什么?感谢

1 个答案:

答案 0 :(得分:0)

问题在于,我使用了一个我手工创建的serviceViewHolder实例来执行OnMinderHolder中的setData()函数(ViewHolder holder,int position);

使用适配器本身带给我的holder实例后,一切正常,因为它正在同步与数据数组中检索对象的位置相匹配的holder实例。