Android:选择器在 TableRow 选择中未按预期工作

发布于 2024-12-28 08:46:04 字数 1832 浏览 2 评论 0原文

我的 XML 中有一个 TAbleLayout,其中有 1 行,即标题行。其他所有行,我动态添加。我想在单击它时选择一行触摸它时。我向每一行添加了 clickListener,并且能够捕获单击事件。但是当按下或按住鼠标时,我看到颜色发生变化,然后又变成正常颜色。我希望颜色保持更改,直到单击其他行或单击其他位置以停用所选行。

我的选择器 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<!--  Active state -->  
<item android:state_selected="true" android:state_focused="false"
      android:state_pressed="false" android:drawable="@android:color/transparent" />      
<!--  Inactive state-->  
<item android:state_selected="false" android:state_focused="false"
      android:state_pressed="false" android:drawable="@android:color/transparent" /> 
 <!--  Pressed state --> 
 <item android:state_pressed="true" android:drawable="@color/yellow" />

<!--  Selected state (using d-pad) -->  
 <item android:state_focused="true" android:state_selected="true"
       android:state_pressed="false" android:drawable="@color/yellow" />

我动态生成行的 Java 代码:

private void createView(TableRow tr, TextView tv, String data, int rowId) {
    tv.setText(data);

    tv.setTextColor(Color.WHITE);
    tv.setPadding(20, 0, 0, 0);

    tr.setPadding(0, 1, 0, 1);
    //tr.setBackgroundColor(Color.TRANSPARENT);
    tr.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.tablerowselector));
    tr.setId(rowId);
    tr.setClickable(true);
    tr.setFocusable(true); // Added thinking that if its not focusable then it can't be selected, but makes no difference
    tr.setOnClickListener(this);
    tr.addView(tv);
}

如果我处理 clickListener 事件,并根据设置 prevRowSelectedcurrRowSelected 逻辑设置行的背景颜色,那么就可以了。但是如何使选择器工作以及为什么它不起作用?选择器不能按照我试图实现的方式工作吗?

谢谢

任何帮助。

I have a TAbleLayout in my XML with 1 row i.e. te headings row. Other all rows, I add dynamically. I want to make a row selected on clicking it & on touching it. I added clickListener to each row and am able to trap click event. But when the mouse is pressed or kept pressed, I see color change, then again it comes to normal color. I want the color to stay changed until I click other row or click somewhere else to deactivate the selected row.

My selector XML code :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<!--  Active state -->  
<item android:state_selected="true" android:state_focused="false"
      android:state_pressed="false" android:drawable="@android:color/transparent" />      
<!--  Inactive state-->  
<item android:state_selected="false" android:state_focused="false"
      android:state_pressed="false" android:drawable="@android:color/transparent" /> 
 <!--  Pressed state --> 
 <item android:state_pressed="true" android:drawable="@color/yellow" />

<!--  Selected state (using d-pad) -->  
 <item android:state_focused="true" android:state_selected="true"
       android:state_pressed="false" android:drawable="@color/yellow" />

My Java code where I generate rows dynamically :

private void createView(TableRow tr, TextView tv, String data, int rowId) {
    tv.setText(data);

    tv.setTextColor(Color.WHITE);
    tv.setPadding(20, 0, 0, 0);

    tr.setPadding(0, 1, 0, 1);
    //tr.setBackgroundColor(Color.TRANSPARENT);
    tr.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.tablerowselector));
    tr.setId(rowId);
    tr.setClickable(true);
    tr.setFocusable(true); // Added thinking that if its not focusable then it can't be selected, but makes no difference
    tr.setOnClickListener(this);
    tr.addView(tv);
}

If I handle in clickListener event, and set Background color of row based on setting prevRowSelected and currRowSelected logic, that works. But how to make selector work and why it is not working ? Can't selector work the way am trying to achieve it ?

Thanks

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

停顿的约定 2025-01-04 08:46:04

据我了解,您必须更改选择器。我在此处找到了食谱

As I understand, you have to change the selector. I have found the recipes here

意中人 2025-01-04 08:46:04

朋友,选择器,没有按我的预期工作。因此,使用适当的逻辑并设置行的颜色以编程方式处理它。

感谢大家。

Friends, selector, doesn't work as I had expected. So have handled it programmatically using apprpriate logic and setting row's color.

Thanks to all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文