ListView控件OnItemClickListener就是不

ListView控件OnItemClickListener就是不

本文介绍了ListView控件OnItemClickListener就是不听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检查了所有关于这个问题的previous问题,但他们都不是有益的给我。

我的列表视图没有响应,我试图改变这 list.setOnItemClickListener(新ContactsListItemClickListener(本));

list.setOnItemClickListener(本);

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

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

如何纠正呢?

下面是我的类:

 公共类PrioritiseContacts扩展活动实现OnClickListener {

    私人的ListView列表;
    //名称为联系人列表
    私人列表<联系和GT; contactsList;
    私人控制器控制研究;
    私人ContactListAdapters适配器;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.select_contacts);

        控制研究=新的控制器();
        contactsList = controll.fetchContacts(本);

        //调用适配器设置列表视图布局
        适配器=新ContactListAdapters(contactsList,这一点);

        名单=(ListView控件)findViewById(R.id.lv_contacts);
        //设置适配器列表
        list.setAdapter(适配器);
        list.setOnItemClickListener(新ContactsListItemClickListener(本));

        //膨胀接触的列表中
    }

    @覆盖
    公共无效的onClick(查看为arg0){
        Toast.makeText(这一点,点击,1000).show();

    }

    类ContactsListItemClickListener实现OnItemClickListener {
        私人语境℃;

        公共ContactsListItemClickListener(
                PrioritiseContacts prioritiseContacts){
            this.c = prioritiseContacts;

        }

        @覆盖
        公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,
                长ID){
            Toast.makeText(C,点击,1500).show();
            System.out.print(点击);
        }
    }
}
 

我的select_contacts XML:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:后台=#000000
    机器人:方向=垂直>


    <的TextView
        机器人:ID =@ + ID / tv_select_contacts
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_margin =5DP
        机器人:文本=选择联系人
        机器人:文字颜色=#fdfdfd
        机器人:TEXTSIZE =30dip
        机器人:重力=中心>
    < / TextView的>


    <的ListView
        机器人:ID =@ + ID / lv_contacts
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_margin =5DP
        机器人:cacheColorHint =#00000000
        机器人:可点击=真
        机器人:可聚焦=真
        机器人:分隔=@机器人:彩色/透明
        机器人:dividerHeight =10.0sp
        机器人:滚动条=无>
    < / ListView控件>

< / LinearLayout中>
 

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

  @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        查看查看= convertView;

        //布局infklater膨胀后列表视图
        LayoutInflater充气=(LayoutInflater)上下文
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        如果(convertView == NULL){
            鉴于= inflater.inflate(R.layout.contacts_list_view,NULL);
        }

        联系人C = contactList.get(位置);

        //设置文本视图的联系人列表
        //字样custom_font =
        // Typeface.createFromAsset(context.getAssets(),字体/ calibril.ttf);
        TextView的名称=(TextView中)view.findViewById(R.id.tv_contact_name);
        // date.setTypeface(custom_font);
        name.setText(c.getName());

        TextView的数量=(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);
        如果(c.getContactImage()!= NULL)
        contact_image.setImageBitmap(c.getContactImage());
        其他
            contact_image.setImageDrawable(view.getResources()getDrawable(R.drawable.ic_contact_picture_2));


        返回查看;
    }
 

我的contacts_list_view XML:

 < XML版本=1.0编码=UTF-8&GT?;
    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:ID =@ + ID / rl_post_list
        机器人:layout_width =match_parent
        机器人:layout_height =50dip
        机器人:后台=#000000
        机器人:重力=左
        机器人:方向=横向
        机器人:paddingBottom会=2DP
        机器人:paddingTop =2DP>
        < ImageView的
            机器人:ID =@ + ID / iv_single_contact
            机器人:layout_width =70dp
            机器人:layout_height =FILL_PARENT
            机器人:layout_weight =0.05
            机器人:填充=2DP/>
        <的LinearLayout
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =0.87
            机器人:方向=垂直>
            <的TextView
                机器人:ID =@ + ID / tv_contact_name
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
                机器人:后台=#000000
                机器人:重力=左
                机器人:以下属性来=2DP
                机器人:文本=联系人姓名
                机器人:文字颜色=#fdfbfb
                机器人:TEXTSTYLE =黑体/>
            <景观风格=@风格/除法/>
            <的TextView
                机器人:ID =@ + ID / tv_number
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:fontFamily中=无衬线薄
                机器人:重力=左
                机器人:文本=这是数字
                机器人:文字颜色=#fdfbfb
                机器人:TEXTSIZE =10dp
                机器人:TEXTSTYLE =黑体/>


        < / LinearLayout中>
        <复选框
            机器人:ID =@ + ID / cb_contact
            机器人:layout_width =70dp
            机器人:layout_height =FILL_PARENT
            机器人:layout_weight =0.05
            机器人:填充=2DP/>


    < / LinearLayout中>
 

解决方案

如果列表中的任何行项目包含可聚焦点击视图,则 OnItemClickListener 将不会如该行中的复选框或按钮等工作item.There有两种解决办法:

1 。行项目必须具有参数如安卓descendantFocusability =blocksDescendants

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:baselineAligned =假
    机器人:descendantFocusability =blocksDescendants
    机器人:重力=center_vertical>

    //您在这里的其他部件

< / LinearLayout中>
 

2 设置给两个属性设置为false像

 机器人:可调焦=假
   机器人:focusableInTouchMode =假
 

例如,如果在该行项目的任何复选框或按钮或图像,然后

 <复选框
    机器人:ID =@ + ID / fav_check_box
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:可聚焦=FALSE/>
 

I checked all the previous questions regarding this issue , but none of them are helpfull to me .

My listview is not responding , i tried changing thislist.setOnItemClickListener(new ContactsListItemClickListener(this));

to

list.setOnItemClickListener(this);

by making my PrioritiseContacts activity just imeplement OnItemClickListener , but then too its not working .

The activity is successfully running , but i am unable to listen for listclick events.

How to correct this?

Here is my class :

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

My 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>

And this is my adapter's 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;
    }

My 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>
解决方案

If any row item of list contains focusable or clickable view then OnItemClickListener won't work such as for checkbox or button etc in the row item.There are two solution:

1. row item must be having param like 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. Set given two attributes to falselike

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

For example if there is any checkbox or button or image in the row item then

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

这篇关于ListView控件OnItemClickListener就是不听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 05:14