突出显示Android中的表格行背景

时间:2018-07-16 08:13:41

标签: java android highlight android-tablelayout

尝试在表视图中突出显示指定行的背景时遇到了一些问题,表视图是来自另一个项目的回收站视图的自定义类。

我设法使用画布和位图设置了不同的图像,但是我不确定如何突出显示整个行,目前我只能突出显示一个单元格,而不能突出显示整个行。

tableViewReminders.addCellRenderer(4, (canvas, selectedRow, parent) -> {
        Paint fillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        List<List<String>> tableData = tableViewReminders.getRows();
        List<String> rowData = tableData.get(selectedRow);
        switch (rowData.get(4)) {
            default:
            case IS_SET:
                break;
            case IS_NOT_SET:
                break;
        }

        // getting the values of 2nd column of each row
        if(rowData.get(1).equals("Invalid")){
            // this part only managed to change the background color of 4th column but not the entire row 
            canvas.drawColor(Color.parseColor("#F3BF37"));
        }

        float left = canvas.getWidth() / 2.f - bitmap.getWidth() / 2.f;
        float top = canvas.getHeight() / 2.f - bitmap.getHeight() / 2.f;
        canvas.drawBitmap(bitmap, left, top, fillPaint);
    });

上面代码的输出:

enter image description here

仅第4列被突出显示,而不是整行。有任何想法吗?谢谢!

public void addCellRenderer(int col, ITableCellRenderer renderer) {
    rendererMap.put(col, renderer);
}

public interface ITableCellRenderer {
    public void onRender(Canvas canvas, int row, View parent);
}

1 个答案:

答案 0 :(得分:-1)

要突出显示表格布局的完整行,请使用以下代码:

private OnClickListener tablerowOnClickListener = new OnClickListener()
{
    public void onClick(View v)
    {
        //Highlight selected row
        //Highlight selected row
        for (int i = 1; i < tblItemDetail.getChildCount(); i++)
        {
            View row = tblItemDetail.getChildAt(i); 
            if (row == v)
            {
                row.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));               
            }
            else
            {
                //Change this to your normal background color.
                row.setBackgroundColor(getResources().getColor(android.R.color.transparent));
            }
        }
        //...
    }
};