仅在点击更改项状态绘制 - 图库视图 [英] Change item state drawable only on click - GalleryView
问题描述
我使用的是带有图库视图一个BaseAdapter
没问题,来改变这取决于项目状态(选择)的背景和文本颜色
但我想改变只有当单击该项目不只是选择的背景和文本颜色,用
在2个字,如果该项目没有点击
背景不能改变
我怎么能走呢?
I use a GalleryView to display string with a BaseAdapter
No problem to change the background and text color depending on the item state (selector)
But I want to change the background and text color only if the item is clicked not just selected, the GalleryView item state changes with the selected event which is called when you fling around (the centered item is automatically selected)
In 2 words the background mustn't change if the item isn't clicked
How can I go with it ?
推荐答案
您必须捕获选定事件以及更改视图以未被选中的背景。
储存最后单击的位置,以避免改变其选定的背景。此外,店内最后单击视图恢复未选择的背景时,点击其他项目不丢:
You must capture the selected event and change the background of the view to unselected. Store last clicked position to avoid changing its selected background. Also, store last clicked view to restore unselected background when another item is clicked without flinging:
gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new DaysGalleryAdapter(this, new int[]{R.layout.gallery_item}, new int[]{R.id.tv_gallery}));
gallery.setOnItemClickListener(mOnGalleryClick);
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(position != savedClickedPosition)
view.setBackgroundResource(R.drawable.bg_unselected);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
private OnItemClickListener mOnGalleryClick = new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
lastItemSelected.setBackgroundResource(R.drawable.tab_bg_unselected);
view.setBackgroundResource(R.drawable.tab_bg_selected);
lastItemSelected = view;
savedClickedPosition = position;
// Do stuff
}
};
如果为时已晚你Fluckysan至少希望它有助于谁是有同样的问题的人。
编码快乐!
If it's too late for you Fluckysan at least hope it helps to anyone who's having the same problem. Happy coding!
这篇关于仅在点击更改项状态绘制 - 图库视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!