Android ViewBinding的onItemClickListener没有被调用。

huangapple go评论76阅读模式
英文:

Android ViewBinding onItemClickListener doesn't called

问题

请帮助解决为何视图绑定中的 onItemClickListener 无法正常工作的问题。

简要说明:我正在加载片段列表。需要通过点击项目选择要加载的片段,因此我为此实现了 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(Context.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>

谢谢。

英文:

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&lt;?&gt;[] 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&lt;?&gt; parent, View view, int position, long id) {
Class&lt;?&gt; clicked = CLASSES[position];
Log.d(&quot;Clicked&quot;, String.valueOf(position));
}
public static class CustomArrayAdapter extends ArrayAdapter&lt;Class&lt;?&gt;&gt; {
private Context mContext;
private int[] mDescriptionIds;
public CustomArrayAdapter(Context context, int resource, Class[] objects) {
super(context, resource, objects);
mContext = context;
}
@SuppressWarnings(&quot;NullableProblems&quot;)
@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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;FrameLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;
android:paddingBottom=&quot;@dimen/activity_vertical_margin&quot;
android:paddingLeft=&quot;@dimen/activity_horizontal_margin&quot;
android:paddingRight=&quot;@dimen/activity_horizontal_margin&quot;
android:paddingTop=&quot;@dimen/activity_vertical_margin&quot;&gt;
&lt;ListView
android:id=&quot;@android:id/list&quot;
android:layout_width=&quot;fill_parent&quot;
android:layout_height=&quot;fill_parent&quot;/&gt;
&lt;/FrameLayout&gt;

Thanks.

答案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");
    }
});
英文:

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&lt;?&gt; parent, View view, int position, long id) {
Log.d(&quot;LOG&quot;, &quot;test&quot;);
}
});

huangapple
  • 本文由 发表于 2020年9月1日 18:54:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63686257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定