2014-02-05 137 views
2

我有一个表格行有多个数据。当单击一行时,该特定行将以深灰色突出显示。但是当我点击下一行时,被点击的行突出显示,但前一行仍然突出显示。我如何禁用上一行的高亮显示。我尝试了很多技巧,但这不起作用。突出显示当表格行点击并禁用下一行点击

这是我的示例代码。

tableRow.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      v.setBackgroundColor(Color.DKGRAY); 

} 

我已经以编程方式创建了布局。我没有使用XML。

在此先感谢。

+0

看到我的新的更新ü只是需要更多的'State'加入到'StateListDrawable'为每U [R要求 –

回答

3

如果你想用股票上点击亮点像你这样一个通用的ListView得到,你要设置的每个行的背景是

android:background="@drawable/selector" 

下面是一个例子:

<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"> 

这是selector.xmlres\drawable文件夹

<?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="@color/blue></item> 
<item android:state_focused="true" 
     android:state_pressed="false" 
     android:drawable="@color/custom"></item> 
<item 
     android:state_focused="false" 
     android:state_pressed="true" 
     android:drawable="@color/gray" /> 


<item android:drawable="@color/white"></item> 

</selector> 

更新:创建StateListDrawable编程像下面并设置为BackgroundTableRow

Drawable d1=activity.getResources().getDrawable(R.drawable.gradient_bg_hover); 

    GradientDrawable g = new GradientDrawable(Orientation.TOP_BOTTOM, 
      new int[] { Color.DKGRAY});  
g.setGradientType(GradientDrawable.LINEAR_GRADIENT); 

      StateListDrawable states = new StateListDrawable(); 
      states.addState(new int[] {android.R.attr.state_pressed,-android.R.attr.state_selected},d1); 
      states.addState(new int[] {-android.R.attr.state_focused},g); 

      table_row.setBackgroundDrawable(states); 

这是res\drawable文件夹gradient_bg_hover.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
<!-- Gradient BgColor for listrow Selected --> 
<gradient 
    android:startColor="#18d7e5" 
    android:centerColor="#16cedb" 
    android:endColor="#09adb9" 
    android:angle="270" /> 
</shape> 

UPDATE2:您可以添加更多StateStateListDrawable按您的要求。

  1. android:state_activated:时设置视图或其父已被“激活”,这意味着用户目前已经将其标记为感兴趣。

  2. android:state_active:StateListDrawable的状态值。

  3. android:state_enabled:启用视图时设置。

  4. android:state_focused:当视图具有输入焦点时设置的StateListDrawable的状态值。

  5. android:state_pressed:当用户在视图中按下时设置。

  6. android:state_selected:当当前选择视图(或其父项之一)时设置。

更多有关StateListDrawable

+0

我已经创建的布局编程。我没有使用XML。 – Suniel

+0

@Suniel查看我的更新 –

+0

在任何情况下,它只会突出显示最后一行。 – Suniel