Domanda

I developd a viewpager with custom list views in android. I followed the tutorial View Pager Part 1-3. The major difference in my development is that i used a customlistview in the views of the paager. The list items in the listview also support click events to display more info about an item.

Here is the problem; I handled the onItemClick inside the instantiateItem method but i noticed that when an item is clicked, the content displayed is that of the adjacent listview (i.e if item 2 of page 2 is clicked the info displayed is item 2 of page 3).

I realised this happens because the pager loads adjacent lists so as to make rendering faster for the user so my code uses the preloaded list instead of the list in current display.

I thought to find out the appropriate way to handle listview click events in viewpager, code snippets, links,.. will really be of help. The appropriate method to use to handle the click events.

Thank you

È stato utile?

Soluzione

You have to get it from the adapter of the listview (the first parameter of the onItemClick method):

@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
     // NOT THIS!: mValue = (HereYourClass) listView.getItemAtPosition(position);
     mValue = (HereYourClass)a.getAdapter().getItem(position);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top