如何禁用图库的项目?
问题描述:
我有一个画廊,我已经实现了扩展BaseAdapter的自定义适配器。对于某些项目,我希望它们被禁用,并且不可点击。如何禁用图库的项目?
重写isEnabled(int)方法不起作用。我仍然可以点击禁用的项目,然后,画廊将其中心放置在该项目中。
任何想法?
答
这下面是画廊插件
public boolean onSingleTapUp(MotionEvent e) {
if (mDownTouchPosition >= 0) {
// An item tap should make it selected, so scroll to this child.
scrollToChild(mDownTouchPosition - mFirstPosition);
// Also pass the click so the client knows, if it wants to.
if (mShouldCallbackOnUnselectedItemClick || mDownTouchPosition == mSelectedPosition) {
performItemClick(mDownTouchView, mDownTouchPosition, mAdapter
.getItemId(mDownTouchPosition));
}
return true;
}
return false;
}
正如你所看到的相关源代码,画廊滚动轻敲的子项在其上进行点击之前。 因此,真正禁用物品的唯一方法是将其延伸Gallery
并覆盖其中的onSingleTapUp(MotionEvent e)
。
@Override
public boolean onSingleTapUp(MotionEvent e) {
int itemPosition = pointToPosition((int) e.getX(), (int) e.getY());
if (item at itemPosition is disabled) {
// Do nothing.
return true;
}
return super.onSingleTapUp(e);
}
试着让我知道。
答
尝试下面的代码,您可以在其中处理特定位置的点击事件。您也可以禁用特定项目。
public class SplashActivity extends Activity{
private Activity _activity;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Gallery g = new Gallery(this);
g.setAdapter(new ImageAdapter(this));
setContentView(g);
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.menu1,
R.drawable.menu2,
R.drawable.menu3,
R.drawable.menu4,
R.drawable.menu1,
R.drawable.menu2,
R.drawable.menu3,
R.drawable.menu4
};
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
if(position!=0){
i.setEnabled(false);
i.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(SplashActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
return i;
}
}
}
+0
你有试过吗? –
try setFocusable(false);可能会工作 – ingsaurabh
这也行不通。 onItemClick事件被调用。 –
你可以粘贴你的代码或来到这个房间http://chat.stackoverflow.com/rooms/10629/agarwal –