【问题标题】:ListView onItemClickListener isn't called未调用 ListView onItemClickListener
【发布时间】:2013-01-25 14:48:56
【问题描述】:

我正在尝试自定义ListView。 ListView 包含一个RelativeLayout,其中包含一个TextView 和一个Switch。当您按下开关时,开关必须从真变为假(反之亦然)。

这是我的 getView 方法:

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.item_gproblem, null);

    //vi.setClickable(true); Tried this
    //vi.setFocusable(true);

    TextView txt_comment_description = (TextView) vi
            .findViewById(R.id.txt_comment_description);
    txt_comment_description.setText(MyTasks.allGComments.get(position)
            .getProblemDescription());
    //txt_comment_description.setFocusable(false); Tried this
    //txt_comment_description.setClickable(false);

    Switch switch_comment = (Switch) vi.findViewById(R.id.switch_comment);
    //switch_comment.setFocusable(false); Tried this
    //switch_comment.setClickable(false);

    //First time running getMyGComments returns a empty ArrayList
    if (MyTasks.allCustomers.get(ServerData.myID - 1).getMyGComments()
            .size() > position) {
        switch_comment.setChecked(MyTasks.allCustomers
                .get(ServerData.myID - 1).getMyGComments().get(position)
                .isProblemValue());
    }
    return vi;
}

这是我的 onClickListener:

list_quality.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

               //Do Something
            }
        });

当我单击 TextView、Switch 或两个对象之间的空间时,不会调用我的 onItemClickListener。当我按下开关时,开关正常工作(状态改变)。但是我的onItemClickListener 没有被调用。我试图禁用 Switch 和 TextView 的 clickablefocusable 但这也不起作用。

setOnItemClickListeren 被执行。

【问题讨论】:

    标签: android listview onitemclicklistener


    【解决方案1】:

    将下面的行添加到列表视图行的容器中:

    android:descendantFocusability="blocksDescendants"
    

    从您放置它们的任何位置删除所有可点击/可聚焦。如果您按下整个项目,则应该调用 onItemClick。

    此外,如果您希望 listview 行中的按钮也可点击,请在 ListView 适配器 getView() 方法中的按钮上添加一个 onClickListener。

    【讨论】:

      【解决方案2】:

      某些 UI 元素可能会获得焦点而不是列表项,因此请使用它来获取焦点

      android:focusable="false"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-17
        相关资源
        最近更新 更多