【问题标题】:how to know on which button user made click?如何知道用户点击了哪个按钮?
【发布时间】:2014-08-31 17:14:41
【问题描述】:

我有ExpandableListView,并且我在此ListView 的每一组上都有按钮。如何知道用户点击了哪个组的按钮? 当用户在这些小部件上执行某些事件时,是否可以为按钮和编辑文本提供一些 ID 并获取此 ID?

【问题讨论】:

  • 您始终可以根据列表中的位置在视图对象上使用 setId() 和 getId()。

标签: android button expandablelistview


【解决方案1】:

您应该为listView_layout 中的按钮设置一个ID。 您需要为您的 listView 设置一个Adapter。在getChildView 方法中,您可以为您的按钮设置点击监听器。

public class ExpandableListAdapter extends BaseExpandableListAdapter {

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

        ViewHolder holder = null;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listView_layout, null);
            holder = new ViewHolder();

            holder.mButton= (ImageButton) convertView.findViewById(R.id.myButton);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        holder.mButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        });
   }

   public static class ViewHolder {
      public ImageButton mButton;
   }
}

【讨论】:

  • 兄弟,它是可扩展的listView,你不会得到getView方法。
  • 兄弟,我的意思是getChildView。
【解决方案2】:

这样做:

yourListView.setOnChildClickListener(this);

在被覆盖的方法中:

@Override
public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {

    switch (groupPosition or childPosition) {
    case 0:
        // do your logic
        break;

    default:
        break;

    }

    return false;
}

您将同时获得 GroupPosition 和 ChildPosition。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多