高亮时表行点击,禁用点击下一行 [英] Highlight when Table row clicked and disable on next row clicked

查看:141
本文介绍了高亮时表行点击,禁用点击下一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有多个数据表行。当行被点击时,该特定行的显示使用深灰色的颜色。但是当我点击下一行,单击的行会突出显示,但previous行依然突出。如何禁用previous行的重头戏。我尝试了许多花样,但不工作。

I have a table row having multiple data. When a row is clicked, that particular row is highlighted with dark gray color. But when i click the next row, the clicked row is highlighted but the previous row is still highlighted. How do i disable the highlight of previous row. I tried many tricks, but that's not working.

这是我的样本code。

This is my sample code.

tableRow.setOnClickListener(new View.OnClickListener() {

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

}

我对创建的版式编程。我没有使用XML。

I have created the layout programmatically. I am not using XML.

在此先感谢。

推荐答案

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

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="@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.xml 水库\绘制文件夹

<?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 编程方式如下图所示,并设置为背景的TableRow

Update: Create StateListDrawable programmatically like below and set as Background to your TableRow:

 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);

这是 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::您可以添加更多的国家 StateListDrawable 按您的要求。

Update2: You can add More State to StateListDrawable as per your requirement.

  1. 机器人:state_activated:设置时,一个视图或其父已被激活,这意味着用户目前已经将其标记为所关注的。

  1. android:state_activated: set when a view or its parent has been "activated" meaning the user has currently marked it as being of interest.

机器人:STATE_ACTIVE:为 StateListDrawable 状态值。

android:state_active: State value for StateListDrawable.

机器人:state_enabled:当一个视图被启用设置。

android:state_enabled: Set when a view is enabled.

机器人:state_focused:为 StateListDrawable 状态值,设置当一个视图有输入焦点。

android:state_focused: State value for StateListDrawable, set when a view has input focus.

安卓STATE_ pressed:当用户为pressing在一个视图下设置。

android:state_pressed: set when the user is pressing down in a view.

机器人:state_selected:当一个视图(或它的某个父)当前选择的设置

了解 StateListDrawable

这篇关于高亮时表行点击,禁用点击下一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆