我必须在列表视图中更改多个选定项目(文本视图)的颜色。我在这里做的是,当用户从列表视图中选择项目时,颜色应更改为蓝色,当用户取消选择项目时,颜色应更改为默认颜色(此处为黑色)。我已经完成了一些教程并实现了一个小演示。但我不明白,如何处理颜色变化。下面是我的代码...
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_height="wrap_content"
android:id="@+id/listView2"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
列表项.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="fill_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
public View row;
ListView lview;
ListViewAdapter lviewAdapter;
private final static String month[] = {"January","February","March","April","May",
"June","July","August","September","October","November","December"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lview = (ListView) findViewById(R.id.listView2);
lviewAdapter = new ListViewAdapter(this, month);
lview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lview.setAdapter(lviewAdapter);
lview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
}
}
ListViewAdapter.java
public class ListViewAdapter extends BaseAdapter
{
ArrayList<Boolean> saved = new ArrayList<Boolean>();
Activity context;
String title[];
String description[];
public ListViewAdapter(Activity context, String[] title) {
super();
this.context = context;
this.title = title;
}
public int getCount() {
// TODO Auto-generated method stub
return title.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView txtViewTitle;
}
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.listitem_row, null);
holder = new ViewHolder();
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.txtViewTitle.setText(title[position]);
return convertView;
}
}