2009-10-05 72 views
19

我已經爲每一行延長LinearLayout實現了自定義ListView。每行都有一個小縮略圖,一個文本和一個check boxlist view正確部署,我可以滾動並通過它沒有任何問題。setOnItemClickListener()沒有自定義的ListView工作@安卓

ListView似乎並沒有給setOnItemClickListener()任何反應,所以我必須在每一行這顯然是創設問題中的文本的getView()設置點擊聽衆找到一個解決辦法,當我試圖重用adapter。有沒有人有辦法解決嗎?

回答

43

試試這個
對於ListView控件,

final ListView list = (ListView) findViewById(R.id.list); 
list.setItemsCanFocus(false); 

此外,請確保複選框內的列表項設置可聚焦假

android:focusable="false" 
android:focusableInTouchMode="false" 
+0

我有同樣的問題,上述解決方案適用於在該行檢測點擊,但我有我的自定義的ListView行2個按鈕。我怎麼知道哪個按鈕被點擊了? 謝謝 – longhairedsi 2010-09-10 11:47:53

+0

謝謝!!!你真的幫助我了! – 2013-07-24 06:06:21

+1

@longhairedsi:請將以下代碼添加到getView()方法中 button1.setFocusable(false); button2.setFocusable(false); button1.setFocusableInTouchMode(false); button2.setFocusableInTouchMode(false); button1.setOnClickListener(this); button2.setOnClickListener(this); 在您的適配器(或您希望的地方)中實現您的點擊監聽器按鈕 設置列表視圖的點擊監聽器也..... 現在檢查您的點擊將適用於這兩種..... listview至於按鈕也 – 2013-09-26 10:34:33

0

你有沒有在你的擴展適配器類的任何ViewHolder?如果是,那麼在setOnItemClickListener()中創建一個你的佔位符的實例,它可能會像這樣工作。

@Override 
    protected void onListItemClick(ListView l, View v, int position, long id) {   
     View rowView = v; 
     if (rowView == null) { 
      LayoutInflater inflater = this.getLayoutInflater(); 
       // GET INFLATE OF YOUR LAYOUT. 
      rowView = inflater.inflate(R.layout.projectpeopledescrate, null); 
      // CUSTOM ViewHolder Class Created in Adapter. 
// name,title,comment are my components on the same listview clicked item. 
      PPDViewHolder viewHolder = new PPDViewHolder(); 
      viewHolder.name  = (TextView) rowView.findViewById(R.id.ppeopledescrvname); 
      viewHolder.title = (TextView) rowView.findViewById(R.id.ppeopledescrvtime); 
      viewHolder.comment = (TextView) rowView.findViewById(R.id.ppeoplervcomment); 
      viewHolder.hiddenLayout = (RelativeLayout) rowView.findViewById(R.id.hiddenCommentPanel); 
      rowView.setTag(viewHolder); 
     } 
      // ANOTHER object instance to apply new changes. 
     PPDViewHolder holder = (PPDViewHolder) rowView.getTag(); 
// I've setted up visibility over the components. You can set your onClickListener over your buttons. 
     holder.comment.setVisibility(View.GONE); 
     holder.name.setVisibility(View.GONE); 
     holder.title.setVisibility(View.GONE); 
     holder.hiddenLayout.setVisibility(View.VISIBLE); 
     holder.hiddenLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.MATCH_PARENT)); 
     holder.hiddenLayout.bringToFront(); 

    } 

希望,你想要的東西一樣。祝你好運!

11

老答案:previous post here

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

無助的時候ImageButton的是自定義視圖中。其中必須使用運行時button.setFocusable(false);(從Java源代碼)

編輯:還有更優雅的解決方案。嘗試在列表元素的根佈局中添加android:descendantFocusability="blocksDescendants"。這將使點擊onListItem可能並單獨u能處理按鈕或點擊的ImageButton

0

我有這樣的代碼

this.mListView.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
       Log.v(TAG,"loul"); 
      } 
     }); 

但它沒有工作

,所以我有中庸之道把onItemSelectedListener下,它工作OO:

this.mListView.setItemsCanFocus(false); 
this.mListView.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { 
     Log.v(TAG,"loul"); 
    } 
}); 
//listener for nothing but it allow OnItemClickListener to work 
this.mListView.setOnItemSelectedListener(new OnItemSelectedListener() { 

    @Override 
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 

    } 

    @Override 
    public void onNothingSelected(AdapterView<?> arg0) { 

    } 
}); 
0

設置這些特性:

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

在您list_item.xml文件的所有UI元素。

如果這不是在您的適配器一套解決:

v.imageView.setFocusable(false); 
    v.imageView.setFocusableInTouchMode(false); 
相關問題