Search code examples
javaandroidonclicklistenerandroid-viewbinding

Android ViewBinding onItemClickListener doesn't called


Please, help to figure out why view binding onItemClickListener doesn't work.

To be short: I'm loading list of fragments. And need to choose fragment to load by clicking to item, so I implemented AdapterView.OnItemClickListener for this purpose. But when I perform a click - nothing happens, method does't called.

public class ChooserFragment extends ListFragment implements AdapterView.OnItemClickListener {

    private ChooserFragmentBinding mBinding;

    private static final Class<?>[] CLASSES = new Class[]{
        EmailPasswordFragment.class,
        EmailPasswordFragment.class,
    };

    private static final int[] DESCRIPTION_IDS = new int[]{
        R.string.desc_emailpassword,
        R.string.desc_emailpassword,
    };

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = ChooserFragmentBinding.inflate(inflater, container, false);
        View view = mBinding.getRoot();

        CustomArrayAdapter adapter = new CustomArrayAdapter(getContext(), android.R.layout.simple_list_item_2, CLASSES);
        adapter.setDescriptionIds(DESCRIPTION_IDS);

        mBinding.list.setAdapter(adapter);
        mBinding.list.setOnItemClickListener(this);

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mBinding = null;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Class<?> clicked = CLASSES[position];
        Log.d("Clicked", String.valueOf(position));

    }


    public static class CustomArrayAdapter extends ArrayAdapter<Class<?>> {

        private Context mContext;
        private int[] mDescriptionIds;

        public CustomArrayAdapter(Context context, int resource, Class[] objects) {
            super(context, resource, objects);
            mContext = context;
        }

        @SuppressWarnings("NullableProblems")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;

            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(android.R.layout.simple_list_item_1, null);
            }

            ((TextView) view.findViewById(android.R.id.text1)).setText(mDescriptionIds[position]);

            return view;
        }

        public void setDescriptionIds(int[] descriptionIds) {
            mDescriptionIds = descriptionIds;
        }
    }
}

XML layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</FrameLayout>

Thanks.


Solution

  • Is there some reason you have to have the Fragment extend ListFragment and implement AdapterView.OnItemClickListener? Try it the normal way and see what happens:

    mBinding.list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d("LOG", "test");
            }
        });