5

我的应用程序代码中有一个ListView,当点击它时使用一个AdapterView.OnItemClickListener来检测点击。问题是,当我单击一个项目时,该项目的背景变为白色,而不是默认的橙色。像这样:在此处输入图像描述

另外,当我不使用 AdapterView 时,点击的项目会变成橙色,没有任何问题。如何使单击项目的背景再次变为橙色?

编辑:

列表布局:main.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" 
    android:background="#00000000">


        <!-- ListView (grid_items) -->

        <ListView android:id="@+id/listview"
            android:layout_height="fill_parent"
            android:textSize="15px"
            android:layout_width="fill_parent"
            android:background="#00000000">
        </ListView>

</RelativeLayout>

创建():

public void onCreate(Bundle savedInstanceState) {try{
    super.onCreate(savedInstanceState);


    setContentView(R.layout.main);
    lv= (ListView)findViewById(R.id.listview);
    lv.setBackgroundColor(Color.TRANSPARENT);
    lv.setCacheColorHint(Color.TRANSPARENT);
    //......calculations
    for(int q = 0; q <v; q++){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("col_1", array[q]);
        fillMaps.add(map);
        lv.setOnItemClickListener(onListClick);
    }
    //......calculations
    }

适配器视图:

private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener(){

public void onItemClick(AdapterView<?> parent,View view, int position, long id)
{

lv.setBackgroundColor(Color.TRANSPARENT);
lv.setCacheColorHint(Color.TRANSPARENT);
//.....calculations
}

正在使用的自定义主题:

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CustomWindowTitleBackground">
        <item name="android:background">#323331</item>
    </style>

    <style name="CustomTheme" parent="android:Theme">
        <item name="android:windowTitleSize">35dip</item>
        <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
    </style>
</resources>
4

4 回答 4

8

自定义列表视图项的最佳方法是在 xml 文件中创建它们,然后使用 BaseAdapter 或任何类型的列表适配器,扩展该 .xml 布局并填充您的数据。为了自定义一个高亮动画,你只需要为一个项目的不同状态创建一个状态 Drawable。

所以你去一个“新的xml文件”->“资源类型drawable”->“形状”命名它并完成你会看到这段代码:

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


</shape>

让我们为 ListItem 按下状态创建一个背景:

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

    <solid android:color="#86d47f" this is your custom color />  

</shape>

非按下状态是另一个文件:

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

    <solid android:color="#00000000" />

</shape>

然后是一个列表选择器文件,它将用作列表视图项的背景:转到“新 xml 文件”->“drawable”->“列表选择器”,将其命名为“item_background”->finish

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"  android:exitFadeDuration="300"  !!this is the fade duration time for your animation>

    <item android:drawable="@drawable/bg_drawable" android:state_pressed="true"></item>
    <item android:drawable="@drawable/transparend"></item>

</selector>

然后为一个项目创建一个 xml 文件,根据需要自定义它,但是对于主布局集

android:background="@drawable/item_background"

给你,一切都很完美......这是我的适配器类:

public class ListAdapter extends ArrayAdapter<String> {
LayoutInflater inflater;

public ListAdapter(Context context, int textViewResourceId,
        List<String> objects) {
    super(context, textViewResourceId, objects);
    inflater = LayoutInflater.from(context);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    String str_value = this.getItem(position);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_item, null);
    }
    TextView tv = (TextView) convertView.findViewById(R.id.textView1);
    tv.setText(str_value);
    return convertView;
}

}

在这里,假设您将其标记为答案.. 列表

于 2013-06-06T06:26:57.350 回答
0

so one simple solution is to ste the Color of the View istself in the onClicklistenener itself:

private AdapterView.OnItemClickListener onListClick=new 
AdapterView.OnItemClickListener(){

public void onItemClick(AdapterView<?> parent,View view, int position, long id)
{

   lv.setBackgroundColor(Color.TRANSPARENT);
   lv.setCacheColorHint(Color.TRANSPARENT);
   // I added this
   if(is the item checked ){
      view.setBackgroundColor(Color.BLUE); //or whatever
   }else{
      view.setBackgroundColor(00000000);
   }
   //.....calculations
}

You will need to hold the Information about weather the item is checked or not.

If you what do not want to use the onClickListener but use only styles (which might be the preferable way), I cannot help you furher for the moment.

Edit: By the way I can't see why yout do this in th onClickListener every time:

lv.setBackgroundColor(Color.TRANSPARENT);
lv.setCacheColorHint(Color.TRANSPARENT);

Hope this helps anyway.

于 2013-06-03T10:01:20.260 回答
0

试试这个来设置任何自定义选择器:

在“styles.xml”中创建这种风格:

<style name="MyList">
        <item name="android:drawSelectorOnTop">true</item>
        <item name="android:listSelector">@drawable/my_list_selector</item>
</style>

在您的布局中,只需将此样式添加到 ListView:

<ListView
....
style="@style/MyList"
.... />

在“drawable”中,您可以使用所需的颜色或图像定义“my_list_selector”:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <shape>
            <solid android:color="@color/state_pressed" />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape>
            <solid android:color="@color/state_focused" />
        </shape>
    </item>
    <item android:drawable="@android:color/transparent" />

</selector> 

将这些颜色值添加到您的 'colors.xml' :

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="state_pressed">#BB7dbcd3</color>
    <color name="state_focused">#777dbcd3</color>
    <color name="transparent">#00000000</color>
</resources>
于 2013-06-07T11:07:17.103 回答
0

我的可绘制文件夹中有一个名为list_selector_background_longpress.9的文件,其中有一张白色照片,顶部有一条橙色线。所以我用颜色的图片改变了它holo_orange_dark,并且解决了它。对于这样的问题,这是多么愚蠢的简单解决方案。

于 2013-06-09T09:43:52.497 回答