[英]android listview and background color
嗨,我正在使用以下xml作为我的customlistview背景
当我单击一个项目时,它会突出显示,但不会持续存在,只会产生可点击的效果,我想在单击时在列表视图中突出显示一个特定的项目
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<!--ON FOCUS -->
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="@@drawable/appcategoryselectedimg" />
<!--ON CLICK-->
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@@drawable/appcategoryselectedimg" />
<!--long touch and simple touch and release-->
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@@drawable/appcategoryselectedimg" />
<!--Default -->
<item
android:drawable="@color/transperent" />
</selector>
请参考以下代码获取示例活动ListViewGeneralSampleProjectActivity.java
public class ListViewGeneralSampleProjectActivity extends Activity {
private ListView sampleListView;
private static int save = -1;
int pos = 0;
MySimpleArrayAdapter adapter;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2","hai","Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2","hai","OS/2","hai", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2","hai","OS/2","hai" };
sampleListView=(ListView) findViewById(R.id.listView1);
adapter = new MySimpleArrayAdapter(this, values);
sampleListView.setAdapter(adapter);
sampleListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),position+" Pressed", Toast.LENGTH_SHORT).show();
adapter.setCurrent(position);
adapter.notifyDataSetChanged();
}
});
}
}
MySimpleArrayAdapter.java
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private int selected=0;
public MySimpleArrayAdapter(Context context, String[] values) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.textView1);
textView.setText(values[position]);
if(position==selected)
{
rowView.setBackgroundColor(Color.RED);
}
return rowView;
}
public void setCurrent(int position) {
// TODO Auto-generated method stub
this.selected=position;
}
}
main.xml
<?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" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
rowlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
因为您单击简单。 您需要为用户单击项目时为state_selected和setSelected(true)
定义可绘制对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.