如何在每个不同的卡片视图中更改指示器颜色?

时间:2018-07-09 08:19:44

标签: android android-cardview

我创建的Cardview设计的左侧带有指示器(红色,绿色),但是我有问题,如何更改指示器的颜色取决于数据库的状态。如果状态为“否”,则指示灯变为红色,如果状态为“是”,则指示灯变为绿色。

enter image description here

这是xml文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <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="6dp"
        card_view:cardElevation="3dp"
        card_view:cardUseCompatPadding="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.9">

                <ImageView
                    android:layout_marginLeft="22dp"
                    android:layout_marginTop="14dp"
                    android:layout_marginBottom="14dp"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/rectangle"/>

            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.1"
                android:orientation="vertical">

                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/layout_card_item"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:orientation="vertical"
                    android:padding="5dp">


                    <TextView
                        android:id="@+id/txt_order_date"
                        android:textSize="13sp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>

                    <TextView
                        android:id="@+id/txt_customer_name"

                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
                    <TextView
                        android:id="@+id/txt_order_number"
                        android:textSize="12sp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>

                    <TextView
                        android:id="@+id/txt_product_status"
                        android:textSize="12sp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
                    <TextView
                        android:id="@+id/txt_notes"
                        android:textSize="12sp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:singleLine="false"
                        android:layout_weight="1"
                        android:maxLines="4"/>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="30dp"
                        android:background="@drawable/layout_bg"
                        android:descendantFocusability="blocksDescendants"
                        android:orientation="horizontal"
                        android:layout_below="@+id/layout_card_item"
                        android:gravity="center"
                        android:weightSum="1">

                        <Button
                            android:id="@+id/download_pdf"
                            android:textSize="14sp"
                            android:layout_width="wrap_content"
                            android:layout_height="30dp"
                            android:background="#e1e3e8"
                            android:focusable="false"
                            android:textColor="#000307"
                            android:focusableInTouchMode="true"
                            android:drawableLeft="@drawable/ic_pictogram_download_orange"
                            android:text="  Download PDF"
                             />
                        <!--<ImageButton-->
                            <!--android:layout_width="10dp"-->
                            <!--android:layout_height="10dp"-->
                            <!--android:src="@mipmap/ic_order_download_pdf"/>-->


                    </LinearLayout>


                </LinearLayout>



            </LinearLayout>
        </LinearLayout>



    </android.support.v7.widget.CardView>
</LinearLayout>

这是适配器:

public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.OrderListViewHolder> {

    private ArrayList<OrderListModel> itemOrderList;

    public OrderListAdapter(ArrayList<OrderListModel> itemOrderList) {
        this.itemOrderList = itemOrderList;
    }

    @Override
    public OrderListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.order_list_item, parent, false);
        return new OrderListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(OrderListViewHolder orderListViewHolder, int position) {
        orderListViewHolder.txtDate.setText(itemOrderList.get(position).getDate());
        orderListViewHolder.txtOrderNumber.setText(itemOrderList.get(position).getOrderNumber());
        orderListViewHolder.txtCustomerName.setText(itemOrderList.get(position).getCustomerName());
        orderListViewHolder.txtProductStatus.setText(itemOrderList.get(position).getProductStatus());
        orderListViewHolder.txtNotes.setText(itemOrderList.get(position).getNotes());
    }

    @Override
    public int getItemCount() {
        return (itemOrderList != null) ? itemOrderList.size() : 0;
//        return itemOrderList.size();
    }

    public class OrderListViewHolder extends RecyclerView.ViewHolder{
        private TextView txtDate, txtOrderNumber, txtCustomerName, txtProductStatus,
                txtNotes;

        public OrderListViewHolder(View itemView) {
            super(itemView);
            txtDate = (TextView) itemView.findViewById(R.id.txt_order_date);
            txtOrderNumber = (TextView) itemView.findViewById(R.id.txt_order_number);
            txtCustomerName = (TextView) itemView.findViewById(R.id.txt_customer_name);
            txtProductStatus = (TextView) itemView.findViewById(R.id.txt_product_status);
            txtNotes = (TextView) itemView.findViewById(R.id.txt_notes);
        }
    }
}

这是主要内容:

public class OrderListFragment extends Fragment {

    private RecyclerView recyclerView;
    private OrderListAdapter adapter;
    private ArrayList<OrderListModel> OrderListArrayList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order_list, container, false);
        getActivity().setTitle(R.string.title_mn_order_list);
        addData();

        recyclerView = (RecyclerView) view.findViewById(R.id.orderList_recycler_view);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
        recyclerView.setLayoutManager(layoutManager);

        adapter = new OrderListAdapter(OrderListArrayList);
        recyclerView.setAdapter(adapter);
        return view;

    }

    void addData(){
        OrderListArrayList = new ArrayList<>();
        OrderListArrayList.add(new OrderListModel(
                "4 Juli 2018 10:54",
                "0001",
                "Jopa",
                "No",
                "Notes"));
        OrderListArrayList.add(new OrderListModel(
                "4 Juli 2018 10:54",
                "0001",
                "Jopa",
                "Yes",
                "Notes"));

    }
}

您知道我该如何更改吗?

5 个答案:

答案 0 :(得分:1)

假设您的ImageView是指示器栏:

<ImageView
android:id="@+id/ivIndicator" <!-- Add this id -->
android:layout_marginLeft="22dp"
android:layout_marginTop="14dp"
android:layout_marginBottom="14dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rectangle"/>

在您的OrderListViewHolder中获取对它的引用:

ivIndicator = (ImageView) itemView.findViewById(R.id.ivIndicator);

然后在onBindViewHolder中为imageview着色:

if("Yes".equals(itemOrderList.get(position).getStatus())) {
    orderListViewHolder.ivIndicator.setColorFilter(ContextCompat.getColor(activity, android.R.color.green))
else {
    orderListViewHolder.ivIndicator.setColorFilter(ContextCompat.getColor(activity, android.R.color.red))
}

答案 1 :(得分:1)

制作两个可绘制文件,例如@ drawable / rectangle,并为其设置所需的不同颜色。

if(status.equals("No") {  
    yourView.setBackground(context.getResources().getDrawable(R.drawable.rectangleWithRed));
}

else if (status.equals("Yes") {
    yourView.setBackground(context.getResources().getDrawable(R.drawable.rectangleWithGreen);
}

答案 2 :(得分:1)

  1. 您必须给idImageView的情况下使用status
  2. OrderListViewHolder中添加您的声明private ImageView status;
  3. 在OrderListViewHolder中添加status = (ImageView) itemView.findViewById(R.id.status);
  4. OrderListViewHolder中添加

    if (itemOrderList.get(position).getStatus().equals("Yes")) {
        orderListViewHolder.status.setBackgroundColor(0x00ff00);
    } else { 
        orderListViewHolder.status.setBackgroundColor(0xff0000); }
    

当然,您必须像创建其他所有getStatus()一样创建getSomething()方法

答案 3 :(得分:0)

如果要根据状态更改颜色,请在Recycler Adapter的onBindViewHolder上检查状态并设置为所需的颜色。

if(status.equals("Yes")
{
  rectangleImageView.setColor(Color.GREEN);
}
else
{
  rectangleImageView.setColor(Color.RED);
}

答案 4 :(得分:0)

public void onBindViewHolder(OrderListViewHolder orderListViewHolder, int position) {
    if (OrderListModel.getProductStatus().equals("Yes"))    
        rectangleImageView.setColorFilter(getContext().getResources().getColor(R.color.green));
    } else {
        rectangleImageView.setColorFilter(getContext().getResources().getColor(R.color.red));    
    }
}