How can I highlight the table row on click ?

2019-01-09 03:20发布

问题:

As my project requirement i have to highlight the table row on onClick. There is any way to do this? Or please suggest me the alternative?

回答1:

If you want to use the stock on click highlight like you get with a generic ListView, you want to set the background of each row to be android:background="@android:drawable/list_selector_background"

Here is an example:

<TableLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:stretchColumns="0">
  <TableRow
     android:id="@+id/first_row"
     android:background="@android:drawable/list_selector_background" >
    ... row content ...
  </TableRow>
</TableLayout>

Then in code,

TableRow firstRow = (TableRow) findViewById(R.id.first_row);
firstRow.setOnClickListener(new OnClickListener() {
       @Override
        public void onClick(View v) {
            // TODO: do your logic here

        }   
}

And you should get a highlight-able row just like in a ListView...

EDIT: Above will give you the default theme's list background selector. If you want the more generic selector (like the material design selector when the user touches a row) use this:

android:background="?android:attr/selectableItemBackground"

Also this applies to more than just TableRows. You should be able to do this on almost any generic widget with an onClickListener attached (TextViews, Buttons, etc).



回答2:

Even I was facing the same problem with the help of salil pandit answer made a little change to it and that works for me

This is TableRow in xml:

<TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:padding="5dip" 
        android:background="@drawable/selector">

This is selector.xml in res\drawable folder

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item   android:state_focused="true"
            android:state_pressed="true"
            android:drawable="@android:drawable/list_selector_background"></item>
    <item   android:state_focused="true"
            android:state_pressed="false"
            android:drawable="@android:drawable/list_selector_background"></item>
    <item
            android:state_focused="false"
            android:state_pressed="true"
            android:drawable="@android:drawable/list_selector_background" />


     <item android:drawable="@android:drawable/list_selector_background"></item>

</selector>


回答3:

Within the onclicklistener add:

 tr1.setBackgroundResource(drawable.list_selector_background);

Where tr1 is your tablerow. (you will need to make the tablerow final for it to work).



回答4:

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


回答5:

String _row_selected = null;
boolean _is_selection_even = false;
private TableLayout TL;
TableRow row_data = new TableRow(this);

row_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (_row_selected != null) {

                    if (Integer.parseInt(_row_selected) == TL.indexOfChild(v)) {

                        if (_is_selection_even) {
                            TL.getChildAt(Integer.parseInt(_row_selected)).setBackgroundColor(0xFF00FF00);
                            _is_selection_even = false;
                        } else {
                            TL.getChildAt(Integer.parseInt(_row_selected)).setBackgroundColor(Color.WHITE);
                            _is_selection_even = true;
                        }


                    } else {
                        TL.getChildAt(Integer.parseInt(_row_selected)).setBackgroundColor(Color.WHITE);
                        v.setBackgroundColor(0xFF00FF00);
                        _row_selected = null;
                        _row_selected = TL.indexOfChild(v) + "";
                    }

                } else {
                    v.setBackgroundColor(0xFF00FF00);
                    _row_selected = null;
                    _row_selected = summaryTL.indexOfChild(v) + "";
                }
              }
        });