Clickable ViewPager onItem Click在片段内不起作用

时间:2019-01-04 11:26:42

标签: android android-viewpager

我拥有自定义viewpager以实现onItemClick,因此我从here那里获得了参考。当我将Viewpager放入Activity时,它工作正常,但是当我放入片段中时,它将停止触发onItemClick事件。

我正在使用数据绑定进行活动和片段代码。我猜正确的触摸事件没有传递给自定义viewpager。

如何在自定义viewpager上实现onItemClick?

如果我在任何地方错了,请纠正我。

谢谢。

自定义查看器

public class ClickableViewPager extends ViewPager {
        private OnViewPagerItemClickListener mOnItemClickListener;
        private GestureDetector tapGestureDetector;

    public ClickableViewPager(Context context) {
        super(context);
        setup();
    }

    public ClickableViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        setup();
    }

    private void setup() {
        tapGestureDetector = new GestureDetector(getContext(), new TapGestureListener());

        this.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                DebugHelper.log("ClickableViewPager : OnTouchListener");
                tapGestureDetector.onTouchEvent(event);
                return false;
            }
        });


    }

    public void setOnItemClickListener(OnViewPagerItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface OnViewPagerItemClickListener {
        void onViewPagerItemClick(int position);
    }

    private class TapGestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            DebugHelper.log("ClickableViewPager : TapGestureListener : onSingleTapConfirmed");
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onViewPagerItemClick(getCurrentItem());
            }
            return true;
        }
    }

}

查看寻呼机适配器

public class OnGoingServiceAdapter extends PagerAdapter {

    private final LayoutInflater inflater;
    private Activity activity;
    private List<HistoryModel> onGoingServiceList;
    private Interfaces.OnMoreServiceClick onMoreServiceClick;

    public OnGoingServiceAdapter(Activity context, List<HistoryModel> onGoingServiceList, Interfaces.OnMoreServiceClick onMoreServiceClick) {
        activity = context;
        inflater = LayoutInflater.from(context);
        this.onGoingServiceList = onGoingServiceList;
        this.onMoreServiceClick = onMoreServiceClick;
    }


    @Override
    public Object instantiateItem(ViewGroup collection, int position) {
        OngoingServiceItemBinding binding = DataBindingUtil.inflate(inflater, R.layout.ongoing_service_item, collection, false);
        binding.setOnGoingService(this.onGoingServiceList.get(position));
        binding.ivCall.setTag(position);
        binding.ivCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (int) v.getTag();
                StaticUtils.onPhoneClick(activity, getItem(position).getPhone());
            }
        });
        binding.tvMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onMoreServiceClick != null) {
                    onMoreServiceClick.onMoreCurrentServiceItemClick();
                }
            }
        });
        collection.addView(binding.getRoot());
        return binding.getRoot();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = (View) object;
        ((ViewPager) container).removeView(view);
        view = null;
    }

    @Override
    public int getCount() {
        return this.onGoingServiceList == null ? 0 : this.onGoingServiceList.size();   //onGoingServiceList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    public HistoryModel getItem(int position) {
        return this.onGoingServiceList.get(position);
    }

    public void updateData(List<HistoryModel> servicesList) {
        this.onGoingServiceList = servicesList;
        notifyDataSetChanged();
    }
}

0 个答案:

没有答案