英文:
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<?>[] 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.
答案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<?> parent, View view, int position, long id) {
Log.d("LOG", "test");
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论