Фрагмент ящика RecyclerView Элемент не получает эффекта пульсации

#android #xml #android-fragments #android-recyclerview

#Android #xml #android-фрагменты #android-recyclerview

Вопрос:

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

Строка элемента RecyclerView:

    <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>
  

Кажется, я менял цвет фона макета, который был установлен с помощью background :

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

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