2016-10-05 3 views
0

У меня есть ящик для фрагментации фрагмента с RecyclerView и некоторые другие представления в нем. Всякий раз, когда я накладываю палец на элемент в recyclerview, он не отображает выбранный цвет состояния. Хотя я добавил все необходимые коды.Фрагмент ящика RecyclerView Элемент не получает эффект пульсации

RecyclerView Item Row:

<LinearLayout android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:paddingTop="15dp" 
android:paddingBottom="15dp" 
android:paddingEnd="10dp" 
android:paddingLeft="10dp" 
android:paddingRight="10dp" 
android:paddingStart="10dp" 
android:clickable="true" 
android:focusable="true" 
android:background="?android:attr/selectableItemBackground" 
android:id="@+id/layout" 
android:orientation="horizontal" 
xmlns:android="http://schemas.android.com/apk/res/android"> 



<ImageView 
    android:layout_width="26dp" 
    android:layout_height="26dp" 
    android:layout_marginLeft="10dp" 
    android:layout_marginStart="10dp" 
    android:layout_gravity="center_vertical" 
    android:id="@+id/icon"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="15dp" 
    android:layout_marginStart="15dp" 
    android:textSize="16sp" 
    android:layout_gravity="center_vertical" 
    android:id="@+id/title"/> 

    </LinearLayout> 

RecyclerView адаптер:

List<NavDrawerItems> data = Collections.emptyList(); 
private LayoutInflater inflater; 
private Context context; 
int selectedPosition = 0; 


public NavigationDrawerAdapter(Context context, List<NavDrawerItems> data) { 
    this.context = context; 
    inflater = LayoutInflater.from(context); 
    this.data = data; 
} 

public void delete(int position) { 
    data.remove(position); 
    notifyItemRemoved(position); 
} 

@Override 
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = inflater.inflate(R.layout.nav_drawer_row, parent, false); 
    return new MyViewHolder(view); 
} 

@Override 
public void onBindViewHolder(MyViewHolder holder, int position) { 
    if(selectedPosition == position){ 
     holder.layout.setBackgroundColor(context.getResources().getColor(R.color.grey_efef)); 
     holder.title.setTextColor(context.getResources().getColor(R.color.dark_red)); 
     holder.icon.setColorFilter(context.getResources().getColor(R.color.dark_red)); 
    } else { 
     holder.layout.setBackgroundColor(context.getResources().getColor(R.color.white)); 
     holder.title.setTextColor(context.getResources().getColor(R.color.black)); 
     holder.icon.setColorFilter(context.getResources().getColor(R.color.black)); 
    } 
    NavDrawerItems current = data.get(position); 
    holder.title.setText(current.getTitle()); 
    holder.icon.setImageResource(current.getIcon()); 

} 



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

class MyViewHolder extends RecyclerView.ViewHolder { 
    TextView title; 
    ImageView icon; 
    LinearLayout layout; 

    public MyViewHolder(View itemView) { 
     super(itemView); 
     title = (TextView) itemView.findViewById(R.id.title); 
     icon = (ImageView) itemView.findViewById(R.id.icon); 
     layout = (LinearLayout) itemView.findViewById(R.id.layout); 
    } 
} 

Если удалить этот код из адаптера RecyclerView, он отлично работает:

if(selectedPosition == position){ 
     holder.layout.setBackgroundColor(context.getResources().getColor(R.color.grey_efef)); 
     holder.title.setTextColor(context.getResources().getColor(R.color.dark_red)); 
     holder.icon.setColorFilter(context.getResources().getColor(R.color.dark_red)); 
    } else { 
     holder.layout.setBackgroundColor(context.getResources().getColor(R.color.white)); 
     holder.title.setTextColor(context.getResources().getColor(R.color.black)); 
     holder.icon.setColorFilter(context.getResources().getColor(R.color.black)); 
    } 

Но этот код используется для отображения текущего выбранного элемента в режиме recyclerview. Как я могу реализовать их оба.

ответ

1

Я решил эту проблему, изменив код элемента recyclerview.

<LinearLayout android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/layout" 
android:orientation="horizontal" 
xmlns:android="http://schemas.android.com/apk/res/android"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:paddingTop="15dp" 
    android:paddingBottom="15dp" 
    android:paddingEnd="10dp" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:paddingStart="10dp" 
    android:clickable="true" 
    android:focusable="true" 
    android:background="?android:attr/selectableItemBackground"> 

    <ImageView 
     android:layout_width="24dp" 
     android:layout_height="24dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginStart="10dp" 
     android:layout_gravity="center_vertical" 
     android:id="@+id/icon"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="15dp" 
     android:layout_marginStart="15dp" 
     android:textSize="16sp" 
     android:layout_gravity="center_vertical" 
     android:id="@+id/title"/> 

    </LinearLayout> 

</LinearLayout> 

Кажется, я изменял цвет фона макета, который был установлен с фоном:

android:background="?android:attr/selectableItemBackground" 

Поэтому я добавил новый макет и передан содержимое внутри него.