Android: ListView elements with multiple clickable elements

后端 未结 7 605
忘了有多久
忘了有多久 2021-01-03 11:22

I\'ve a ListView where every element in the list contains a TextView and two different Buttons. Something like this:

ListView
------------------         


        
相关标签:
7条回答
  • 2021-01-03 11:31

    i solved the same problem in this way: in the layout.xml of the ListViewItem give the elements a tag, where you want to add the OnClickListener:

           android:id="@+id/CheckBox"
           android:tag="CheckBox" />
    

    ....

           android:id="@+id/Button"
           android:tag="Button" />
    

    In the code, where your ListView is inflated add the OnClickListener with the following code:

           listView.findViewWithTag("CheckBox").setOnClickListener(createCheckBoxOnClickListener(act));
           listView.findViewWithTag("Button").setOnClickListener(createButtonOnClickListener(act));
    
    0 讨论(0)
  • 2021-01-03 11:32

    check below code it may help you.

    private class ListViewAdapter extends BaseAdapter {
    
            private LayoutInflater mInflater;
    
            public ListViewAdapter(Context con) {
                // TODO Auto-generated constructor stub
                mInflater = LayoutInflater.from(con);
            }
    
            public int getCount() {
                // TODO Auto-generated method stub
                return main_genral_class.review_name.size();
            }
    
            public Object getItem(int position) {
                // TODO Auto-generated method stub
    
                return position;
            }
    
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }
    
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                ListContent holder;
    
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.review_row, null);
                    holder = new ListContent();
    
                    holder.img = (ImageView) convertView
                            .findViewById(R.id.imageView1);
                    holder.name = (TextView) convertView
                            .findViewById(R.id.textView1);
                    holder.check_date = (TextView) convertView
                            .findViewById(R.id.textView2);
    
    
    
    
                    holder.img.setOnClickListener(mOnTitleClickListener);
                    holder.name.setOnClickListener(mOnTitleClickListener1);
                    holder.check_date.setOnClickListener(mOnTitleClickListener2);
    
    
                    convertView.setTag(holder);
                } else {
    
                    holder = (ListContent) convertView.getTag();
                }
    
                holder.text2.setText(main_genral_class.review_shout.get(position));
                return convertView;
            }
        }
    

    private OnClickListener mOnTitleClickListener = new OnClickListener() {
            public void onClick(View v) {
                final int position = mListView.getPositionForView((View) v
                        .getParent());
    
                    Toast.makeText(review_activity.this, "click on Image View",
                            Toast.LENGTH_SHORT).show();
    
            }
        };
    

    private OnClickListener mOnTitleClickListener1 = new OnClickListener() {
                public void onClick(View v) {
                    final int position = mListView.getPositionForView((View) v
                            .getParent());
    
                        Toast.makeText(review_activity.this, "click on Text View",
                                Toast.LENGTH_SHORT).show();
    
                }
            };
    

    private OnClickListener mOnTitleClickListener2 = new OnClickListener() {
                public void onClick(View v) {
                    final int position = mListView.getPositionForView((View) v
                            .getParent());
    
                        Toast.makeText(review_activity.this, "click on Date Text View",
                                Toast.LENGTH_SHORT).show();
    
                }
            };
    
    0 讨论(0)
  • 2021-01-03 11:34

    you can add listeners in your getView() method of Adapter.

    0 讨论(0)
  • 2021-01-03 11:40

    easy, just implement the OnClickListener for these two buttons, and get the position that you get from getView(), make sure you set the position to final in order to get it in OnClickListener

    0 讨论(0)
  • 2021-01-03 11:44

    you need to make baseAdpter to achieve this

    public class ContactsAdapter extends BaseAdapter {
    
        ArrayList<ContactInfo> mlist;
        Context mcontext;
    
    
    public BluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {      
            mlist =  mchtlist;
            mcontext = context;
    
        }
    
        @Override
        public int getCount() {
            return mlist.size();
        }
    
        @Override
        public Object getItem(int postion) {
            return mlist.get(postion);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
            public View getView(int position, View convertview, ViewGroup viewgroup){
                View view = null;
                if(convertview == null){
                    LayoutInflater inflater = context.getLayoutInflater();
                    view = inflater.inflate(R.layout.contactrow, null);
    
                    ContactHolder holder = new ContactHolder();
    
                    holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
                    holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
                    holder.chkselected = (CheckBox)view.findViewById(R.id.check);
    
                    setOnClickListener(new OnClickListener() {
            @Override
                public void onClick(View arg0) {
                // to open the selected file in resp
    
                      // do your work here
                     }});
    
    
        chkselected .setOnClickListener(new OnClickListener() {
            @Override
        public void onClick(View v) {
        // Toast.makeText(context,// "checked is clicke="+pos, 12).show();
                if (chkselected.isChecked())          
                           {            
    
                            // do your work here
                } else {
    
         // do your work here                               
                }
            }
    });
    
    
    
                view.setTag(holder);
    
            }
                else{
                    view = convertview;
                }
                ContactHolder holder2 = (ContactHolder) view.getTag();
                holder2.txtviewfirstname.setText(list.get(position).firstname);
                holder2.txtviewphone.setText(list.get(position).phonenumber);
                holder2.chkselected.setChecked(list.get(position).selected);
                return view;
            }
    
    }
    
    0 讨论(0)
  • 2021-01-03 11:44

    dont add onItemClickListener for the listview rather add onClick for your required button and for checkbox do it like this

     CheckBox cbox = new CheckBox(this);// find your checkbox from the view here.
            cbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked){ // do what ever you want. } } });
    0 讨论(0)
提交回复
热议问题