setOnItemClickListener不适用于ListView

时间:2014-05-23 16:37:52

标签: android listview

这是我的代码:

的ListView:

ListView lv;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        final View rootView = inflater.inflate(R.layout.aller_layout, container, false);
        lv = (ListView) rootView.findViewById(R.id.listView1);
        CustomAdapter ca = new CustomAdapter(rootView.getContext(), R.layout.preflist_checkbox_layout, oggetti);
        lv.setAdapter(ca);
        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parentAdapter, View view, int position, long id){

                Log.i("onclick", "si");

                Toast.makeText(view.getContext(), "You clicked", Toast.LENGTH_SHORT).show();
            }   
        });

        return rootView;
    }

和适配器:

public class CustomAdapter extends ArrayAdapter<Oggetto>{

    private Context context;
    private Oggetto[] oggetti = null;
    private int layoutid;

    public CustomAdapter(Context context, int layoutid, Oggetto[] oggetti){
        super(context, layoutid, oggetti);

        this.context = context;
        this.oggetti = oggetti;
        this.layoutid = layoutid;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            convertView = inflater.inflate(layoutid, parent, false);
        }

        Oggetto oggetto = oggetti[position];
        CheckBox cb = (CheckBox) convertView.findViewById(R.id.pref_checkbox);
        cb.setText(oggetto.nome);
        cb.setChecked(oggetto.preferito == 1);
        return convertView;
    }   
}

现在我只是试图抓住列表视图中的复选框上的点击,但是它没有正常工作的代码,当我点击任何时候都没有显示日志或Toast元件。

哪里弄错了?

编辑:因为在xml中似乎是一个问题,我将添加ListView行的布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:descendantFocusability="blocksDescendants">

    <CheckBox
        android:id="@+id/pref_checkbox"
        android:layout_width="fill_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:gravity="center_vertical"
        android:checkMark="?android:attr/listChoiceIndicatorMultiple"
        android:paddingLeft="6dip"
        android:paddingRight="6dip" 
        android:focusable="false"
        android:focusableInTouchMode="false"/>

</LinearLayout>

5 个答案:

答案 0 :(得分:0)

我猜测CheckBox在您点击ListItem

时会获得焦点

有这个

android:descendantFocusability="blocksDescendants"

表示preflist_checkbox_layout.xml

中的根元素

答案 1 :(得分:0)

问题是Android不允许您选择包含可关注元素的列表项。像这样添加:

android:focusable="false"

答案 2 :(得分:0)

将这些属性添加到CheckBox:

android:focusable="false"
android:focusableInTouchMode="false"

答案 3 :(得分:0)

 lv.setOnItemClickListener(this);
    @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            Toast toast = Toast.makeText(getApplicationContext(),
                    "Item " + (position + 1) + ": ",Toast.LENGTH_SHORT);

        }

试试这个

答案 4 :(得分:0)

好的,我设法找到了问题。

我无法使onItemClick正常工作,我想这是因为它实际上根本不适用于复选框(或者至少我无法找到方法)。

因此我将onClickListener链接到适配器内的复选框,它就像魅力一样。