у вас есть 2 возможных способа
Первый
Удалить
android:layout_alignParentStart="true"
из вашего TextView
Второй
в вашем onBindViewHolder добавьте «removeRule», например, так:
params.removeRule(RelativeLayout.ALIGN_PARENT_START);
перед добавлением нового правила.
Вот код, на котором я тестировал:
Первый
@Override
protected void onBindViewHolder(IngredienteViewHolder holder, int position, Ingrediente model) {
holder.name.setText(model.getName());
holder.setIngrediente(model);
if (position % 2 == 0){
holder.name.setBackgroundColor(Color.BLACK);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)holder.name.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
holder.name.setLayoutParams(params); //causes layout update
} else {
holder.name.setBackgroundColor(Color.RED);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)holder.name.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
holder.name.setLayoutParams(params); //causes layout update
}
}
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item_name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="@dimen/list_ingredienti_size"
android:focusable="false"
android:text="testo"
android:textColor="@drawable/selected_textcolor"
android:gravity="center"
android:background="@drawable/selected_background"
/>
</RelativeLayout>
Второй
@Override
protected void onBindViewHolder(IngredienteViewHolder holder, int position, Ingrediente model) {
holder.name.setText(model.getName());
holder.setIngrediente(model);
if (position % 2 == 0){
holder.name.setBackgroundColor(Color.BLACK);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)holder.name.getLayoutParams();
params.removeRule(RelativeLayout.ALIGN_PARENT_START);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
holder.name.setLayoutParams(params); //causes layout update
} else {
holder.name.setBackgroundColor(Color.RED);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)holder.name.getLayoutParams();
params.removeRule(RelativeLayout.ALIGN_PARENT_START);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
holder.name.setLayoutParams(params); //causes layout update
}
}
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item_name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:textSize="@dimen/list_ingredienti_size"
android:focusable="false"
android:text="testo"
android:textColor="@drawable/selected_textcolor"
android:gravity="center"
android:background="@drawable/selected_background"
/>
</RelativeLayout>