Android 如何将 Viewpager 与 OnTouchListener 一起使用
Android how to use Viewpager with OnTouchListener
我是一名编码初学者,对于我对问题的错误解释深表歉意。
在我的 MainActivity 中,我有一个全屏 Viewpager,它会在我每次向左或向右滑动时更改其内容(xml-文件)。这些视图集合在枚举中定义,我还为此使用了 CustomPagerAdapter。
这工作正常,但是当我在我的 MainActivity 中实现 onTouchListener 时,"swiping" 不再工作。
有谁知道,如何解决?
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 扩展了 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 枚举 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;
}
}
你总是returntrue
来自onTouch()
方法。这表示您的处理程序已使用触摸事件,因此不会进一步处理该事件。这导致滑动无法正常工作。 Return false
代替。
我是一名编码初学者,对于我对问题的错误解释深表歉意。
在我的 MainActivity 中,我有一个全屏 Viewpager,它会在我每次向左或向右滑动时更改其内容(xml-文件)。这些视图集合在枚举中定义,我还为此使用了 CustomPagerAdapter。 这工作正常,但是当我在我的 MainActivity 中实现 onTouchListener 时,"swiping" 不再工作。
有谁知道,如何解决?
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 扩展了 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 枚举 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;
}
}
你总是returntrue
来自onTouch()
方法。这表示您的处理程序已使用触摸事件,因此不会进一步处理该事件。这导致滑动无法正常工作。 Return false
代替。