更改列表视图中所选项目或行的背景颜色

时间:2015-03-12 20:06:20

标签: android

我想更改Listview中所选项目的背景颜色。如果我在第一个位置选择项目,我希望所选项目的背景颜色为灰色,而不是文本,而是该单行的背景。如果我在第二个位置选择另一个项目,我希望在第一个位置选择的项目背景恢复为原始颜色,新项目的背景颜色变为灰色。

我一直在使用以下代码:

public void onItemClick (AdapterView < ? > parent, View view,
int position, long id){
    for (int j = 0; j < parent.getChildCount(); j++)
        parent.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);

    // change the background color of the selected element
    view.setBackgroundColor(Color.LTGRAY);
}

然而,它会自动更改每个第十项的背景。

2 个答案:

答案 0 :(得分:0)

删除该循环。请改用:

YourList.getChildAt(position).setBackgroundColor(Color.LTGRAY)

答案 1 :(得分:0)

我最终要做的是以下事情:

在我的适配器类中

public class OrderlistAdapter extends RecyclerView.Adapter<OrderlistAdapter.OrderViewHolder>
{

    private List<Order> orders;
    private Context context;
    private OrderClickListener orderClickListener;
    private int selectedPosition=-1;

    public OrderlistAdapter(Context context, List<Order> orders, OrderClickListener orderClickListener)
    {
        this.context = context;
        this.orders = orders;
        this.orderClickListener = orderClickListener;
    }

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

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

        Order order = orders.get(position);
        String orderStatus = checkStatus(order.getOrder_status());

        String date = null;
        try
        {
            date = formatDate(order.getOrder_date_added());
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }
        holder.order_id.setText(order.getOrder_id());
        holder.ordered_date.setText(date);
        holder.order_status.setText(orderStatus);

        if(selectedPosition==position)
        {
            holder.itemView.setBackgroundColor(Color.parseColor("#F57C00"));
        }
        else if(isComplete(order.isFullyProcessed()))
        {
            holder.itemView.setBackgroundColor(Color.parseColor("#C0C0C0"));
            //holder.view.setSelected(true);
        }
        else
        {
            holder.itemView.setBackgroundColor(Color.parseColor("#ffffff"));
        }

        holder.view.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                orderClickListener.onItemClicked(position);
                selectedPosition = position;
                notifyDataSetChanged();
            }
        });

        if(isComplete(order.isFullyProcessed()))
        {

        }
    }


    public class OrderViewHolder extends RecyclerView.ViewHolder
    {
        @BindView(R.id.textViewOrderId)
        TextView order_id;
        @BindView(R.id.textViewDateAdded)
        TextView ordered_date;
        @BindView(R.id.textViewOrderStatus)
        TextView order_status;
        public View view;

        public OrderViewHolder(View itemView)
        {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.view = itemView;
        }

    }