我想根据是否点击该项目来更改列表项的背景颜色。我怎样才能做到这一点!我确实尝试了以下内容:
articleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// Find the current article that was clicked on
Article currentArticle = mAdapter.getItem(position);
if (currentArticle.getUrl() != null) {
TextView article_TV = (TextView) findViewById(R.id.post_title);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
article_TV.setBackgroundColor(getApplicationContext().getColor(R.color.colorItemClicked));
}
else
article_TV.setBackgroundColor(getResources().getColor(R.color.colorItemClicked));
}
}
});
更新: - 这是愚蠢的错误。正如ak sacha所建议的那样 TextView article_TV =(TextView)view.findViewById(R.id.post_title);
答案 0 :(得分:1)
正如ak sacha所建议的,我们可以通过使用它来实现它 TextView article_TV =(TextView)view.findViewById(R.id.post_title); 这是因为我们在适配器中使用它,所以我们需要在listview row中查找视图。我们使用view.findviewbyid