سؤال

I have a ViewPager that uses ListFragments on each page. I implement onListItemClick and onContextItemSelected to handle list item click and long clock, respectively. The ListFragment data is loaded via AsyncTask from the internet.

The desired behavior is to short press on a list view item to start a detail activity to show detail for that location (it's a weather app). When the user long presses, a context menu pops up to give the user other options, including to view raw data.

The short press and detail activity works fine. the problem is the long press, context menu. When the user is on page 3 and selects the 2nd item in the list using a long press, and then selects "show raw data" for that location, the raw data for the 2nd item on page 2 is shown, not page 3.

Right now I'm using setOffscreenPageLimit(1). when I change that to setOffscreenPageLimit(2), the data from page 1 is displayed instead of page 3.

Code:

@Override
public boolean onContextItemSelected(android.view.MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
    case R.id.cmenu_showRaw:    

        ViewGroup dataPanel = (ViewGroup) myListView.getChildAt(info.position);         
        TextView tvRaw = (TextView) dataPanel.findViewById(R.id.textRaw);
        TextView tvLocationID = (TextView) dataPanel.findViewById(R.id.textLocationId);

        showDismissDialog(tvRaw.getText().toString(), tvLocationID .getText().toString()); 
        return true;

I'm trying to figure out why the raw and locationID data are not pulled from the fragment that is currently shown on the ViewPager, but from the offset page. I'm still learning Fragments, so I'm probably missing something subtle.

هل كانت مفيدة؟

المحلول

I figured it out: info.targetView gets me the view that I longpressed on. From there I can do a findViewById to get what I need.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top