изменение цвета изменения статуса - PullRequest
0 голосов
/ 25 мая 2018

Я подаю заявку на управление отпуском и хочу сделать макет моего списка отпусков, как показано на изображении enter image description here. Я хочу изменить цвет представления списка при изменении статуса из базы данных.Теперь я просто добавляю простое текстовое представление, чтобы показать список, но я хочу изменить его, как на изображении, которое будет очень полезно для меня, если вы дадите мне код.
вот мой адаптер кода Java:

@NonNull
@Override
public LeaveViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.leave_list, null);
    view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
    LeaveViewHolder viewHolder = new LeaveViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull LeaveViewHolder holder, int position) {

    LeaveModel leave = leaveList.get(position);
    holder.tVFrom_date.setText("From:  " + leave.getFrom_date());
    holder.tVTo_date.setText("To:  " + leave.getTo_date());
    holder.tVStatus.setText("Status: " + leave.getLeavestatus());
    if (holder.tVStatus == null) {

    } else {

    }
}

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

public class LeaveViewHolder extends RecyclerView.ViewHolder  {
    protected TextView tVFrom_date, tVTo_date, tVStatus;
    public View container;

    public LeaveViewHolder(View itemView) {
        super(itemView);
        tVFrom_date = itemView.findViewById(R.id.tVFrom_date);
        tVTo_date = itemView.findViewById(R.id.tVTo_date);
        tVStatus = itemView.findViewById(R.id.tVStatus);

    }
}

Вот мой макет XML:

<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="5dp"
    card_view:cardElevation="5dp"
    card_view:cardPreventCornerOverlap="false"
    card_view:cardUseCompatPadding="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingEnd="6dp"
        android:paddingStart="6dp">

        <TextView
            android:id="@+id/tVFrom_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:textColor="@color/Black"/>

        <TextView
            android:id="@+id/tVTo_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tVFrom_date"
            android:layout_marginTop="5dp"
            android:textColor="@color/Black"/>

        <TextView
            android:id="@+id/tVStatus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tVTo_date"
            android:layout_marginTop="5dp"
            android:textColor="@color/Red"
            android:textStyle="bold" />
    </RelativeLayout>
</android.support.v7.widget.CardView>

Ответы [ 2 ]

0 голосов
/ 25 мая 2018

Я добавил изображение в ваш макет, поэтому измените цвет этого изображения в соответствии со статусом

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="5dp"
card_view:cardPreventCornerOverlap="true"
card_view:cardUseCompatPadding="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/img_LeaveAdapter_highlight"
        android:layout_width="8dp"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingEnd="6dp"
        android:paddingStart="6dp">

        <TextView
            android:id="@+id/tVFrom_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:textColor="@color/Black" />

        <TextView
            android:id="@+id/tVTo_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tVFrom_date"
            android:layout_marginTop="5dp"
            android:textColor="@color/Black"

            />

        <TextView
            android:id="@+id/tVStatus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tVTo_date"
            android:layout_marginTop="5dp"
            android:textColor="@color/Red"
            android:textStyle="bold" />

    </RelativeLayout>
</LinearLayout>

, и я создал адаптер только для вашего понимания

public class LeaveAdapter extends RecyclerView.Adapter<LeaveAdapter.MyViewHolder> {
Activity activity;
ArrayList<LeaveModel> list;

public LeaveAdapter(Activity activity, ArrayList<LeaveModel> list) {
    this.activity = activity;
    this.list = list;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_leaveadapter, parent, false);
    return new MyViewHolder(rootView);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

    String leaveStatus = list.get(position).getLeaveStatus();
    if (leaveStatus.equals("Panding for approval")) {
        holder.img_LeaveAdapter_highlight.setBackgroundColor(ContextCompat.getColor(activity, R.color.Red));

    } else if (leaveStatus.equals("Approved")) {
        holder.img_LeaveAdapter_highlight.setBackgroundColor(ContextCompat.getColor(activity, R.color.Green));

    }
}

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

public class MyViewHolder extends RecyclerView.ViewHolder {
    ImageView img_LeaveAdapter_highlight;

    public MyViewHolder(View itemView) {
        super(itemView);
        img_LeaveAdapter_highlight = itemView.findViewById(R.id.img_LeaveAdapter_highlight);
    }
}
}
0 голосов
/ 25 мая 2018

У вас есть 2 варианта:

  • В вашем RelativeLayout добавьте пустой элемент FrameLayout и выровняйте левого родителя по полной высоте и ширине ~ 5dp.Затем в вашем onBindViewHolder () установите цвет фона FrameLayout, как вам нужно.Конечно, эта опция - простая фигура (прямоугольник)

  • Если нужная фигура специфична, также в RelativeLayout добавьте изображение .PNG и установите backgroundTint программно

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...