gpt4 book ai didi

android - 如何更改特定 ListView 的项目或元素的颜色

转载 作者:行者123 更新时间:2023-11-29 15:13:46 25 4
gpt4 key购买 nike

我有一个 ListView,它由一个字符串数组扩充。在 ListView 的顶部,我添加了一个按钮,我希望每当用户单击该按钮时,4 个项目的颜色会自动更改。

这是我的 Activity 代码:

    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.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或者像这样的简单的For:

for(int i=0;i<listView.getChildCount();i++) {
View item = listView.getChildAt(i);
// some yours code
}

关于android - 如何更改特定 ListView 的项目或元素的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26868907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com