我有一個畫廊,我已經實現了擴展BaseAdapter的自定義適配器。對於某些項目,我希望它們被禁用,並且不可點擊。如何禁用圖庫的項目?
重寫isEnabled(int)方法不起作用。我仍然可以點擊禁用的項目,然後,畫廊將其中心放置在該項目中。
任何想法?
我有一個畫廊,我已經實現了擴展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;
}
}
}
你有試過嗎? –
try setFocusable(false);可能會工作 – ingsaurabh
這也行不通。 onItemClick事件被調用。 –
你可以粘貼你的代碼或來到這個房間http://chat.stackoverflow.com/rooms/10629/agarwal –