以编程方式点击后,行不会在listview android中突出显示

时间:2014-05-14 11:03:49

标签: android listview

我有一个已填充的列表视图,在填充之后我已经以编程方式进行了单击,这使得我的listview onItemclick事件被执行。直到这里它的罚款,但我的问题是我无法突出显示以编程方式点击的行我已经设置了在点击事件上更改的背景颜色我在这里放我的代码

messageListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                try {
//                  arg1.setSelected(true);
                    lastSelectedView1.setBackgroundColor(HomeActivity.this
                            .getResources().getColor(R.color.white));
                    lastSelectedView2.setBackgroundColor(HomeActivity.this
                            .getResources().getColor(R.color.white));
                    lastSelectedTextView1.setTextColor(HomeActivity.this
                            .getResources().getColor(R.color.dark_gray));
                    lastSelectedTextView2.setTextColor(HomeActivity.this
                            .getResources().getColor(R.color.dark_gray));
                    lastSelectedTextView3.setTextColor(HomeActivity.this
                            .getResources().getColor(R.color.dark_gray));
                    lastSelectedView.setBackgroundColor(HomeActivity.this
                            .getResources().getColor(R.color.white));
                    lastFriendNameTextView.setTextColor(HomeActivity.this
                            .getResources().getColor(R.color.dark_gray));
                    lastSentMessageLabelTextView.setTextColor(HomeActivity.this
                            .getResources().getColor(R.color.dark_gray));
                    lastSentMessageTextView.setTextColor(HomeActivity.this
                            .getResources().getColor(R.color.dark_gray));
                    lastReceivedMessageLabelTextView
                            .setTextColor(HomeActivity.this.getResources()
                                    .getColor(R.color.dark_gray));
                    lastReceivedMessageTextView.setTextColor(HomeActivity.this
                            .getResources().getColor(R.color.dark_gray));
                    lastPhoneNumberTextView.setTextColor(HomeActivity.this
                            .getResources().getColor(R.color.dark_gray));
                } catch (Exception e) {
                    System.out.println(">>>Exception>>>" + e.toString()
                            + ">>>Message>>>" + e.getMessage());
                }

                try {
                    View view1 = arg1.findViewById(R.id.top_layout);
                    View view2 = arg1.findViewById(R.id.row_background);
                    TextView textView1 = (TextView) arg1
                            .findViewById(R.id.textView1);
                    textView1.setTextColor(HomeActivity.this.getResources()
                            .getColor(R.color.white));
                    TextView textView2 = (TextView) arg1
                            .findViewById(R.id.textView2);
                    textView2.setTextColor(HomeActivity.this.getResources()
                            .getColor(R.color.white));
                    TextView timeTextView = (TextView) arg1
                            .findViewById(R.id.time_textview);
                    timeTextView.setTextColor(HomeActivity.this.getResources()
                            .getColor(R.color.white));
                    view1.setBackgroundColor(HomeActivity.this.getResources()
                            .getColor(R.color.dark_gray));
                    view2.setBackgroundColor(HomeActivity.this.getResources()
                            .getColor(R.color.dark_gray));
                    lastSelectedView1 = view1;
                    lastSelectedView2 = view2;
                    lastSelectedTextView1 = textView1;
                    lastSelectedTextView2 = textView2;
                    lastSelectedTextView3 = timeTextView;
                    isSelectedFromMessageList = true;
                    selectedItemFromMessageList = messageArrayList.get(arg2).Name;
                    selectedContactItem = arg2;
                    // createMessageLayout(selectedItemFromMessageList);

                    // setListButtonBackground();
                    createMessageLayout(messageArrayList.get(arg2).Name);
                } catch (Exception e) {
                    System.out.println(">>>Exception>>>" + e.toString()
                            + ">>>Message>>>" + e.getMessage());
                }
            }
        });

我的代码怎么了?

1 个答案:

答案 0 :(得分:1)

只需在drawable文件夹下创建一个background.xml

background.xml

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

    <item android:drawable="@color/orange" android:state_pressed="true"/>
    <item android:drawable="@color/cyan" android:state_pressed="false"/>

</selector>  

为你的列表行设置android:background =“@ drawable / background”