0

请帮助找出为什么视图绑定onItemClickListener不起作用。

简而言之:我正在加载片段列表。并且需要通过点击item来选择要加载的fragment,所以我实现AdapterView.OnItemClickListener了这个目的。但是当我执行点击时 - 没有任何反应,方法不会被调用。

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 布局:

<?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>

谢谢。

4

1 回答 1

1

是否有某些原因必须让 Fragment 扩展 ListFragment 并实现 AdapterView.OnItemClickListener?以正常方式尝试,看看会发生什么:

mBinding.list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d("LOG", "test");
        }
    });
于 2020-09-01T11:34:27.913 回答