4

I have a ViewPagerActivity with two views. I want to register a context menu, different for each view. I tried a few things (registerForContextMenu()) but I always ended up with both views behaving like the second one. Any ideas?

public class ViewPagerActivity extends UomeActivity {

    private ViewPager viewPager;        
    private ViewPagerAdapter viewPagerAdapter;
    private ListAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_pager);

        refreshAdapter();
    }

    private class ViewPagerAdapter extends PagerAdapter {

        private final List<Transaction> accountTransactions;
        private final List<User> accountUsers;

        public ViewPagerAdapter() {
            // initialize lists
        }

        @Override
        public Object instantiateItem(View collection, int viewPosition) {
            View layout;
            ListView listView;
            switch (viewPosition) {
                case 0:
                    layout = LayoutInflater.from(getApplicationContext()).inflate(
                            R.layout.list_account_paired_transactions, null);

                    List<PairedTransaction> pairedTransactions = getPairedTransactions();

                    adapter = new AccountPairedTransactionItemAdapter(ViewPagerActivity.this,
                            R.layout.list_account_paired_transactions, pairedTransactions);

                    listView = (ListView) layout.findViewById(android.R.id.list);
                    listView.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
                            // do something
                        }
                    });
                    registerForContextMenu(listView);
                    break;
                case 1:
                    layout = LayoutInflater.from(getApplicationContext()).inflate(
                            R.layout.list_account_transactions, null);
                    initViewPagerInfo(layout, R.string.paired_transactions,
                            R.string.all_account_transactions, R.string.empty, viewPosition);

                    adapter = new TransactionItemAdapter(ViewPagerActivity.this,
                            R.layout.list_account_transactions, accountTransactions,
                            new UserDaoImpl(
                                    getApplicationContext()));

                    listView = (ListView) layout.findViewById(android.R.id.list);
                    registerForContextMenu(listView);
                    break;
                default:
                    throw new IllegalArgumentException("Unsupported number of views.");
            }

            listView.setAdapter(adapter);
            listView.setDivider(new ColorDrawable(R.color.list_separator));
            listView.setDividerHeight(1);
            listView.setBackgroundColor(Color.WHITE);
            listView.setCacheColorHint(Color.WHITE);
            ((ViewPager) collection).addView(layout);

            return layout;
        }

        // omitted

    }

    @Override
    protected void onResume() {
        refreshAdapter();
        super.onResume();
    }

    private void refreshAdapter() {
        viewPagerAdapter = new ViewPagerAdapter();
        viewPager = findById(R.id.view_pager);
        viewPager.setAdapter(viewPagerAdapter);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
        menu.add(R.string.show_info);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // TODO: toast which object has been selected
    }

}
user219882
  • 15,274
  • 23
  • 93
  • 138

1 Answers1

2

The problem is that both views are instantiated at the same time (filling the viewpager). Setting the onRegisterForContextMenu in the second view is overwriting it from the first view.

A possible solution would be to check to see what view is visible in you onContextItemSelected method.

David Scott
  • 1,666
  • 12
  • 22
  • I thought so. Do you have any examples how to check which view I have and how to get the right item? – user219882 May 03 '12 at 11:18
  • There's probably a more simple way for you problem, but I have used this method in my apps. I used the source code for a ViewPager and modified it by adding a method to return the current position. From memory you just need to adding something like: `public int getCurrentItem() { return mCurItem; }`. You can then call this in your `onContextItemSelected` method. – David Scott May 03 '12 at 11:57
  • I made it differently but I definitely try your solution in the future. Seems nice... Thanks – user219882 May 03 '12 at 12:57