0

这是我的列表视图代码:

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

    }
}
4

3 回答 3

1

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

于 2013-02-27T15:00:55.273 回答
0

使用你android:listSelector="@drawable/list_selector"custom item layout你用于ListView物品。

就像你在ListView这里

<?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>
于 2013-02-27T14:04:16.670 回答
0

我猜您希望在列表行上添加选择器,而不是在 ListView 上:) 这是您的链接

于 2013-02-27T14:05:59.473 回答