滑動事件問題
阿新 • • 發佈:2018-11-09
最近用到了Gallery這個控制元件,我的adapter是這樣寫的:
public GalleryAdapter(Context mContext, List<String> mData) { this.mContext = mContext; this.mData = mData; } // 決定了item顯示的個數 @Override public int getCount() { return mData.size(); } // 根據postition獲取對應item的資料 @Override public Object getItem(int position) { returnmData.get(position); } // 獲取對應postition的item的ID @Override public long getItemId(int position) { return position; } // 建立對應的item的檢視(View) @Override public View getView(final int position, View convertView, ViewGroup parent) { if (null == convertView) { convertView = new ImageView(mContext); convertView.setLayoutParams(new Gallery.LayoutParams(200, 400)); } // 設定image大小 ((ImageView) convertView).setScaleType(ImageView.ScaleType.FIT_CENTER); convertView.setPadding(5, 5, 5, 5); GliderManager.getGlide().load(mData.get(position), com.chad.library.R.drawable.abc_list_focused_holo, ((ImageView) convertView)); convertView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (null != mItemClickListener) mItemClickListener.onItemClick(v, position); } }); return convertView; } public void setOnItemClickListener(ESTOnItemClickListener listener) { mItemClickListener = listener; }
這麼看看沒什麼問題(至少我感覺沒什麼問題),但是就是這樣後居然滑動不了,後來看了看攔截事件的方法,Gallery沒有重寫這個方法,這個很是鬱悶了,在網上找到了這個替代的方案,不使用onClickListener,使用onTouchListener然後return為false這樣就成功的把事件傳遞到Gallery了改後是這樣的
public GalleryAdapter(Context mContext, List<String> mData) { this.mContext = mContext; this.mData = mData; } // 決定了item顯示的個數 @Override public int getCount() { return mData.size(); } // 根據postition獲取對應item的資料 @Override public Object getItem(int position) { return mData.get(position); } // 獲取對應postition的item的ID @Override public long getItemId(int position) { return position; } // 建立對應的item的檢視(View) @Override public View getView(final int position, View convertView, ViewGroup parent) { if (null == convertView) { convertView = new ImageView(mContext); convertView.setLayoutParams(new Gallery.LayoutParams(200, 400)); } // 設定image大小 ((ImageView) convertView).setScaleType(ImageView.ScaleType.FIT_CENTER); convertView.setPadding(5, 5, 5, 5); GliderManager.getGlide().load(mData.get(position), com.chad.library.R.drawable.abc_list_focused_holo, ((ImageView) convertView)); convertView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (null != mItemClickListener) mItemClickListener.onItemClick(v, position); return false; } }); return convertView; } public void setOnItemClickListener(ESTOnItemClickListener listener) { mItemClickListener = listener; }