嗨,我是一个新的Android开发者。我试图改变一些元素在我的项目上一次点击,这是我的代码
ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ImageView img = findViewById(R.id.imageView2);
img.setImageResource(R.drawable.validate);
TextView txtNumTicket, txtCaisse;
txtNumTicket = findViewById(R.id.ref);
txtCaisse = findViewById(R.id.textView);
txtNumTicket.setTextColor(ContextCompat.getColor(Commande.this, R.color.bon));
txtCaisse.setTextColor(ContextCompat.getColor(Commande.this, R.color.bon));
Toast.makeText(getApplicationContext(), "You have selected item no."
+ (i + 1) + "", Toast.LENGTH_SHORT).show();
}
});
我想恢复到初始状态(改变形象R.id.novalidate和颜色的TextView)位置时,等于6,我该怎么办呢?
评论后更新:
if (yourListView.getAdapter().getCount() == 6) {
img.setImageResource(R.drawable.novalidate);
txtNumTicket.setTextColor(ContextCompat.getColor(Commande.this, R.color.your_new_colour));
txtCaisse.setTextColor(ContextCompat.getColor(Commande.this, R.color.your_new_colour));
}
2ndUPDATE:基于this answer,这里就是你可以在你的列表中的特定位置获得一个视图
public View getViewByPosition(int pos, ListView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
if (pos < firstListItemPosition || pos > lastListItemPosition ) {
return listView.getAdapter().getView(pos, null, listView);
} else {
final int childIndex = pos - firstListItemPosition;
return listView.getChildAt(childIndex);
}
}
所以,你可以得到你想要通过调用视图:
View view = getViewByPosition (number_of_the_view_you_want_to_change, name_of_your_listview);
然后结合该项目的textviews / imageviews(view.findViewById(...)),并如上所述设置的颜色和文本