答案 0 :(得分:1)
您可以为选中的项目自定义ItemTouchHelperViewHolder类并清除拖放:
public interface ItemTouchHelperViewHolder {
void onItemSelected(int adapterPosition);
void onItemClear(int adapterPosition, int position);
}
这是CustomSimpleTouchListner类:
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
// We only want the active item to change
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
if (viewHolder instanceof ItemTouchHelperViewHolder) {
// Let the view holder know that this item is being moved or dragged
ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
itemViewHolder.onItemSelected();
}
}
super.onSelectedChanged(viewHolder, actionState);
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
viewHolder.itemView.setAlpha(ALPHA_FULL);
if (viewHolder instanceof ItemTouchHelperViewHolder) {
// Tell the view holder it's time to restore the idle state
ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
itemViewHolder.onItemClear(fromPos, toPos);
}
}
在Recycleview ViewHolder中扩展此类
public class ViewHolder extends RecyclerView.ViewHolder implements ItemTouchHelperViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
@Override
public void onItemSelected(int adapterPosition) {
itemView.setBackgroundColor(activity.getResources().getColor(R.color.presented_color));
}
@Override
public void onItemClear(int adapterPosition, int position) {
itemView.setBackgroundColor(activity.getResources().getColor(android.R.color.transparent));
}
}
答案 1 :(得分:0)
我没有去过Instagram看你真正想要的东西,但似乎你应该使用setOnTouchListener突出显示特定的单元格。您将收到有关每个单元格的触摸事件的通知。请查看以下链接以获取示例。
答案 2 :(得分:-1)
https://stackoverflow.com/a/35360331/5098486
这个答案可以很容易地修改recyclerView中的选定项目。
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
Item item = items.get(position);
if(selected_position == position){
// Here I am just highlighting the background
holder.itemView.setBackgroundColor(Color.GREEN);
}else{
holder.itemView.setBackgroundColor(Color.TRANSPARENT);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Updating old as well as new positions
notifyItemChanged(selected_position);
selected_position = position;
notifyItemChanged(selected_position);
// Do your another stuff for your onClick
}
});
}