我有以下课程:
public class ChannelsListTabsFragment extends Fragment {
private SlidingTabLayout mSlidingTabLayout;
private ViewPager mViewPager;
public ListChannelsPagerAdapter listChannelsPagerAdapter;
private ChannelCountriesModel[] tabTitles;
private Context applicationContext;
private String session;
private long time_offset;
private ListView listViewFragment;
public static ChannelsListTabsFragment newInstance(String session, ChannelCountriesModel[] tabTitles, long time_offset) {
ChannelsListTabsFragment fragment = new ChannelsListTabsFragment();
Bundle args = new Bundle();
args.putParcelableArray("tabTitles", tabTitles);
args.putString("session", session);
args.putLong("time_offset", time_offset);
fragment.setArguments(args);
return fragment;
}
public ChannelsListTabsFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tabbet, container, false);
System.out.println("onCreateView");
applicationContext = getActivity().getApplicationContext();
tabTitles = (ChannelCountriesModel[]) getArguments().getParcelableArray("tabTitles");
session = getArguments().getString("session");
time_offset = getArguments().getLong("time_offset");
return inflater.inflate(R.layout.channels_list_tabs_fragment, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
listChannelsPagerAdapter = new ListChannelsPagerAdapter(session, tabTitles);
mViewPager.setAdapter(listChannelsPagerAdapter);
mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.myred);
}
@Override
public int getDividerColor(int position) {
return getResources().getColor(R.color.myred);
}
});
mSlidingTabLayout.setViewPager(mViewPager);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
if (v.getId()==R.id.channelList) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
Object o = listViewFragment.getItemAtPosition(info.position);
Channel_Model channelModel = (Channel_Model) o;
menu.setHeaderTitle(channelModel.getCh_Name());
String[] menuItems = getResources().getStringArray(R.array.menu);
for (int i = 0; i<menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if (getUserVisibleHint()) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int menuItemIndex = item.getItemId();
Object o = listViewFragment.getItemAtPosition(info.position);
Channel_Model channelModel = (Channel_Model) o;
switch (menuItemIndex) {
// Přehrát program
case 0:
Intent Play_Channel=new Intent(applicationContext,Play_Channel.class);
Play_Channel.putExtra("channelModel", channelModel);
Play_Channel.putExtra("epgModel", channelModel.getEpg_Model_Now());
Play_Channel.putExtra("session", session);
Play_Channel.putExtra("from_beginning", false);
Play_Channel.putExtra("time_offset", time_offset);
//Play_Channel.putExtra("channelListFragment", channelListFragment);
Play_Channel.putExtra("position", info.position);
startActivityForResult(Play_Channel, 110);
break;
// Přehrát program od začátku
case 1:
Intent Play_Channel_From_Beginning=new Intent(applicationContext,Play_Channel.class);
Play_Channel_From_Beginning.putExtra("channelModel", channelModel);
Play_Channel_From_Beginning.putExtra("epgModel", channelModel.getEpg_Model_Now());
Play_Channel_From_Beginning.putExtra("session", session);
Play_Channel_From_Beginning.putExtra("from_beginning", true);
Play_Channel_From_Beginning.putExtra("time_offset", time_offset);
//Play_Channel.putExtra("channelListFragment", channelListFragment);
Play_Channel_From_Beginning.putExtra("position", info.position);
startActivityForResult(Play_Channel_From_Beginning, 110);
break;
// Archiv
case 2:
Intent ListArchive = new Intent(applicationContext, ListArchive.class);
ListArchive.putExtra("channelModel", channelModel);
ListArchive.putExtra("session", session);
startActivityForResult(ListArchive, 120);
break;
// Zpět
case 3 :
break;
}
return true;
}
return false;
}
class ListChannelsPagerAdapter extends PagerAdapter {
private ChannelCountriesModel[] tabTitles;
private String session;
private ListView listViewInAdapter;
public ListChannelsPagerAdapter(String session, ChannelCountriesModel[] tabTitles) {
this.tabTitles = tabTitles;
this.session = session;
}
@Override
public int getCount() {
try {
return tabTitles.length;
}
catch (Exception e) {
//e.printStackTrace();
return 0;
}
}
@Override
public boolean isViewFromObject(View view, Object o) {
return o == view;
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position].getName();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View rootView = getActivity().getLayoutInflater().inflate(R.layout.channels_list_tabs_pager_item, container, false);
container.addView(rootView);
listViewInAdapter = (ListView) rootView.findViewById(R.id.channelList);
listViewInAdapter.requestFocus();
Channel_Row_Adapter channel_Row_Adapter = new Channel_Row_Adapter(listViewInAdapter.getContext(), ((ChannelsListTabs)getActivity()).channelsCountriesMap.get(tabTitles[position].getShortcut()), session);
listViewInAdapter.setAdapter(channel_Row_Adapter);
registerForContextMenu(listViewInAdapter);
listViewInAdapter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
try {
listViewFragment = listViewInAdapter;
getActivity().openContextMenu(v);
} catch (Exception e) {}
}
});
return rootView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}
一切看起来都不错,但直到我想使用上下文菜单 - 它是最后创建的列表视图,而不是实际页面。
我该怎么做?
答案 0 :(得分:0)
我在PagerAdapter中使用ListView并遇到了与您相同的问题。
当我尝试使用ArrayList将数据传递给ListView Adapter时,我终于发现我使用ArrayList作为全局变量。因此,当程序进入instantiateItem()时,它将使用相同的ArrayList。
也许它可以帮助你搞清楚。