Le setOnItemClickListener d'Android ListView de PopupWindow n'est pas appelé
J'essaie de montrer une liste à partir d'un PopupWindow. mais quand je tente d'appeler du ListView setOnItemClickListener rien à haapen. Ici, Il fichier Java
PopupWindowActivity.java
public class PopupWindowActivity extends Activity {
String[] data = { "DATA 1", "DATA 2", "DATA 3", "DATA 4", "DATA 5", "DATA 6" };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
final Button btnOpenPopup = (Button) findViewById(R.id.openpopup);
btnOpenPopup.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.main, null);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ListView listView = (ListView) popupView.findViewById(R.id.listView1);
listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,data));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//TODO Auto-generated method stub
System.out.println("Item Clicked");
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(btnOpenPopup, 20, -5);
}
});
}
}
Ici, c'est d'abord fichier xml
a.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/openpopup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open Popup Window" />
</LinearLayout>
Ici gonfler fichier xml
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/recording"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="30sp"
android:layout_marginLeft="30sp"
android:layout_marginRight="30sp"
android:layout_marginTop="100sp" >
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000" >
</ListView>
</RelativeLayout>
</LinearLayout>
Ce que je fais mal?
Grâce
source d'informationauteur Mansi
Vous devez vous connecter pour publier un commentaire.
Juste un petit changement dans votre code et BOUM votre code sera à l'écoute de vous liste, cliquez sur l'événement
Vous oubliez de mentionner focusable réglage vrai dans PopupWindow constructeur.
eu le même problème, mais dans mon cas,
setFocusble(false)
était nécessaire (et à l'aide deListPopupWindow
n'était pas une solution dans mon cas, comme beaucoup de choses dans le projet déjà utilisé la base dePopupWindow
's la fonctionnalité, y compris l'extension).Si quelqu'un dans la même situation il y a une sorte de solution basée sur bug discusson ici (post #9)
L'idée principale est que
ListView
'hiérarchie est toujours reçoit les événements tactiles afin que nous puissions déclencher manuellementonItemClick()
.Toutefois, cette approche n'est pas 100% identique à la vraie
ListView
's touch manipulation (comme il n'y a pas de lueur de sélection, tout en exploitant une ligne) de ce fait assez bien pour moi pour le moment.Si quelqu'un a plus de précision de la solution de ce problème, s'il vous plaît partager.
Donc, ici, est complet
Adapter
le code qui peut être utilisé avecListView
à l'intérieur dePopupWindow
qui estsetFocusable(false)
:privé de classe CustomAdapter s'étend ArrayAdapter {
}
Cela peut vous aider
.
Déclarer listview et liste onClickListener à l'extérieur du bouton ClickListener.