listview item with button not responding to onListItemClick

前端 未结 4 2039
我在风中等你
我在风中等你 2021-01-26 02:52

I am working on an app where the list item are complex, TextView and two ImageButtons. I have looked at the around for a solution, and tried all that I have seen, still nothing.

4条回答
  •  闹比i
    闹比i (楼主)
    2021-01-26 03:45

    you can create View.OnClickListner object which can listen your imagebutton click in getView. onListItemClick generally used to handle row click event not items in rows.

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflator = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View listItem = inflator.inflate(R.layout.medcince_list_item, null);
    
        ImageButton mEdit = (ImageButton)listItem.findViewById(R.id.item_edit);
        mEdit.setOnClickListener(new View.OnClickListener(){
    
        @Override
        public void onClick(View v) {
            // HERE YOU CAN HANDLE BUTTON CLICK. POSITION YOU CAN HAVE FROM getView already.
        }
    
        });
        mEdit.setTag(getItem(position));
    
        ImageButton mHistory = (ImageButton)listItem.findViewById(R.id.item_history);
        mHistory.setOnClickListener(new View.OnClickListener(){
    
        @Override
        public void onClick(View v) {
            // HERE YOU CAN HANDLE BUTTON CLICK. POSITION YOU CAN HAVE FROM getView already.
        }
    
        });
        mHistory.setTag(getItem(position));
    
        return listItem;
    }
    

提交回复
热议问题