简体   繁体   English

状态变化的颜色变化

[英]color change of status change

I am making an application on leave management and I want to make my leave list layout like shown in the 我正在休假管理上申请,我想使休假列表布局如图所示。 在此处输入图片说明 image I want to change the colour of list view on status change from the database. 我想从数据库更改状态更改时列表视图的颜色。 Now i just add the simple text view to show the list but i want to modify it like the image it will really helpful to me if you give me the code. 现在,我只是添加简单的文本视图以显示列表,但我想像图像一样对其进行修改,如果您给我代码,这对我真的很有帮助。
here is my java code adapter: 这是我的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);

    }
}

Here is my Xml layout: 这是我的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>

I have added image view in your layout so change color of this image view as par the status 我已在您的布局中添加了图像视图,因此请将该图像视图的颜色更改为与状态相同的颜色

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

and i have created adapter just for your understanding 我创建了适配器只是为了您的理解

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

You have 2 options here: 您在这里有2个选项:

  • In your RelativeLayout, add FrameLayout empty element and align left parent with full height and ~5dp width. 在您的RelativeLayout中,添加FrameLayout空元素,并将左父对象与完整高度和〜5dp宽度对齐。 Then in your onBindViewHolder() set FrameLayout background colour as you need. 然后在您的onBindViewHolder()中根据需要设置FrameLayout背景色。 Of course, this option is simple shape (Rectangle) 当然,这个选项是简单的形状(矩形)

  • If the shape you want is specific, also in RelativeLayout, add .PNG image and set backgroundTint programmatically 如果您想要的形状是特定的,则也在RelativeLayout中添加.PNG图像并以编程方式设置backgroundTint

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM