我有一个ViewPager
我正在尝试应用onLongClickListener
来更改无效的片段。
mPager1.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
onViewPagerClick();
return false;
}
});
片段替换:
public void onViewPagerClick() {
FragmentTransaction ft5 = getSupportFragmentManager().beginTransaction();
fragment = new SingleProductFragment();
container.setVisibility(View.VISIBLE);
ft5.add(R.id.container, fragment);
// ft.addToBackStack(null);
ft5.commit();
sv.setVisibility(View.INVISIBLE);
addButton.setVisibility(View.INVISIBLE);
}
答案 0 :(得分:0)
ViewPager不会使用点击事件,因此请尝试在instantiateItem()
中设置图片或容器布局上的侦听器,而不是viewPager。
<强> 更新 强>
@Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
ViewGroup viewPagerItem = (ViewGroup)inflater.inflate(R.layout.viewPager_item, container, false);
ImageView imageView = (ImageView)viewPagerItem.findViewById(R.id.imageView);
viewPagerItem.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Toast.makeText(MainActivity.this, "TEST", Toast.LENGTH_LONG).show();
return false;
}
});
container.addView(viewPagerItem);
return viewPagerItem;
}