Android 初始状态可绘制列表选择器不工作

Android 初始状态可绘制列表选择器不工作,android,selector,Android,Selector,这是我的列表视图代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"

这是我的列表视图代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@color/darkgrey">
    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

</LinearLayout>

这是列表选择器:

   <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true"
    android:drawable="@drawable/gradient_bg_hover" />

  <item android:state_selected="true"
    android:state_pressed="false"
    android:drawable="@drawable/gradient_bg_hover" />

  <item android:drawable="@drawable/gradient_bg" />
</selector>

我的目标是为列表项添加背景:初始(没有点击,没有焦点)->灰色背景。按下->蓝色背景。释放时->灰色背景

但最初的背景并没有显现出来。打开应用程序时以及您尚未单击某个项目时,灰色背景不会显示。有人知道怎么解决这个问题吗

java部分:

package com.example.whs;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class Index extends Activity {

    public static final Object TITLE = "title";
    public static final Object SUBTITLE = "subtitle";
    public static final Object THUMBNAIL = "thumbnail";
    protected static final String POSITION = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);

        // Back actionbar icon enable
        //ActionBar actionBar = getActionBar();
        //actionBar.setDisplayHomeAsUpEnabled(true);

        buildMenu();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.index, menu);
        return true;
    }

    //Builds the menu for listview
    public void buildMenu(){
        ArrayList<HashMap<String, String>> menu = new ArrayList<HashMap<String, String>>();
        //Arrays for info
        String[] menuTitleArray = {"Updates", "Gallerij"}; 
        String[] menuSubtitleArray = {"Bekijk updates", "Bekijk foto's en geef reacties", "Bekijk de updates"};
        String[] menuThumbnailArray = {"updates", "gallery"};
        for(int i=0; i < menuTitleArray.length; i++){
            // Build Hashmap for the item
            HashMap<String, String> item = new HashMap<String, String>();
            item.put((String) TITLE, menuTitleArray[i]);
            item.put((String) SUBTITLE, menuSubtitleArray[i]);
            item.put((String) THUMBNAIL, menuThumbnailArray[i]);
            menu.add(item);
        }


        // Add adapter to the list
        MenuAdapter adapter = new MenuAdapter(this, menu);
        ListView list = (ListView)findViewById(R.id.list);
        list.setAdapter(adapter);



        // Initialize the click event
        list.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                switch(position){
                case 0:
                    Intent intent = new Intent(Index.this, Updates.class);
                    startActivity(intent);
                }
            }
        });

    }
}
package com.example.whs;
导入java.util.ArrayList;
导入java.util.HashMap;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.ListView;
公共类索引扩展了活动{
公共静态最终对象TITLE=“TITLE”;
公共静态最终对象SUBTITLE=“SUBTITLE”;
公共静态最终对象缩略图=“缩略图”;
受保护的静态最终字符串位置=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
//返回操作栏图标启用
//ActionBar ActionBar=getActionBar();
//actionBar.setDisplayHomeAsUpEnabled(true);
buildMenu();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.index,menu);
返回true;
}
//为listview生成菜单
公共菜单(){
ArrayList菜单=新建ArrayList();
//信息数组
字符串[]menuTitleArray={“Updates”,“Gallerij”};
字符串[]menusubtitleray={“Bekijk更新”、“Bekijk foto的en geef反应”、“Bekijk de updates”};
字符串[]menuThumbnailArray={“更新”,“库”};
for(int i=0;i
在用于
列表视图
项目的
自定义项目布局中使用您的
android:listSelector=“@drawable/list\u selector”

就像这里有你的
列表视图一样

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical"
   android:background="@color/darkgrey">
    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
         />

</LinearLayout>

它使用以下布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:listSelector="@drawable/list_selector">

</LinearLayout>

编辑

也可以使用选择器,如

<item android:drawable="@drawable/button_bg_selected" android:state_selected="true"></item>
<item android:drawable="@drawable/button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="@drawable/button_bg_normal"></item>

我猜您希望在列表行中添加选择器,而不是在ListView:)
这是给你的答案。

我想这是因为列表行的布局xml有自己的背景设置。然后您的listSelector设置成功,但被覆盖:)

但是您必须在listview中使用它,不是吗?是的,它需要。但是如果出现问题,你也可以使用这个场景。我认为我的列表选择器是错误的,我必须实现什么状态?有没有办法关闭背景设置?如果你想使用列表选择器,然后将行布局的背景设置为@android:color/transparent:S,然后尝试将listSelector设置为@android:color/transparent,并使用自定义选择器作为列表行的背景。真奇怪,你能发布包含listview的片段或活动吗?java部分,不是xml,我遇到了同样的问题,从上面的链接中找到了解决方案,我使用了Shikha Shah的解决方案