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

时间:2013-01-25 12:36:35

标签: android expandablelistview

在父级点击的可扩展列表中,我有一个孩子,其中我有3个文本视图,我想在点击不同的文本视图时触发不同的事件。

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

7 个答案:

答案 0 :(得分:2)

正如edwin所说,你可以制作一个自定义适配器。其中您可以在每个View上设置setOnClickListener()方法。就像我在这里做的那样..

   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;
   }

  }

答案 1 :(得分:1)

ExpandableListView expandableListView; 
expandableListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                // TODO Auto-generated method stub

//////////////////////////Change your child text values here.


                return false;

            }
        });

答案 2 :(得分:1)

尝试这种方式:

public View getChildView(int groupPosition, int childPosition, boolean isLastChild,View convertView, ViewGroup parent) 
{
    if (convertView == null) 
    {
        LayoutInflater infalInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.layout, null);
    }       
    tv_ChildFieldId = (TextView) convertView.findViewById(R.id.tv1);
    tv_ChildFieldId.setText(getChildIdFromDB(groupPosition, childPosition).toString());
tv_ChildFieldId.setOnClickListener(new ID_OnClickListener());
    //And so on   
    return convertView;
}
/**Onclick Listener for the Textview*/
private class ID_OnClickListener implements OnClickListener
{
    @Override
    public void onClick(View v) 
    {
    }
});

答案 3 :(得分:0)

因此,您必须为可扩展列表创建自定义适配器。然后在Custom adapter class

中为不同的小部件接口实现onClickListener

在自定义适配器类

 @Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
        View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.child_layout, null);
    }
   //Give your On click Implementations Here
    return convertView;
}

浏览Thisthis

使用OnChildClickListener

实施活动
@Override
public boolean onChildClick(ExpandableListView parent, View v,
   int groupPosition, int childPosition, long id) {
     Toast.makeText(MainActivity.this, "Clicked On Child",
     Toast.LENGTH_SHORT).show();
      return true; //or false
 }

答案 4 :(得分:0)

看看here 您可以定义具有

的自定义可扩展适配器
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) 

在此方法中,您可以确定相对于父级的子位置,并设置所需的单击侦听器

答案 5 :(得分:0)

上面的@edwin方法失败了!在活动中实现 ExpandableListView.onchildItemClick 并不会给出任何调用。

如果您有自定义且复杂的ChildView,则会出现问题。

对我来说,它可以通过在 convertView 本身设置 onclickListener 来实现。我从适配器内部获得了完美的点击。

convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(_context, groupPosition + " clicked "
                    + childPosition, Toast.LENGTH_SHORT).show();
        }
    });

请务必在

之外写下
if(convertView==null){.....layout inflation}

括号。

答案 6 :(得分:0)

试试这会有帮助

    expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int     groupPosition, int childPosition, long id) {
   if (groupPosition == 0){
       if (childPosition == 0){
           Intent intent = new Intent(MarketJaipur.this, ClothesMarket.class);
           startActivity(intent);
       }
   }
        return false;
    }
});