Android列表视图项目单击无效

时间:2014-05-12 07:04:38

标签: android listview onclicklistener

我已经看过类似的问题,并尝试过将焦点设置为false但仍然无效的解决方案。这是我的布局包含listview:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >


<ListView 
    android:id="@+id/listView_open_groups"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/button_add_group"
    android:layout_alignParentTop="true"
    ></ListView>

<Button 
    android:id="@+id/button_add_group"
    android:text="Add Group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:focusable="false"
    />

<Button 
    android:id="@+id/button_add_entry"
    android:text="Add Entry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/button_add_group"
    android:focusable="false"
    />


</RelativeLayout>

我的custom_list_row

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:longClickable="true" >


<ImageView 
    android:id="@+id/imgView_group_entry_icon"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:focusable="false"
    />
<TextView 
    android:id="@+id/textView_group_entry_name"
    android:layout_toRightOf="@id/imgView_group_entry_icon"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false"/>
<TextView 
    android:id="@+id/textView_group_entry_type"
    android:layout_alignParentRight="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"/>


</RelativeLayout>

我的自定义适配器:

public class OpenDbListAdapter extends ArrayAdapter<KeepassDBGroupv1>{

Context context;

public OpenDbListAdapter(Context context, int resource,
        List<KeepassDBGroupv1> objects) {
    super(context, resource, objects);
    this.context=context;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    KeepassDBGroupv1 rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.custom_list_row, null);
        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.textView_group_entry_name);
        holder.imageView = (ImageView) convertView.findViewById(R.id.imgView_group_entry_icon);
        holder.type = (TextView) convertView.findViewById(R.id.textView_group_entry_type);
        convertView.setTag(holder);
    } else 
        holder = (ViewHolder) convertView.getTag();

    holder.name.setText(rowItem.getGroupName());
    holder.type.setText("Group");
    Drawable d = context.getResources().getDrawable(context.getResources().getIdentifier("ic"+Integer.toString(rowItem.getImageId()), "drawable", context.getPackageName()));
    holder.imageView.setImageDrawable(d);
    //App.getDB().drawFactory.assignDrawableTo(holder.imageView, context.getResources(), rowItem.icon);

    return convertView;
}

private class ViewHolder {
    ImageView imageView;
    TextView name;
    TextView type;
}

}

最后,这是我如何设置监听器:

public class OpenDbActivity extends Activity{    
    static ListView lv;  

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_opendb);
    db = MyApplication.keepassdb;
    InitializeComponents();

}

    private void InitializeComponents() {

    lv = (ListView) findViewById(R.id.listView_open_groups);
    OpenDbListAdapter adapter = new OpenDbListAdapter(this, R.layout.custom_list_row, childGroups);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Log.d("Something");

        }
    });



}

任何人都可以看到问题所在吗?

由于

3 个答案:

答案 0 :(得分:9)

我也遇到过这个问题,我通过将点击监听器设置为convertView的{​​{1}}来克服这个问题。我不知道这是一个好方法,但它解决了我的问题。

custom adapter

答案 1 :(得分:4)

  

实际上没有错。

但问题来自custom_list_row.xml,当您点击列表视图项

  

您实际点击了custom_list_row.xml

中的其他代码

并且这些标签不负责触发ItemClickListener

android:layout_width中标记的属性custom_list_row.xml更改为"wrap_content"或删除标记,然后重试。

答案 2 :(得分:1)

设置

  

机器人:可聚焦=&#34;假&#34;

for ListView内容,在ListView标签添加

  

机器人:可点击=&#34;真&#34;