使用OnLongClickListener查看寻呼机不起作用

时间:2017-10-17 08:55:51

标签: android android-fragments android-viewpager onlongclicklistener

我有一个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);
}

1 个答案:

答案 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;
}