2016-12-23 43 views

回答

0

你可以讓你自定義的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)); 
     } 
    } 
+0

我們必須重寫onSelectedChanged和clearView方法。 –

+0

當您將其擴展到ItemTouchHelper.Callback類'public class CustomItemTouchHelperCallback extends ItemTouchHelper.Callback' –

-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 
     } 
    }); 
} 
0

我還沒有去Instagram看看你真正想要什麼,但它似乎應該使用setOnTouchListener突出顯示特定的單元格。你會得到每個單元觸摸事件的通知。查看下面的鏈接作爲例子。

how to highlight the selected Item of Recycler View?

相關問題