为什么我的ListView永远不会调用onItemClick? [英] why my ListView never calls onItemClick?

查看:124
本文介绍了为什么我的ListView永远不会调用onItemClick?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的搜查谷歌,栈溢出了很多,发现这一点:<一href=\"http://stackoverflow.com/questions/5551042/onitemclicklistener-not-working-in-listview-android\">OnItemCLickListener在列表视图ANDROID 不工作。但是,似乎阳光的回答并不适用于我的情况。其他的答案都是类似的。

我曾尝试以下方法:

添加机器人:可聚焦=FALSE我的列表项XML

添加 TextView.setFocusable(假) TextView.setClickable(假)在ViewHolder

使用如在上述链接中描述的XML

但他们没有工作。

下面是我的XML和Java code:

list_item.xml:

 &LT;?XML版本=1.0编码=UTF-8&GT?;&LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:baselineAligned =假
机器人:descendantFocusability =blocksDescendants
机器人:可聚焦=假
机器人:paddingTop =2DP
机器人:重力=center_vertical&GT;&LT;的TextView
    机器人:ID =@ + ID / ninegrid_number_list_choice
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:TEXTSIZE =@扪/ ninegrid_number_listchoice_text_size
    机器人:比重=中心
    &GT;
&LT; / TextView的&GT;
&LT; / LinearLayout中&GT;

getView INT列表adapter.java:

  @覆盖
    公共查看getView(INT位置,查看convertView,最终的ViewGroup父){
        ViewHolder持有人;
        如果(convertView == NULL){
            convertView = mInflater.inflate(R.layout.list_item,NULL);
            持有人=新ViewHolder();
            holder.mTextView =(TextView中)convertView.findViewById(R.id.ninegrid_number_list_choice);
            holder.mTextView.setFocusable(假);
            holder.mTextView.setClickable(假);
            convertView.setTag(保持器);
        }其他{
            支架=(ViewHolder)convertView.getTag();
        }        holder.mTextView.setText(mList.get(位置));
        holder.mTextView.setTextColor(mTextColor);
        holder.mTextView.setFocusable(假);
        holder.mTextView.setClickable(假);
        返回convertView;
    }

编辑:
在我的活动:

  listchoice.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
 listChoice.setOnItemClickListener(新OnItemClickListener(){
 @覆盖
 公共无效onItemClick(适配器视图&LT;&GT;为arg0,ARG1观,诠释ARG2,
     长ARG3){      Log.v(标签,自卫队);
 }
 });


解决方案

找到这里的源$ C ​​$ c中的完整的答案的与图像和文本的Andr​​oid ListView的例子

I have really searched a lot on google, stack overflow , and found this : OnItemCLickListener not working in listview ANDROID. But seems sunshine's answer not works for my case. Other answers are all similar ones.

I have tried the following approaches:

add android:focusable="false" to my list item xml

add TextView.setFocusable(false) and TextView.setClickable(false) in ViewHolder

using the xml as described in the above link.

But none of them work.

Here's my xml and java code:

list_item.xml:

<?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="wrap_content"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:paddingTop="2dp"
android:gravity="center_vertical" >

<TextView
    android:id="@+id/ninegrid_number_list_choice"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="@dimen/ninegrid_number_listchoice_text_size"
    android:gravity="center"
    >
</TextView>
</LinearLayout>

getView int list adapter.java:

@Override
    public View getView(int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null ) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.mTextView = (TextView)convertView.findViewById(R.id.ninegrid_number_list_choice);
            holder.mTextView.setFocusable(false);
            holder.mTextView.setClickable(false);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        holder.mTextView.setText(mList.get(position));
        holder.mTextView.setTextColor(mTextColor);
        holder.mTextView.setFocusable(false);
        holder.mTextView.setClickable(false);
        return convertView;
    }

Edit: in my activity:

 listchoice.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
 listChoice.setOnItemClickListener(new OnItemClickListener() {
 @Override
 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
     long arg3) {

      Log.v(tag, "sdf");
 }
 });

解决方案

find the complete answer with source code from here Android ListView example with Image and Text

这篇关于为什么我的ListView永远不会调用onItemClick?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆