3
我有一個ViewPagerActivity
與兩個視圖。我想註冊一個上下文菜單,對於每個視圖都是不同的。我嘗試了一些東西(registerForContextMenu()
),但是我總是以兩個視圖結束,就像第二個視圖一樣。有任何想法嗎?ViewPager與ContextMenu這是不同的每個視圖
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
對於您的問題,可能有更簡單的方法,但我在我的應用程序中使用了此方法。我使用了ViewPager的源代碼,並通過添加一個返回當前位置的方法對其進行了修改。從內存中,你只需要添加如下內容:'public int getCurrentItem(){return 0; mCurItem; }'。然後你可以在你的'onContextItemSelected'方法中調用它。 –
我做了不同的嘗試,但我肯定會在未來嘗試您的解決方案。看起來不錯...謝謝 – user219882