I am going to create customized ListView. In each row item put a Button so when user click on button it will show position of the item in ListView. In the same way when user click on ListView it will show position of the item in ListView.
Set the listener on ListView-:
Define the listener
Set the OnClickListener on Button which is entail in ListView Row Item layout-:
Define the OnClickListener :
Set the listener on ListView-:
listView.setOnItemClickListener(onItemClickListener);
private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { Toast.makeText(MainActivity.this, "This is click=" + position, Toast.LENGTH_LONG).show(); } };
Set the OnClickListener on Button which is entail in ListView Row Item layout-:
eventButton.setOnClickListener(eventButtonClickListener);
Define the OnClickListener :
private OnClickListener eventButtonClickListener = new OnClickListener() { @Override public void onClick(View v) { View parentRow = (View) v.getParent(); ListView listView = (ListView) parentRow.getParent(); final int position = listView.getPositionForView(parentRow); } };
No comments:
Post a Comment