我需要在单击时更改ListView中所选项目的颜色,以便用户知道点击的内容。
到目前为止,我已为此完成了此代码:
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
for (int a = 0; a< parent.getChildCount();a++){
parent.getChildAt(a).setBackgroundColor(Color.TRANSPARENT);
view.refreshDrawableState();
if(parent.getChildAt(a) == view){
view.setBackgroundColor(getResources().getColor(R.color.soft_opaque));
view.refreshDrawableState();
}
}
它的作用是更改所选项目的背景颜色,并保持这样,直到我点击另一个项目,使其仅更改当前项目选择的背景颜色
我也想知道是否有XML方法可以做到这一点。我到目前为止所发现的是:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/orange" /> <!-- pressed -->
<item android:state_focused="true" android:drawable="@color/black" /> <!-- focused -->
<item android:state_selected="true" android:state_pressed="true" android:drawable="@color/black" /> <!-- focused and pressed-->
<item android:drawable="@color/black" /> <!-- default -->
</selector>
但是没有得到我之前的代码所做的事情。但它的作用是更改背景颜色并在点击时更改背景颜色,但不会使所选项目保持特定颜色
编辑: 我的适配器
public class ExtraAdapter extends ArrayAdapter<String> {
Context context;
String[] tittleArray;
public ExtraAdapter(Context c, String[] titles) {
super(c, R.layout.custom_listview_extra, titles);
this.context = c;
this.tittleArray = titles;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.custom_listview_extra, parent,
false);
}
TextView myTitle = (TextView) row.findViewById(R.id.plato_extra);
myTitle.setText(tittleArray[position]);
return row;
}
}
我的活动
public class ExtraMenu extends ActionBarActivity {
ListView lcategory;
ListView lextras;
ExtraAdapter adapter;
String[] categoria;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_extra_test);
lcategory = (ListView) findViewById(R.id.categorias);
String[] listaExtras = getResources().getStringArray(R.array.listaExtras);
adapter = new ExtraAdapter(getApplicationContext(), listaExtras);
lcategory.setAdapter(adapter);
lcategory.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
parent.setSelected(true);
/*
for (int a = 0; a< parent.getChildCount();a++){
parent.getChildAt(a).setBackgroundColor(Color.TRANSPARENT);
view.refreshDrawableState();
if(parent.getChildAt(a) == view){
view.setBackgroundColor(getResources().getColor(R.color.soft_opaque));
view.refreshDrawableState();
}
}*/
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.extra_test, menu);
return true;
}
}
我的活动XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:weightSum="2" >
<ImageView
android:id="@+id/logo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.5"
android:src="@drawable/logo" />
<ListView
android:id="@+id/categorias"
android:layout_weight="0.5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
//android:background="@drawable/list_color"
android:layout_marginTop="20dp">
</ListView>
</LinearLayout>
我的适配器XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Widget.AppCompat.ListView.Menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingTop="10dip"
android:paddingBottom="10dip">
<TextView
android:id="@+id/plato_extra"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
//android:background="@drawable/list_color"
android:textColor="#000000"
android:textSize="25sp" />
</RelativeLayout>
答案 0 :(得分:0)
当您按下行时,您将选择器变为黑色的方式。这是因为按下的视图也是聚焦的,第一次匹配是按下并聚焦的。更改命令abd摆脱onItemClick
的实现<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/green" /> <!-- pressed -->
<item android:state_focused="true" android:drawable="@color/black" /> <!-- focused -->
<item android:state_focused="true" android:state_pressed="true" android:drawable="@color/black" /> <!-- focused and pressed-->
<item android:drawable="@color/black" /> <!-- default -->
</selector>
答案 1 :(得分:0)
使用
listView.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
arg0.setSelected(true);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
在您的选择器Drawable
中<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/green" /> <!-- pressed -->
<item android:state_focused="true" android:drawable="@color/black" /> <!-- focused -->
<item android:state_selected="true" android:drawable="@color/black" /> <!-- selected-->
<item android:drawable="@color/black" /> <!-- default -->
</selector>
我没有测试过,但肯定会有用。