0

i am a coding beginner, so sorry for my bad explanation of the problem.

In my MainActivity I have a full screen Viewpager, which changes its content (the xml-file) every time I swipe left or right. These collection of views is defined in a enum, I have also used a CustomPagerAdapter for that. This works fine, but when I implement an onTouchListener in my MainActivity, the "swiping" doesn't work anymore.

Does anyone know, how to solve that?

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Main = (View) findViewById(R.id.MainActivity); viewPager = (ViewPager) findViewById(R.id.colorgroup); viewPager.setAdapter(new CustomPagerAdapter(this)); View.OnTouchListener handleTouch = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int x = (int) event.getX(); int y = (int) event.getY(); viewPager.setDrawingCacheEnabled(true); viewPager.buildDrawingCache(); Bitmap bitmap = viewPager.getDrawingCache(); int pixel = bitmap.getPixel(x, y); String hexColor = String.format( "#%02x%02x%02x", r, g, b ); return true; } }; viewPager.setOnTouchListener(handleTouch); } 

public class CustomPagerAdapter extends PagerAdapter {

private Context mContext; public CustomPagerAdapter(Context context) { mContext = context; } @Override public Object instantiateItem(ViewGroup collection, int position) { CustomPagerEnum customPagerEnum = CustomPagerEnum.values()[position]; LayoutInflater inflater = LayoutInflater.from(mContext); ViewGroup layout = (ViewGroup) inflater.inflate(customPagerEnum.getLayoutResId(), collection, false); collection.addView(layout); return layout; } @Override public void destroyItem(ViewGroup collection, int position, Object view) { collection.removeView((View) view); } @Override public int getCount() { return CustomPagerEnum.values().length; } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public CharSequence getPageTitle(int position) { CustomPagerEnum customPagerEnum = CustomPagerEnum.values()[position]; return mContext.getString(customPagerEnum.getTitleResId()); } 

}

public enum CustomPagerEnum {

YELLOW(R.string.YELLOW, R.layout.view_yellow), PUCE(R.string.PUCE, R.layout.view_puce), BROWN(R.string.BROWN, R.layout.view_brown), GREEN(R.string.GREEN, R.layout.view_green), CYAN(R.string.CYAN, R.layout.view_cyan), BLUE(R.string.BLUE, R.layout.view_blue), RED(R.string.RED, R.layout.view_red), PINK(R.string.PINK, R.layout.view_pink), PURPLE(R.string.PURPLE, R.layout.view_purple), SLATE(R.string.SLATE, R.layout.view_slate), GREY(R.string.GREY, R.layout.view_grey), DARK(R.string.DARK, R.layout.view_dark); private int mTitleResId; private int mLayoutResId; CustomPagerEnum(int titleResId, int layoutResId) { mTitleResId = titleResId; mLayoutResId = layoutResId; } public int getTitleResId() { return mTitleResId; } public int getLayoutResId() { return mLayoutResId; } 

}

1
  • Don't use enums they require too much overhead Commented Dec 8, 2016 at 14:42

1 Answer 1

2

You always return true from the onTouch() method. This signifies that your handler has consumed the touch event and so the event is not further processed. This results in the swipe never working. Return false instead.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.