i have implemented view pager in my code and i have put on-click listener on view pager but my on-click listener does not working i\'ll post my code
viewpager.s
There is no onClick listener in ViewPager, You can use the same effect by using a Recyclerview instead of a ViewPager is shown as below
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_scroll_page"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
Using Recyclerview as horizontal then you can scroll it and get the click events. Please note that use the RecyclerView.Adapter
Use clicklistener inside the item of ViewPager. This is not working because Child of ViewPager is intercepting event of parent(i.e ViewPager). E.G if you are adding ImageView inside ViewPager using PagerAdapter then use click of ImageView to make it simple
I implemented custom ViewPager
:
public class ClickableViewPager extends ViewPager {
private OnClickListener mOnClickListener;
public ClickableViewPager(Context context) {
super(context);
setup();
}
public ClickableViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
setup();
}
private void setup() {
final GestureDetector tapGestureDetector = new GestureDetector(getContext(), new TapGestureListener());
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
tapGestureDetector.onTouchEvent(event);
return false;
}
});
}
public void setOnViewPagerClickListener(OnClickListener onClickListener) {
mOnClickListener = onClickListener;
}
public interface OnClickListener {
void onViewPagerClick(ViewPager viewPager);
}
private class TapGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
if(mOnClickListener != null) {
mOnClickListener.onViewPagerClick(ClickableViewPager.this);
}
return true;
}
}
}
And example how to implement onClickListener
:
ClickableViewPager viewPager = (ClickableViewPager) findViewById(R.id.viewPager);
mViewPager.setOnViewPagerClickListener(new ClickableViewPager.OnClickListener() {
@Override
public void onViewPagerClick(ViewPager viewPager) {
//yourCode
}
});