【问题标题】:Custom List Array Adapter is not clickable自定义列表阵列适配器不可点击
【发布时间】:2013-08-30 12:53:08
【问题描述】:

当我为列表视图使用自定义列表数组适配器时,数据显示在列表中但不可点击。我已经在列表视图项单击上设置了一个侦听器,但它不起作用。

另外,我找到了另一种解决方案,可以在线性布局列表视图项布局上使行可点击并设置背景可绘制,但使用此解决方案是一个坏技巧。

还有其他解决办法吗?

谁能告诉我为什么会这样?在列表视图中,哪些默认功能不适用于自定义数组适配器

这是我的代码:

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;
    }
}

点击项目时没有任何反应。

【问题讨论】:

  • 发布您的自定义适配器代码和布局 xml。

标签: java android listview user-interface android-arrayadapter


【解决方案1】:

您首先要注意的是,只要您的 ListView 元素中出现 Button 或 ImageButton 等可点击元素,它们就会控制点击事件。所以你的 ListView 将没有机会接受点击事件。

您只需将 ListView 中的 Button 或 ImageButton 的 focusable 属性设置为 false。但它们仍然可以正常工作,而且您的 ListView 的 onListItemClick 也可以正常工作。

试试这个,

    <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 + "/>

我在这里添加了这个 android:focusable="false" 并且它工作正常。试试看。

这里是参考链接 Click is not working on the Listitem Listview android

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 那么您针对该问题提出的解决方案是什么
    • 查看行的布局。确保那里没有可以窃取点击的可点击元素。修复布局。或者更改您的适配器并将 onClickListener 分配给行视图或某些视图的元素
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    相关资源
    最近更新 更多