其他分享
首页 > 其他分享> > 如何在Android的可扩展列表视图中的子点击部分触发事件

如何在Android的可扩展列表视图中的子点击部分触发事件

作者:互联网

在父单击的可扩展列表中,我有一个孩子,其中有3个文本视图,例如,当我单击不同的文本视图时,我想触发其他事件.

for text view 1 output is hello
for text view 2 output is hi
for text view 3 output is heya

解决方法:

正如edwin所说,您可以制作一个Costum适配器.您可以在其中在每个View上使用setOnClickListner()方法.就像我在这里一样

   class CustomAdapter extends ArrayAdapter<Contact>
  {
     LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    public CustomAdapter(Context context, int textViewResourceId,
     ArrayList<Contact> strings) {

      //let android do the initializing :)
  super(context, textViewResourceId, strings);
  }


        //class for caching the views in a row  
   private class ViewHolder
   {

    TextView id,name,phn_no;

   }

   ViewHolder viewHolder;

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {

    if(convertView==null)
    {        
                                 //inflate the custom layout                        
     convertView=inflater.inflate(R.layout.activity_main, null);
     viewHolder=new ViewHolder();

     //cache the views
     viewHolder.id=(TextView) convertView.findViewById(R.id.contact_id_txt);
     viewHolder.name=(TextView) convertView.findViewById(R.id.contact_name_txt);
     viewHolder.phn_no=(TextView) convertView.findViewById(R.id.contact_ph_no_txt);
     viewHolder.id.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Hi!!", Toast.LENGTH_SHORT).show();

        }
    }); 
     viewHolder.name.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Hello!!", Toast.LENGTH_SHORT).show();

        }
    });
     viewHolder.phn_no.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Heya!!!", Toast.LENGTH_SHORT).show();      
        }
    });

     //link the cached views to the convertview
     convertView.setTag(viewHolder);

    }
    else
     viewHolder=(ViewHolder) convertView.getTag();
       //set the data to be displayed
       viewHolder.id.setText(contacts.get(position).get_id()+"");
    viewHolder.name.setText(contacts.get(position).get_name());
    viewHolder.phn_no.setText(contacts.get(position).get_phn_no());

    //return the view to be displayed
   return convertView;
   }

  }

标签:expandablelistview,android
来源: https://codeday.me/bug/20191031/1975014.html