简体   繁体   English

自定义列表阵列适配器不可单击

[英]Custom List Array Adapter is not clickable

When I used a custom list array adapter for list view, the data are shown on the list but are not clickable. 当我使用自定义列表数组适配器进行列表视图时,数据显示在列表中但不可单击。 I already set a listener on a listview item click, but it's not working. 我已经在listview项目上设置了一个监听器点击,但它不起作用。

Also, I found another solution to make rows clickable and set background drawable on linear layout list view item layout but it's a bad trick to use this solution. 此外,我找到了另一种解决方案,使行可点击并在线性布局列表视图项目布局上设置背景可绘制,但这是使用此解决方案的一个坏技巧。

Is there any other solution for this? 还有其他解决方案吗?

Can any body tell me why does this happen? 任何人都可以告诉我为什么会这样吗? In list view which default functionality not working on custom array adapter 在列表视图中,默认功能不适用于自定义阵列适配器

Here is my code: 这是我的代码:

public class Model {
    String name;
    String id;
    String price;
}

mArrayList = new ArrayList<Model>();
// mArrayList has some data in it 

mListiView.setsetAdapter(new myCoustomAdapter(mContext, mArrayList));
mListiView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getBaseContext(), ""+position, Toast.LENGTH_LONG).show();
    }
});

public class myCoustomAdapter extends ArrayAdapter<Model> {
    ArrayList<Model> mList;

    public myCoustomAdapter(Context context, ArrayList<Model> list) {
        super(context, R.layout.item);
        this.mList=list;
    }

    @Override
    public int getCount() {
        return this.mList.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // layout data here 
        return view;
    }
}

Nothing happens on item click. 单击项目时没有任何操作。

The first thing what you have to note here is, whenever there are Clickable elements like Buttons or ImageButtons present in your ListView element, they take the control of click events. 首先要注意的是,只要ListView元素中存在Buttons或ImageButtons等可点击元素,它们就会控制点击事件。 And so your ListView won't get the chance to accept the click event. 因此,您的ListView将无法接受click事件。

What you simply have to do is, set the focusable attribute to false for the Button or ImageButton you have in your ListView. 您只需要为ListView中的Button或ImageButton设置focusable属性为false。 But still they will work without any problem and also your ListView's onListItemClick will also work. 但是它们仍然可以正常工作,而且ListView的onListItemClick也可以工作。

Try this, 尝试这个,

    <Button  android:id="@+id/textsize_increaser"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/back_button"
    android:focusable="false"
    android:text=" A + "/>

Here I have added this android:focusable="false" and it works fine. 在这里我添加了这个android:focusable =“false”,它工作正常。 try it. 试试吧。

Here is link for reference Click is not working on the Listitem Listview android 这里是链接供参考点击不在Listitem Listview android上

很可能您的列表视图行的布局包含可点击的元素,这会“窃取”您的点击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM