setOnItemClickListener() not working on custom ListView @ Android

前端 未结 6 1806
刺人心
刺人心 2020-12-01 09:55

I have Implemented a custom ListView by extending LinearLayout for every row. Every row has a small thumbnail, a text and a check box.

相关标签:
6条回答
  • 2020-12-01 10:00

    Try this
    For ListView,

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

    Also, make sure that for CheckBox inside list item set focusable false

    android:focusable="false"
    android:focusableInTouchMode="false"
    
    0 讨论(0)
  • 2020-12-01 10:00

    Did you made any ViewHolder in your extended adapter class? If yes, then make an instance of your that placeholder in the setOnItemClickListener() something will may work like this.

    @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();
    
        }
    

    Hope, you want something same. Good Luck!

    0 讨论(0)
  • 2020-12-01 10:09

    old answer: I wrote in previous post here

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

    will not help when ImageButton is in custom view.. One must use button.setFocusable(false); during runtime (from java source code)

    Edit: There is even more elegant solution. Try to add android:descendantFocusability="blocksDescendants" in root layout of list element. That will make clicks onListItem possible and separately u can handle Button or ImageButton clicks

    0 讨论(0)
  • 2020-12-01 10:10

    For a ListView where you set the item views to CheckBox

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

    http://code.google.com/p/android/issues/detail?id=3414

    0 讨论(0)
  • 2020-12-01 10:10

    I have this code

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

    But it didn't work

    So i have juste put a onItemSelectedListener under and it work 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 讨论(0)
  • 2020-12-01 10:20

    Set these properties:

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

    for your all UI elements in your list_item.xml file.

    if this is not resolved in your adapter set:

        v.imageView.setFocusable(false);
        v.imageView.setFocusableInTouchMode(false);
    
    0 讨论(0)
提交回复
热议问题