public class CGallery extends Gallery
{
public interface IOnItemClickListener
{
public void onItemClick(int position);
}
public void setOnItemClickListener(IOnItemClickListener listener)
{
clickListener = listener;
}
public boolean onSingleTapUp(android.view.MotionEvent e)
{
try
{
Field f = CGallery.class.getSuperclass().getDeclaredField("mDownTouchPosition");
f.setAccessible(true);
int position = f.getInt(this);
if(clickListener!=null&&position>=0)
{
clickListener.onItemClick(position);
}
}
catch(SecurityException e1)
{
e1.printStackTrace();
}
catch(NoSuchFieldException e1)
{
e1.printStackTrace();
}
catch(IllegalArgumentException e2)
{
e2.printStackTrace();
}
catch(IllegalAccessException e3)
{
e3.printStackTrace();
}
return false;
}
}
//////////////////////////////////////////////////////////////////////////////////上面是gallery的新类
newGallery.setOnItemClickListener(new CGallery.IOnItemClickListener()
{
public void onItemClick(int position)
{
此处是gallery子项的单击事件
}
});
下面是gallery的布局
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageview_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:background="@layout/gallery_item_selector_image" />
下面是gallery_item_selector_image.xml文件内容
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="true" android:state_focused="true" android:drawable="@drawable/ic_gallery_item_selected" />
<item android:state_window_focused="false" android:state_focused="true" android:drawable="@drawable/ic_gallery_item_selected" />
<item android:state_window_focused="false" android:drawable="@color/cl_beige" />
<item android:state_selected="true" android:drawable="@drawable/ic_gallery_item_selected" />
<item android:state_pressed="true" android:drawable="@drawable/ic_gallery_item_selected" />
<item android:drawable="@color/cl_beige" />
</selector>
问题在下面:
由于修改了public boolean onSingleTapUp(android.view.MotionEvent e)后,无法实现gallery_item_selector_image.xml的效果,
请问在修改onSingleTapUp后无法触发state_selecte
------解决方案--------------------
明天来看看。。