2017-07-28 82 views
0

好吧,我有兩個佈局類型一RecyclerView,它看起來像如下: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()); 
    } 
    } 
} 

的問題是項目的立場保持滾動時的recyclerView洗牌,我需要在視圖中保持固定的順序。

正如你可以注意到的,我確實使用過,在絕大多數Satck答案和帖子中都使用了1- hasStableIds(true)函數, -used getItemId()。

我在做什麼錯?謝謝

回答

0

問題是,我用一個我手動創建的serviceViewHolder實例來執行OnMinderHolder(ViewHolder holder,int position)中的setData()函數;

使用由適配器本身帶給我的持有者實例後,一切正常,因爲它正在同步持有者實例,匹配用於從數據數組中檢索對象的位置。