ListView OnItemClickListener没有收听

时间:2015-03-08 18:16:39

标签: android android-listview

我检查了之前关于这个问题的所有问题,但没有一个对我有帮助。

我的列表视图没有响应,我试着改变它 list.setOnItemClickListener(new ContactsListItemClickListener(this));

list.setOnItemClickListener(本);

通过使我的PrioritiseContacts活动只是imeplement OnItemClickListener,但是它也无法正常工作。

活动已成功运行,但我无法收听listclick事件。

如何纠正?

这是我的班级:

public class PrioritiseContacts extends Activity implements OnClickListener {

    private ListView list;
    // list of contacts with name
    private List<Contacts> contactsList;
    private Controller controll;
    private ContactListAdapters adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_contacts);

        controll = new Controller();
        contactsList = controll.fetchContacts(this);

        // call the adapter to set the list view layout
        adapter = new ContactListAdapters(contactsList, this);

        list = (ListView) findViewById(R.id.lv_contacts);
        // set the adapter to list
        list.setAdapter(adapter);
        list.setOnItemClickListener(new ContactsListItemClickListener(this));

        // inflate the list of contact
    }

    @Override
    public void onClick(View arg0) {
        Toast.makeText(this, "clicked", 1000).show();

    }

    class ContactsListItemClickListener implements OnItemClickListener {
        private Context c;

        public ContactsListItemClickListener(
                PrioritiseContacts prioritiseContacts) {
            this.c = prioritiseContacts;

        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            Toast.makeText(c, "Clicked", 1500).show();
            System.out.print("clicked");
        }
    }
}

我的select_contacts xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000" 
    android:orientation="vertical">


    <TextView
        android:id="@+id/tv_select_contacts"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:text="Choose Contacts"
        android:textColor="#fdfdfd"
        android:textSize="30dip"
        android:gravity="center" >
    </TextView>


    <ListView
        android:id="@+id/lv_contacts"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:cacheColorHint="#00000000"
        android:clickable="true"
        android:focusable="true"
        android:divider="@android:color/transparent"
        android:dividerHeight="10.0sp"
        android:scrollbars="none" >
    </ListView>

</LinearLayout>

这是我的适配器的getview():

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;

        // layout infklater to inflate the post list view
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            view = inflater.inflate(R.layout.contacts_list_view, null);
        }

        Contacts c = contactList.get(position);

        // set text views in contact lists
        // Typeface custom_font =
        // Typeface.createFromAsset(context.getAssets(),"fonts/calibril.ttf");
        TextView name = (TextView) view.findViewById(R.id.tv_contact_name);
        // date.setTypeface(custom_font);
        name.setText(c.getName());

        TextView number = (TextView) view.findViewById(R.id.tv_number);
        // title.setTypeface(custom_font);
        number.setText(c.getPhone());

        ImageView contact_image = (ImageView) view.findViewById(R.id.iv_single_contact);
        // hut.setTypeface(custom_font);
        if(c.getContactImage() != null)
        contact_image.setImageBitmap(c.getContactImage());
        else
            contact_image.setImageDrawable(view.getResources().getDrawable(R.drawable.ic_contact_picture_2));


        return view;
    }

我的contacts_list_view xml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/rl_post_list"
        android:layout_width="match_parent"
        android:layout_height="50dip"
        android:background="#000000"
        android:gravity="left"
        android:orientation="horizontal"
        android:paddingBottom="2dp"
        android:paddingTop="2dp" >
        <ImageView
            android:id="@+id/iv_single_contact"
            android:layout_width="70dp"
            android:layout_height="fill_parent"
            android:layout_weight="0.05"
            android:padding="2dp" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.87"
            android:orientation="vertical" >
            <TextView
                android:id="@+id/tv_contact_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#000000"
                android:gravity="left"
                android:paddingLeft="2dp"
                android:text="Contact Name"
                android:textColor="#fdfbfb"
                android:textStyle="bold" />
            <View style="@style/Divider" />
            <TextView
                android:id="@+id/tv_number"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-thin"
                android:gravity="left"
                android:text="this is number"
                android:textColor="#fdfbfb"
                android:textSize="10dp"
                android:textStyle="bold" />


        </LinearLayout>
        <CheckBox
            android:id="@+id/cb_contact"
            android:layout_width="70dp"
            android:layout_height="fill_parent"
            android:layout_weight="0.05"
            android:padding="2dp" />


    </LinearLayout>

1 个答案:

答案 0 :(得分:2)

如果列表中的任何行项目包含focusableclickable视图,则OnItemClickListener无法在行项目中使用复选框或按钮等工作。有两种解决方案:

<强> 1 即可。行项目必须具有android:descendantFocusability="blocksDescendants"

之类的参数
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:descendantFocusability="blocksDescendants"
    android:gravity="center_vertical" >

    // your other widgets here

</LinearLayout>

2。将两个属性设置为false 像

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

例如,如果行项目中有任何复选框或按钮或图像,则

<CheckBox
    android:id="@+id/fav_check_box"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false" />