0

我有一个画廊,在那里我实现了一个扩展 BaseAdapter 的自定义适配器。对于某些项目,我希望它们被禁用,并且不可点击。

覆盖 isEnabled(int) 方法不起作用。我仍然可以单击禁用的项目,然后画廊将这个项目置于其中。

有任何想法吗?

4

2 回答 2

2

以下是图库小部件的相关源代码

  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);
}

试着让我知道。

于 2012-05-18T16:34:48.897 回答
0

试试下面的代码,您可以在其中处理特定位置的点击事件。您也可以禁用特定项目。

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;
        }
    }
}
于 2012-05-20T07:20:52.590 回答