繁体   English   中英

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

[英]How to change the indicator color every different cardview?

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

在此处输入图片说明

这是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"));

    }
}

你知道我该如何更改吗?

假设您的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))
}

制作两个类似@ 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);
}
  1. 您必须为ImageView发言status指定一个id
  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); } 

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

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

if(status.equals("Yes")
{
  rectangleImageView.setColor(Color.GREEN);
}
else
{
  rectangleImageView.setColor(Color.RED);
}
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));    
    }
}

暂无
暂无

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

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