gpt4 book ai didi

android - onClick 事件不适用于 android 中的嵌套 ListView 项控件

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:23:00 25 4
gpt4 key购买 nike

我有 2 个弹出窗口。在第一个弹出窗口中,我有带有可选项目的 ListView。当我单击第一个列表中的项目时,会出现第二个弹出窗口,其中还有 ListView 以及可供选择的选项。

我已经将第一个 ListView 项实现为自定义 View 并在 View 构造函数中订阅了点击,就像这样:

class CustomListItem extends RelativeLayout{
public CustomListItem(){
...
//inflating stuff there
((Button)findViewById(R.id.listItemButton)).setOnClickListener(
//This code not working as expected, but then could fire a lot of times
v -> System.out.println("item clicked");
);
}
}

当我第一次打开第一个对话框时,onClick 处理程序按预期触发并出现第二个弹出窗口。但是当我关闭第二个弹出窗口并返回(为弹出窗口调用 Dialog.dismiss())到第一个弹出窗口时,第一个 ListView 上的 onClick 处理程序停止工作。还有一些其他有趣的事情:

  1. onTouch ListView 项的监听器仍在工作(调用 action=ACTION_DOWNaction=ACTION_UP);
  2. 当我为 listview onItemClickListener 设置时,它总是被调用(第一次打开弹出窗口以及我们返回时);
  3. 有时当我多次点击 ListView 中的项目时 onClick发生了,之后它被调用了我点击的次数之前。

您是否知道问题的原因可能是什么?

更新

似乎适配器内部的 getView 方法有问题。我试图在其中创建一个自定义列表项控件(或从缓存中获取):

@Override
public View getView(int position, View view, ViewGroup parent) {
if (!constructedViewCache.containsKey(position)) {
constructedViewCache.put(position, new CustomListItem ());
}

return constructedViewCache.get(position);
}

当我将此代码更改为下面的代码时,一切正常:

@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null)
{
view = LayoutInflater.from(context).inflate(R.layout.list_view_item, null);
}

view.setOnClickListener(v -> {
v -> System.out.println("item clicked");
});

return view;
}

为什么不能使用 new CustomListItem () 创建 View ?或者,如果可能的话,我应该怎么做?

最佳答案

尝试在放置 listItemButton 的列表项的父布局声明中插入属性 android:descendantFocusability="blocksDescendants"

Where属性android:descendantFocusability

Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.

常量 blocksDescendants 意味着:

The ViewGroup will block its descendants from receiving focus.

第二个问题

可以使用新的 CustomListItem () 创建 View 。请参见下面的示例:

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
return new CustomListItem(context);
}

编辑

我没有看到你的完整代码,看来你有问题。这是工作示例看看:

public class CustomAdapter extends BaseAdapter {
private Context context;
private HashMap<Integer, CustomListItem> constructedViewCache = new HashMap<>();

public CustomAdapter(Context context) {
this.context = context;
}

@Override
public int getCount() {
return 20;
}

@Override
public Object getItem(int i) {
return null;
}

@Override
public long getItemId(int i) {
return 0;
}

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
if (!constructedViewCache.containsKey(position)) {
constructedViewCache.put(position, new CustomListItem(context, position));
}
return constructedViewCache.get(position);
}
}

第二类:

public class CustomListItem extends RelativeLayout {
private static final String TAG = "CustomListItem";

public CustomListItem(Context context, int position) {
super(context);

View view = LayoutInflater.from(context).inflate(R.layout.item_view, this);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(view1 -> {
Log.e(TAG, "CustomListItem: " + position );
FragmentManager fragmentManager = ((AppCompatActivity) context).getSupportFragmentManager();
new CustomDialog().show(fragmentManager, "tag");
});

}
}

编辑 2

我更新了 CustomListItem 以在单击按钮时显示对话框。有自定义 DialogFragment 的代码,它使用与 Activity 相同的布局和适配器的项目列表:

public class CustomDialog extends DialogFragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, container, false);
ListView listView = (ListView) view.findViewById(R.id.list);
listView.setAdapter(new CustomAdapter(getActivity()));
return view;
}
}

关于android - onClick 事件不适用于 android 中的嵌套 ListView 项控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39149604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com