4

我有ViewPagerActivity两种观点。我想注册一个上下文菜单,每个视图都不同。我尝试了一些东西(registerForContextMenu()),但我总是以两种视图的行为方式结束第二个视图。有任何想法吗?

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
    }

}
4

1 回答 1

2

问题是两个视图是同时实例化的(填充 viewpager)。在第二个视图中设置onRegisterForContextMenu是从第一个视图覆盖它。

onContextItemSelected一种可能的解决方案是检查您的方法中可见的视图。

于 2012-05-03T11:01:23.710 回答