我有一个简单的RecyclerView,使用ItemTouchHelper来滑动和拖动项目。一切都很好,直到我需要以不同的样式呈现第一个和最后一个项目,所以我需要在适配器中重写getItemViewType(int p)函数。在我这样做之后,拖动功能就不再流畅,并且总是在移动一个位置后放下项目。这是我的RecyclerView适配器:
public class CurrentStopsAdapter extends RecyclerView.Adapter<CurrentStopsAdapter.ViewHolder> {
private RemoveFromTripListener removeFromTripListener = null;
private SwapPlacesTripListener swapPlacesTripListener = null;
private Context context;
private List<PlaceLink> stops;
public CurrentStopsAdapter(Context context, List<PlaceLink> stops,
RemoveFromTripListener removeListener, SwapPlacesTripListener swapListener) {
this.context = context;
this.stops = stops;
removeFromTripListener = removeListener;
swapPlacesTripListener = swapListener;
}
public interface RemoveFromTripListener {
void onRemoveTripButtonClick(String id);
}
public interface SwapPlacesTripListener {
void onSwapPlacesButtonClick(int first, int second);
}
@Override
public int getItemViewType(int position) {
if (position == (getItemCount()-1)) {
return -1;
}
return position;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case -1:
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.list_item_destination, parent, false));
case 0:
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.list_item_origin, parent, false));
default:
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.list_item_stop, parent, false));
}
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.bindStop(stops.get(position));
}
@Override
public int getItemCount() {
return stops.size();
}
public void remove(int position) {
if (removeFromTripListener != null) {
removeFromTripListener.onRemoveTripButtonClick(stops.get(position).getId());
}
notifyItemRemoved(position);
}
public void swap(int firstPosition, int secondPosition) {
if (swapPlacesTripListener != null) {
swapPlacesTripListener.onSwapPlacesButtonClick(firstPosition, secondPosition);
}
notifyItemMoved(firstPosition, secondPosition);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final TextView placeTitle;
public ViewHolder(View view){
super(view);
placeTitle = (TextView) view.findViewById(R.id.stops_list_item_title);
}
public void bindStop(PlaceLink place){
this.placeTitle.setText(place.getTitle());
}
}
}
我的ItemTouchHelper:
public class TripTouchHelper extends ItemTouchHelper.SimpleCallback {
private CurrentStopsAdapter currentStopsAdapter;
public TripTouchHelper(CurrentStopsAdapter currentStopsAdapter){
super(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
this.currentStopsAdapter = currentStopsAdapter;
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
currentStopsAdapter.swap(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
currentStopsAdapter.remove(viewHolder.getAdapterPosition());
}
}
如果我跳过在getItemViewType上的覆盖并对所有项目使用相同的布局,一切都可以正常工作,但我非常希望找到解决这个问题的方法。任何帮助都将不胜感激 :)