2012-01-21 132 views
27

我有一个使用定制的适配器ListView,但我不能点击ListView项..ListView项目不可点击。为什么?

活动的列表视图..

package com.adhamenaya.projects; 

import java.util.ArrayList; 

import android.app.Activity; 
import android.content.Context; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.Filter; 
import android.widget.Filterable; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.adhamenaya.classes.Place; 

public class PlacesListActivity extends Activity { 
    private ArrayList<Place> places; 
    private ArrayList<String> items; 
    GridviewAdapter mAdapter; 
    private ListView lvPlaces; 
    private EfficientAdapter adap; 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.places_list); 
     lvPlaces = (ListView) this.findViewById(R.id.lvPlaces); 
     new DowanloadPlaces().execute(""); 
    } 
    private void bindList(ArrayList<Place> places) { 
     this.places = places; 
     // Start creating the list view to show articles 
     items = new ArrayList<String>(); 
     for (int i = 0; i < places.size(); i++) { 
      items.add(String.valueOf(places.get(i).mName)); 
     } 
     adap = new EfficientAdapter(this); 
     adap.notifyDataSetChanged(); 
     lvPlaces.setAdapter(adap); 
    } 

    // EfficientAdapter : to make a customized list view item 
    public class EfficientAdapter extends BaseAdapter implements Filterable { 

     // The function of inflater to convert objects from XML layout file (i.e. main.xml) to a programmable 
     LayoutInflater inflater; 
     Context context; 

     public EfficientAdapter(Context context) { 
      inflater = LayoutInflater.from(context); 
      this.context = context; 
     } 

     public int getCount() { 
      // Get the number of items in the list 
      return items.size(); 
     } 

     public Object getItem(int position) { 
      // To return item from a list in the given position 
      return items.get(position); 
     } 

     public long getItemId(int position) { 
      // TODO Auto-generated method stub 
      return 0; 
     } 

     public View getView(final int position, View convertView,ViewGroup parent) { 
      ViewHolder holder; 
      if (convertView == null) { 
       convertView = inflater.inflate(R.layout.adaptor_content, null); 

       holder = new ViewHolder();// Create an object to hold at components in the list view item 
       holder.textLine = (TextView) convertView.findViewById(R.id.textLine); 
       holder.buttonLine = (Button) convertView.findViewById(R.id.buttonLine); 
       holder.buttonLine.setOnClickListener(new OnClickListener() { 
        private int pos = position; 

        public void onClick(View v) { 
         places.remove(pos); 
         bindList(places);// to bind list items 
         Toast.makeText(getApplicationContext(),"Deleted successfuly :)", Toast.LENGTH_LONG).show(); 
        } 
       }); 
       convertView.setTag(holder); 
      } else { 
       holder = (ViewHolder) convertView.getTag(); 
      } 
      // Bind the data efficiently with the holder. 
      holder.textLine.setText(String.valueOf(places.get(position).mName)); 
      return convertView; 
     } 

     public Filter getFilter() { 
      // TODO Auto-generated method stub 
      return null; 
     } 

    } 

    // ViewHolder : class that represents a list view items 
    static class ViewHolder { 
     TextView textLine; 
     Button buttonLine; 
    } 

    // DownloadRSSFeedsTask: works in a separate thread 
    private class DowanloadPlaces extends AsyncTask<String, Void, ArrayList<Place>> { 

     @Override 
     protected ArrayList<Place> doInBackground(String... params) { 
      ArrayList<Place> places = new ArrayList<Place>(); 
      Place p = new Place(); 
      for(int i =0;i<25;i++){ 
       p.mName = "Al Mathaf Hotel"; 
       places.add(p);    
      } 

      return places; 
     } 

     @Override 
     protected void onPostExecute(ArrayList<Place> places) { 
      bindList(places); 


     } 

    } 


} 

places_list.xml布局

adaptor_content.xml布局

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textLine" 
    android:layout_centerVertical="true" 
    android:src="@drawable/settings" /> 

</RelativeLayout> 
+0

你的ListView的OnClickListener在哪里? –

+0

我也会使用一个ContextMenu作为这个任务的列表项(删除列表项),但这取决于你我猜... –

+0

你看到什么行为会导致你相信它不可点击?你确定ListView项目被设置为可点击吗?如果不是,则不会生成onClick事件。一般来说,如果遇到问题:1.首先尽可能多地进行调试。 2.如果你期望一种行为,但没有发生,请确保你的代码达到了你期望的行为发生的地步。如果没有,请发布代码和问题“为什么我的代码不到达点”? 3.如果发生崩溃,请发布崩溃消息和堆栈跟踪,然后询问“为什么我的代码在此处中止?” M –

回答

2

试试这个获取焦点: View.getFocus();

+0

我认为这不再可用 –

132

Android不允许选择具有可聚焦元素的列表项(按钮)。 修改按钮的XML属性:

android:focusable="false" 

应该仍然可以点击,就不会获得焦点...

+4

谢谢,这应该被标记为答案!解决了我的问题。 –

+10

仅供参考:这不仅出现在按钮上,而且出现了设置了“背景”的TextViews。我用'focusable =“false”'来解决它。 –

+0

正确我把所有东西放在android下:focusable =“false”,现在都很好:D – aimiliano

1

列表视图项目可以点击。要使用它,你必须在列表视图中设置项目点击监听器。然后它会工作。

+0

你能详细说一下“设置点击监听器”吗? – Zapnologica

25

我有同样的问题与只包含一个单选按钮一个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="horizontal" > 

<RadioButton 
    android:id="@+id/userNameRadioButton" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

</LinearLayout> 

我使用的单选按钮的每一行中显示默认的项目选择,使ListView控件处理点击我不得不使用:

radioButton.setFocusableInTouchMode(false); 
radioButton.setFocusable(false); 

或XML文件:

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

所以这是一个焦点相关的问题...威特h上述修饰符的重点是针对点击ListView。

+5

谢谢你的解释!我在我的“CheckBox”中,想知道为什么我可以点击“CheckBox”而不是“ListView”项。为了记录,复选框(或在您的情况下,单选按钮)可以保持点击,但不可聚焦。此外,您还可以用'机器人做它在XML:可聚焦=“假” 机器人:focusableInTouchMode =“假”' – Randy

+0

这个固定为我。显然这是不足以在XML中做到这一点。它一旦我以编程的方式工作, – user990230

+0

看起来很奇怪,你必须在XML和代码中设置它,但是这个解决方案对我很有用。干得好! – Hector

-2

设置 机器人:可点击=“假” 机器人:可调焦=“FASLE”

+1

在什么元素? – DevZer0

1

我很迟,但发现了一些我认为这是有趣的:

如果您的适配器从ArrayAdapter下降,就像我试过的一样,onItemClickListener不会被调用。但是,如果你的适配器是从BaseAdapter下降的(所以你必须实现getCount()和getItem(),对于一个数组来说它是微不足道的),它总是被调用的。

3

考虑通过指定的android使得文本值可选:textIsSelectable = “真”

不要听谷歌。在行布局,设置

textIsSelectable="false"

谢谢林特(不!)

+0

感谢您的+1。在设置_android:focusable =“false”_后,_textIsSelectable =“false”_应该在旁边尝试。我知道我会采取与盐下一次粮食^^ –

+0

你可以尝试Upvoting太:)很高兴认识另一个问题解决得Lint的建议。 –

3

我想添加到rupps答案评论,但我没有足够的声誉。

如果您使用的扩展,你可以用areAllItemsEnabled()和的IsEnabled(INT位置)在你的类重写一个ArrayAdapter自定义适配器:

@Override 
public boolean areAllItemsEnabled() { 
    return true; 
} 

@Override 
public boolean isEnabled(int position) { 
    return true; 
} 

上述固定我的非可点击列表视图。也许这个评论也可以帮助别人,因为“Android列表不可点击”的搜索词在Google中相当高。

+0

也适用于扩大BaseAdapter –

+0

@MarcioGranzotto这仅适用于ArrayAdapter回答的作品。 descendantFocusability =“blocksDescendants” +1这个 – Prabs

1

我用的是列表视图中的视图和按钮,这样我用:

android:focusable="false" 

<button><view> ...

之后,我用下面的代码为我的列表视图,它工作

// Attach the adapter to a ListView 
ListView listView = (ListView) findViewById(R.id.shipping_item_list); 
listView.setAdapter(adapter); 
listView.setOnItemClickListener(listPairedClickItem); 

private AdapterView.OnItemClickListener listPairedClickItem = new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 

    Toast.makeText(getBaseContext(), "Item ", Toast.LENGTH_LONG).show(); 
    } 
}; 
11

这里这个答案为我工作: https://stackoverflow.com/a/16536355/5112161

主要是他在的LinearLayout或RelativeLayout的添加了以下内容:

android:descendantFocusability="blocksDescendants" 

您还需要从所有的XML删除以下:

android:focusable="false" 
android:focusable="true" 
android:clickable="true" 
android:clickable="false" 
+0

机器人做descendantFocusability =“blocksDescendants”已经做了把戏对我来说+1 –

+0

的android:如何用'BaseAdapter' – vida

0

对我来说,问题是,我的项目在我的ListView中有clickable设置为true

+0

你的意思是将其设置为“假的?”我在列表视图中有一个复合视图,并且我将复合视图中每个按钮的clickable设置为false,并且它可以工作。 –