我的应用代码中有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>
的onCreate():
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>
答案 0 :(得分:8)
自定义列表视图项的最佳方法是在xml文件中创建它们,然后使用BaseAdapter或任何类型的列表适配器,膨胀该.xml布局并填充您的数据。为了自定义高亮动画,您只需为项目的不同状态创建一个Drawable状态。
所以你去了一个“新的xml文件” - &gt; “资源类型drawable” - &gt; “形状”命名并完成 你会看到这段代码:
<?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>
然后是一个列表选择器文件,它将用作listview项的背景: 转到“新xml文件” - &gt;“drawable” - &gt;“列表选择器”将其命名为“item_background” - &gt;完成
<?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;
}
}
在这里,你应该把它标记为答案..
答案 1 :(得分:0)
所以一个简单的解决方案是在onClicklistenener本身中使用View的颜色:
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
}
您需要保存有关天气的信息,否则将检查该项目。
如果您不想使用onClickListener但只使用样式(这可能是更好的方式),我暂时无法帮助您。
编辑:顺便说一下,我无法理解为什么你每次都在onClickListener中这样做:
lv.setBackgroundColor(Color.TRANSPARENT);
lv.setCacheColorHint(Color.TRANSPARENT);
希望这无论如何都有帮助。
答案 2 :(得分: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>
答案 3 :(得分:0)
我的drawable文件夹中有一个名为list_selector_background_longpress.9
的文件,其中有一张白色照片,顶部有橙色线条。所以我用holo_orange_dark
颜色的图片改变了它,并解决了它。对于这样的问题来说,这是一个非常简单的解决方案。