List item with button not clickable anymore

后端 未结 6 1151
再見小時候
再見小時候 2021-02-02 12:23

I have a list item with a button inside.

When the button is shown, the list item is not clickable anymore. To make it clickable again, I have replaced the button with a

6条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-02 13:08

    Here is an example of a clickable button inside a ListView. If you want to download the project you can download the IntelliJ Gradle project from my web site: http://developersfound.com/ListButtonClickExample.zip

    The custom adapter in this example has the click listener instead of the listener being inside the Fragment or Activity. It's done is such a way that there is only on listener object and all button are bound to them for efficiency.

    Here is the ListItem layout:

    
    
    
        
    
            
    
            
    
            

    And here is the CustomAdapter:

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    
    public class MyCustomAdapter extends ArrayAdapter {
    
        private final ArrayList list;
        private static Activity context;
        private View.OnClickListener adaptrDynaListener = null;
    
        public MyCustomAdapter(Activity context, ArrayList list) {
    
            super(context, R.layout.fragment_items, list);
            this.context = context;
            this.list = list;
    
            adaptrDynaListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    String buttonText = ((Button) v).getText().toString();
                    new AlertDialog.Builder(MyCustomAdapter.context).setTitle("Alert").setMessage(buttonText).setNeutralButton("OK", null).show();
    
                }
    
            };
    
        }
    
        static class ViewHolder {
    
            protected ImageView image_list_image;
            protected TextView lbl_list_item;
            protected Button cmd_list_button;
    
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = null;
            ViewHolder viewHolder = new ViewHolder();
    
            if (convertView == null) {
    
                LayoutInflater inflator = context.getLayoutInflater();
                view = inflator.inflate(R.layout.fragment_items, null);
                viewHolder.image_list_image = (ImageView) view.findViewById(R.id.image_list_image);
                viewHolder.lbl_list_item = (TextView) view.findViewById(R.id.lbl_list_item);
                viewHolder.cmd_list_button = (Button) view.findViewById(R.id.cmd_list_button);
                viewHolder.cmd_list_button.setTag(viewHolder);
                view.setTag(viewHolder);
    
            }
            else {
    
                view = convertView;
                viewHolder = (ViewHolder) view.getTag();
    
            }
    
            ViewHolder holder = (ViewHolder) view.getTag();
    
            holder.lbl_list_item.setText(list.get(position).getMessage());
            holder.cmd_list_button.setText(list.get(position).getButtonText());
            holder.cmd_list_button.setOnClickListener(adaptrDynaListener);
    
            return view;
    
        }//public View getView(int position, View convertView, ViewGroup parent)
    
        public int getCount() {
    
            if(list.size() <= 0) {
    
                return 1;
    
            }
    
            return list.size();
    
        }
    
        public Object getItem(int position) {
    
            return position;
    
        }
    
        public long getItemId(int position) {
    
            return position;
    
        }
    
    }
    

    I've tested this Adapter pattern quite extensively and it seems very stable in ListView, ListActivities and ListFragments.

提交回复
热议问题