如何在android中的viewpage图像上打开longclick上下文菜单

时间:2015-06-22 10:31:44

标签: android android-viewpager contextmenu

我是android和developing wallpaper application的新手。我使用view-pager创建了一个页面,其中所有wallpaper slides one by one.

现在,用户open context menu时我必须long-press on any one image。我尝试了很多次也搜索但没有找到任何地方。

使用下面的代码:

public void onCreate(Bundle savedInstanceState) {

viewPager = (ViewPager) findViewById(R.id.view_pager);
int position = getIntent().getExtras().getInt("position");
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.setCurrentItem(position);
viewPager.setOnPageChangeListener(this);

registerForContextMenu(viewPager);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Picture Options");
    menu.add(0, v.getId(), 0, "Set Picture as");
    menu.add(0, v.getId(), 0, "Share");

}

适配器代码

public class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
}

@Override
public int getCount() {
    return GridViewDemo.mThumbIds.length;
}

@Override
    public Fragment getItem(final int position) {
        return new Fragment() {
            @Override
            public View onCreateView(LayoutInflater inflater,
                ViewGroup container, Bundle savedInstanceState) {
                ImageView imageView = new ImageView(FullImageView.this);
                imageView.setImageResource(GridViewDemo.mThumbIds[position]);
                return imageView;
                }
            };
        }
}

如果有任何经验,请告诉我。

由于

1 个答案:

答案 0 :(得分:0)

长时间点击监听器是否适合您?

   imageView.setOnLongClickListener(new OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        //your code 
        return true;
    }
});