java - listview 的 onItemClick 和 case 内的循环

标签 java android listview colors crash

我的目的是使选定的项目(从 ListView 中)突出显示。 使用下面的代码它可以完美地工作:

@Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int post,
                    long arg3) {

                int itemPosition = post;
                String itemValue = (String) lst_peers
                        .getItemAtPosition(itemPosition);
                sendMessage(itemValue + " has been selected!");

                obOpponent = new Opponent(peerListID.get(itemPosition),
                        itemValue);

                // turning off the discovery process if any
                discTime = 0;

                // set the item highlighted
                lst_peers.setItemChecked(itemPosition, true);
                arg1.setBackgroundColor(Color.YELLOW);

            }

但是,我的问题是 如何使项目恢复正常状态颜色(未突出显示), 一旦用户点击另一个项目?

我尝试将循环放入 onItemClick 方法中,但 android 崩溃了!

最佳答案

答案 1 - 最快:

尝试一个小技巧:

定义一个全局 View 变量View TempView并使用它来存储您的View arg1以供稍后(下次单击)将其更改为原始背景:

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int post,
                long arg3) {

            int itemPosition = post;
            String itemValue = (String) lst_peers
                    .getItemAtPosition(itemPosition);
            sendMessage(itemValue + " has been selected!");

            obOpponent = new Opponent(peerListID.get(itemPosition),
                    itemValue);

            // turning off the discovery process if any
            discTime = 0;

            // set the item highlighted
            lst_peers.setItemChecked(itemPosition, true);
            if (!(tempView == null)) {
                    tempView.setBackgroundColor(YOUR_ORIGINAL_BACKGROUND);
                }
                tempView = arg1;
                tempView.setBackgroundColor(Color.YELLOW);
            

        }

任何时候单击,颜色都会更改为黄色,之前单击的颜色会返回到您想要的原始颜色。

答案 2 - 更好的选择器

在您的 xml 中,添加到 ListView android:listSelector="@drawable/yourselector"> 这是一个 xml 文件,您可以在其中实现 listview 中的点击事件,如下例所示:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:state_enabled="true"> 
   <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle">
       <gradient
          android:startColor="#6018d7e5"
          android:centerColor="#6016cedb"
          android:endColor="#6009adb9"
          android:angle="270" />
    </shape>
   </item>
   <item android:state_pressed="true">
      <!-- (...)
   </item>
   <item android:state_selected="true" android:state_pressed="false">
      <!-- (...)
   </item>
    
 </selector>

关于java - listview 的 onItemClick 和 case 内的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24734370/

相关文章:

android - 安卓版 Chrome : Hide horizontal scrollbar when not scrolling

android - 如何将 imageview runtime 添加到 listview

android - 滚动后如何保存和维护ListView项目状态?

带有泛型的 Java 反射方法调用

java - J2SSH 支持的密码/Mac

java - Jersey json 序列化错误记录在哪里?

Java 计时器程序不会更新显示的时间

java - 由 : java. lang.NullPointerException 引起:尝试在空对象引用上调用接口(interface)方法

android - 某些设备上的自定义 ViewPager 渲染问题

android - 子元素的阴影在 flutter 中改变了一个 wrap 中的位置