2012-01-21 131 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,並且它可以工作。 –