2013-05-08 66 views
12

我已經使用了自定義ListView,並且我使用相同的ListView顯示了一些數據。

當我點擊列表視圖項目時,onClickListener沒有被調用。我無法選擇任何列表項目。OnItemClickListener和OnClickListener不能用於ListView

佈局代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_alignParentBottom="true" 
android:layout_alignParentLeft="true" 
android:layout_marginBottom="16dp" 
android:background="@drawable/list_selector" 
android:clickable="true" 
android:orientation="horizontal" 
android:padding="5dip" > 

<LinearLayout 
    android:id="@+id/imgProperty" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_marginRight="5dip" 
    android:padding="3dip" > 

    <ImageView 
     android:id="@+id/list_image" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:contentDescription="@string/app_name" 
     android:src="@drawable/ic_launcher" 
     android:focusable="false"/> 
</LinearLayout> 

<TextView 
    android:id="@+id/tvCity" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_marginLeft="75dip" 
    android:layout_toRightOf="@+id/list_image" 
    android:paddingBottom="10dip" 
    android:text="property" 
    android:textColor="#040404" 
    android:textSize="15sp" 
    android:textStyle="bold" 
    android:typeface="sans" /> 

<TextView 
    android:id="@+id/tvprice" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/imgProperty" 
    android:layout_alignLeft="@+id/tvCity" 
    android:text="Price" 
    android:focusable="false"/> 

</RelativeLayout> 

適配器代碼:

public class CustomListAdapter extends BaseAdapter { 

ArrayList<Propety> PropertiesArray; 
private LayoutInflater Inflater; 


public CustomListAdapter(ArrayList<Propety> PropertiesArray) { 

    this.PropertiesArray=PropertiesArray; 

} 
@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return PropertiesArray.size(); 
} 

@Override 
public Object getItem(int position) { 

    return PropertiesArray.get(position); 
} 

@Override 
public long getItemId(int position) { 

    return position; 
} 

@Override 
public View getView(int position, View convertView, final ViewGroup parent) { 

    if (convertView == null) { 
      LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 
      convertView = inflater.inflate(R.layout.customlistview, parent, false); 
     } 


     final Propety ListArray = PropertiesArray.get(position); 

     TextView tvPropertyName = (TextView) convertView.findViewById(R.id.tvCity); 
     tvPropertyName.setText(ListArray.getName()); 

     TextView tvPrice = (TextView) convertView.findViewById(R.id.tvprice); 
     tvPrice.setText(ListArray.getPrice()); 

     ImageView imgProperty = (ImageView) convertView.findViewById(R.id.list_image); 
     imgProperty.setImageResource(R.drawable.ic_launcher); 


     convertView.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      Toast.makeText(parent.getContext(), "view clicked: " + ListArray.getName(), Toast.LENGTH_SHORT).show(); 
     } 
    }); 


    return convertView; 
} 

} 

ListView的代碼:

ListView propertylistview = (ListView) findViewById(R.id.listview); 
CustomListAdapter customlistview=new CustomListAdapter(PropertiesArray); 
propertylistview.setAdapter(customlistview); 

的ListView XML:

<ListView 
    android:id="@+id/listview" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/customview" 
    android:layout_marginBottom="10dp" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginTop="24dp" 
    android:background="@drawable/list_selector" 
    android:textAlignment="center" > 

custom.xml:

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

<SurfaceView 
    android:id="@+id/surface" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" /> 

<TextView 
    android:id="@+id/txtangle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_marginBottom="115dp" 
    android:layout_marginLeft="95dp" 
    android:text="" /> 

<ListView 
    android:id="@+id/listview" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/customview" 
    android:layout_marginBottom="10dp" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginTop="24dp" 
    android:background="@drawable/list_selector" 
    android:textAlignment="center" > 

</ListView> 

<view 
    android:id="@+id/customview" 
    android:layout_width="110dp" 
    android:layout_height="110dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    class="com.example.samplebuapp.CustomCompass" /> 

<view 
    android:id="@+id/view1" 
    android:layout_width="110dp" 
    android:layout_height="110dp" 
    android:layout_above="@+id/listview" 
    android:layout_alignParentRight="true" 
    android:layout_marginRight="18dp" 
    class="com.example.samplebuapp.CustomView" /> 

<LinearLayout 
    android:id="@+id/rl1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/listview" 
    android:orientation="vertical" 
    android:focusable="false"> 
</LinearLayout> 

</RelativeLayout> 

即使滾動不工作。

我無法弄清楚爲什麼會發生這種情況?我錯過了什麼嗎?

解決此問題的任何幫助表示讚賞。

+0

這也許可以幫助:http://www.thepcwizard.in/2012/09/android-creating-custom-listview-for。 html – ThePCWizard 2013-05-08 10:11:59

+0

看來,觸摸事件不會傳播到ListView。佈局文件中是否有任何東西與ListView一起阻止傳播? – 2013-05-08 10:13:20

+0

我正在顯示ListView以及其他自定義視圖和surfaceView。請參閱custom.xml – Mahe 2013-05-08 10:17:56

回答

10

以下將做你的案件的工作。

ListView propertylistview = (ListView) findViewById(R.id.listview); 
    propertylistview.setOnItemClickListener( myListViewClicked): 

     OnItemClickListener myListViewClicked = new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Toast.makeText(YourActivity.this, "Clicked at positon = " + position, Toast.LENGTH_SHORT).show(); 

      } 
     }; 

不要忘記刪除從CustomAdapter

convertView.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 

     Toast.makeText(parent.getContext(), "view clicked: " + ListArray.getName(), Toast.LENGTH_SHORT).show(); 
    } 
}); 
+0

謝謝@Ninja。那不行。我在SurfaceView上顯示ListView。在SurfaceView上顯示ListView會導致任何問題? – Mahe 2013-05-08 10:15:10

+1

在聲明它之前,你使用'myListViewClicked' ..? – 2014-12-27 12:21:31

+0

通過首先將OnItemClickListener分配給一個變量(然後傳入setOnItemClickListener),它神祕地解決了我的問題。然後之後,當我做到這一點,而不必分配給變量,那麼它仍然有效。) – 2015-07-16 09:29:25

-2

你應該使用ListView.setOnItemClickListener()來實現你想要的。

+0

但實際上,customListAdapter類中的OnClickListener應該可以工作。不知道爲什麼它不工作 – Mahe 2013-05-08 10:06:24

+0

這不是一個真正的問題的答案... – 2014-11-09 01:07:52

+0

我一直堅持這個6小時了,我真的需要幫助。 http://stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/35109304#35109304 – 2016-01-31 03:02:58

54

我NINJIA同意以下,但你應該theese添加到XML代碼: 你應該定義所有的子對象項列表視圖(TextView的,ImageView的等):

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

而對於根項目的RelativeLayout/LinearLayout中,因此,定義:

android:clickable="false" 
android:descendantFocusability="blocksDescendants" 
android:focusable="false" 
android:focusableInTouchMode="false" 

如果你不定義這些,他們就會「捕獲」 click事件。如果你有一個自定義的ListView適配器,只檢查是否覆蓋:

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

你可以得到更多的細節爲什麼設置'android:clickable =「false」 android:focusable =「false」 android:focusableInTouchMode =「false 「' 謝謝。 – boiledwater 2013-10-08 01:57:54

+0

@ boiledwater:我不知道爲什麼,但似乎click事件是由ListView對象獨佔處理的,您應該禁用其他點擊處理以避免干擾 – 2013-10-09 08:26:43

+1

我發現http://stackoverflow.com/a/4518810/ 596555 – boiledwater 2013-10-10 10:07:16

2

簡單的代碼,解決了我的問題,方法觀。setOnClickListener是我的自定義適配器

view.setFocusable(false) 
2

之內。如果觸摸事件越來越截獲的單元佈局中,在佈局爲您定製的細胞,在你的頂層佈局設置 android:descendantFocusability="blocksDescendants"。對我而言,將xml屬性添加到每個單獨的視圖太痛苦了。

注意,這也可以在代碼中設置:

cell.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);

我還與細胞內的ListView嘗試這樣做,我不得不重寫的onTouchEvent得到它的工作:

public class NoTouchListView extends ListView { 

    @Override 
    public boolean onTouchEvent(MotionEvent ev) { 
     return false; 
    } 
} 
+0

我一直堅持6個小時,我真的需要幫助。 http://stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/35109304#35109304我已經堅持了6個小時,現在我真的需要幫助。 http://stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/35109304#35109304v – 2016-01-31 03:02:48

1

即使我有同樣的問題,我有複選框,做了以下掩蓋itemClickListener工作,

添加了以下pro複選框,

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

和ItemClickListner開始工作。

對於詳細的例子,你可以去通過鏈接,

http://knowledge-cess.com/android-itemclicklistner-with-checkbox-or-radiobutton/

希望它可以幫助乾杯!

2

檢查列表行佈局一次:

佈局或任何觀點不應該是'機器人:可點擊= 「true」 和 機器人:可調焦= 「真」

如果是那裏只需刪除並再次運行該應用程序。

相關問題