Inside list_item.xml, mark your layout with some id:
<?xml version="1.0" encoding="utf-8"?> <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true"> <LinearLayout android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> ...
Open your user adapter, in getView(...) write:
layout = view.findViewById(R.id.layout); layout.setTag(position); layout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (callback != null) { callback.showItem(getItem((int) v.getTag())); } } });
Write below in the adapter:
public interface OnClickInterface { void showItem(YourClass item); }
And in the adapter constructor, assign a callback:
public CustomAdapter(Context context, List<YourClass> list, OnClickInterface callback, int resId) { this.callback = callback; ...
In your activity / fragment that contains the ListView, create this adapter:
CustomAdapter.OnClickInterface callback = new CustomAdapter.OnClickInterface() { @Override public void showItem(YourClass item) {
source share