我在一个活动中的列表视图上工作,我希望当我按下任何列表项时,操作栏上的按钮会改变它的颜色,就像被认为是高调一样。 我选择 列表项操作栏上的按钮从白色变为灰色" 。
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// changes the color button ;
return false;
}
});
还是有其他办法可以做到这一点。
我没有足够的声誉来发布图片首先它看起来像Before selecting any item,看起来像这样When item is long pressed or selected希望你理解我的问题, 提前致谢
答案 0 :(得分:0)
编辑:误解原始问题:
int mPosition = -1;
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
if(mPosition!=-1) {
//reset color of previously pressed item to white
lv.getChildAt(mPosition).setBackgroundColor(Color.WHITE);
}
//set current item to fray.
lv.getChildAt(pos).setBackgroundColor(Color.GRAY);
//put value of currently selected item to mPosition.
mPosition = pos;
return true;
}
});
下面的旧答案:
将此添加到您的方法中。
Button button1 = (Button)findViewById(R.id.button1);
if(this condition){
button1.setBackgroundColor(Color.GRAY);
}else{
//whatever..
}
This answer涵盖相同的原则,但使用资源更详细。
答案 1 :(得分:0)
使用
选择时,您可以为列表项目添加颜色 .xml文件中的 android:listSelector="@color/AnyColor"
..请参阅编码
<ListView
android:id="@+id/nameOfYourListview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:background="AnyYouWant"
<!--code for color the selected item --> android:listSelector="ColorHereWhichYouWant" >
</ListView>