Android 如何更改特定listview项或元素的颜色
我有一个列表视图,它由一个字符串数组膨胀。在ListView的顶部,我添加了一个按钮,我希望每当用户单击按钮时,4个项目的颜色都会自动更改 这是我的活动代码:Android 如何更改特定listview项或元素的颜色,android,android-layout,listview,android-listview,Android,Android Layout,Listview,Android Listview,我有一个列表视图,它由一个字符串数组膨胀。在ListView的顶部,我添加了一个按钮,我希望每当用户单击按钮时,4个项目的颜色都会自动更改 这是我的活动代码: public class MainActivity extends Activity { String[] countryNames = {"Germany", "France", "Italy", "Spain", "Russia", "USA", "Iran", "China", "Inida"}; B
public class MainActivity extends Activity
{
String[] countryNames = {"Germany", "France", "Italy", "Spain", "Russia", "USA", "Iran", "China", "Inida"};
Button button;
ListView listView;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
button = (Button) findViewById(R.id.button_changeColor);
ListViewAdapter adapter = new ListViewAdapter(getApplicationContext());//
listView = (ListView) findViewById(R.id.listView_1);//
listView.setAdapter(adapter);//
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// here i want to change the the color 4th item.
}
});
}
private class ListViewAdapter extends BaseAdapter
{
LayoutInflater layoutInflater;
public ListViewAdapter(Context context)
{
layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount()
{
return countryNames.length;
}
@Override
public Object getItem(int position)
{
return null;
}
@Override
public long getItemId(int position)
{
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
ViewHolder viewHolder;
if (convertView == null)
{
convertView = layoutInflater.inflate(R.layout.listview_template, null);
viewHolder = new ViewHolder();
viewHolder.addedTextView = (TextView) convertView.findViewById(R.id.textView_Template);
convertView.setTag(viewHolder);
} else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.addedTextView.setText(countryNames[position]);
return convertView;
}
}
private class ViewHolder
{
TextView addedTextView;
}
}
这里还有我的listview.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Color"
android:id="@+id/button_changeColor"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="300dp"
android:id="@+id/listView_1">
</ListView>
</LinearLayout>
最后是listview_template.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView_Template"
/>
</LinearLayout>
因此,我的问题是,如何通过单击按钮来更改特定项目的颜色。由于滚动时Listview会重复使用视图,因此需要在适配器的getView方法中重新设置颜色
然后让按钮单击listener,设置布尔值:
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
adapter.isHighlighted = true;
}
});
同样,您也可以在适配器内创建一个可变位置,并使用它突出显示特定项。由于滚动时Listview会重新使用视图,因此需要在适配器的getView方法中重新设置颜色
然后让按钮单击listener,设置布尔值:
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
adapter.isHighlighted = true;
}
});
同样,您也可以在适配器内创建一个可变位置,并使用它突出显示特定项目。像这样吗
listView.getChildAt( 4 );
View item_4 = listView.getChildAt(4);
item_4.setBackgroundResource( <new_background> );
( (TextView) item_4.findViewById( R.id.text1 ) ).setTextColor( <new_font_color> );
如果需要更改所有项目,可以使用cicle with iterator或simple,如下所示:
for(int i=0;i<listView.getChildCount();i++) {
View item = listView.getChildAt(i);
// some yours code
}
像那样
listView.getChildAt( 4 );
View item_4 = listView.getChildAt(4);
item_4.setBackgroundResource( <new_background> );
( (TextView) item_4.findViewById( R.id.text1 ) ).setTextColor( <new_font_color> );
如果需要更改所有项目,可以使用cicle with iterator或simple,如下所示:
for(int i=0;i<listView.getChildCount();i++) {
View item = listView.getChildAt(i);
// some yours code
}
你是一个储蓄者!!!相信我,谢谢这正是我想要的…你是一个储蓄者!!!相信我,谢谢这正是我想要的…你的答案看起来是正确的,但不幸的是我无法实现你的答案看起来是正确的,但不幸的是我无法实现