[英]How to avoid selector while dragging image in Gallery in Android
我在圖庫中顯示了一系列圖像。 當用戶點擊一個時,他們會被帶到不同的視圖。 我想在用戶執行點擊時向用戶提供一些反饋,就在視圖更改之前。
我因此定義了一個選擇器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/pic_frame_pressed" />
<item android:drawable="@drawable/pic_frame" /> <!-- default -->
</selector>
這行得通。 當用戶單擊圖庫中的圖像時,會顯示一個框架...
但是,當用戶使用圖像前后拖動圖庫時也會顯示。
我查看了可以為選擇器找到的不同狀態:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:color="hex_color"
android:state_pressed=["true" | "false"]
android:state_focused=["true" | "false"]
android:state_selected=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_window_focused=["true" | "false"] />
</selector>
我嘗試了它們,但找不到僅在用戶單擊圖像時觸發的組合(例如,“選擇”總是針對位於圖庫中心的圖像觸發)
我需要一種“state_clicked”...
我在這里面臨同樣的情況
我找到的唯一解決方案是在 OnItemClick 事件上自己處理后台切換(這並不優雅:/):
mGallery.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View clickedView, int position, long id) {
for(int i = 0; i < parent.getChildCount(); i++){
parent.getChildAt(i).setBackgroundResource(R.drawable.bg_menu);
((TextView) parent.getChildAt(i)).setTextColor(getResources().getColor(R.color.white));
}
clickedView.setBackgroundResource(R.drawable.bg_menu_selected);
((TextView) clickedView).setTextColor(getResources().getColor(R.color.dark_blue));
((SectionAdapter) mGallery.getAdapter()).setSelected(position);
}
});
如您所見,我將選定的 position 存儲在我的適配器上,以便在重繪時以正確的布局顯示項目(失去焦點)
我真的很想找到另一個解決方案,但我還沒有
如果有人得到它,我會很高興聽到:D
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.