如何在Expandable listview中获取child和parent的id?

时间:2013-04-17 11:48:38

标签: android expandablelistview expandable

Expandable列表视图。我写了以下代码:

public class MainActivity extends Activity {
  ExpandableListView explvlist;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    explvlist = (ExpandableListView) findViewById(R.id.expandableListView1);
    explvlist.setAdapter(new ParentLevel());
  }

  public class ParentLevel extends BaseExpandableListAdapter {
    @Override
    public Object getChild(int arg0, int arg1) {
      return arg1;
    }
    @Override
    public long getChildId(int groupPosition, int childPosition) {
      return childPosition;
    }
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
      CustExpListview SecondLevelexplv = new CustExpListview(MainActivity.this);
      SecondLevelexplv.setAdapter(new SecondLevelAdapter());
      SecondLevelexplv.setGroupIndicator(null);
      return SecondLevelexplv;
    }
    @Override
    public int getChildrenCount(int groupPosition) {
      return 3;
    }
    @Override
    public Object getGroup(int groupPosition) {
      return groupPosition;
    }
    @Override
    public int getGroupCount() {
      return 8;
    }
    @Override
    public long getGroupId(int groupPosition) {
      return groupPosition;
    }
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
      TextView tv = new TextView(MainActivity.this);
      tv.setText("->FirstLevel");
      tv.setTextColor(Color.BLACK);
      tv.setTextSize(20);
      tv.setBackgroundColor(Color.BLUE);
      tv.setPadding(10, 7, 7, 7);
      return tv;
    }
    @Override
    public boolean hasStableIds() {
      return true;
    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
      return true;
    }
  }

  public class CustExpListview extends ExpandableListView {
    int intGroupPosition, intChildPosition, intGroupid;
    public CustExpListview(Context context) {
      super(context);
    }
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST);
      heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST);
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
  }
  public class CustExpListview2 extends ExpandableListView {
    int intGroupPosition2, intChildPosition2, intGroupid2;
    public CustExpListview2(Context context) {
      super(context);
    }
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      widthMeasureSpec = MeasureSpec.makeMeasureSpec(2000, MeasureSpec.AT_MOST);
      heightMeasureSpec = MeasureSpec.makeMeasureSpec(1000, MeasureSpec.AT_MOST);
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
  }
  public class CustExpListview3 extends ExpandableListView {
    int intGroupPosition2, intChildPosition2, intGroupid2;
    public CustExpListview3(Context context) {
      super(context);
    }
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      widthMeasureSpec = MeasureSpec.makeMeasureSpec(2000, MeasureSpec.AT_MOST);
      heightMeasureSpec = MeasureSpec.makeMeasureSpec(1000, MeasureSpec.AT_MOST);
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
  }

  public class SecondLevelAdapter extends BaseExpandableListAdapter {
    @Override
    public Object getChild(int groupPosition, int childPosition) {
      return childPosition;
    }
    @Override
    public long getChildId(int groupPosition, int childPosition) {
      return childPosition;
    }
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
      CustExpListview2 SecondLevelexplv2 = new CustExpListview2(MainActivity.this);
      SecondLevelexplv2.setAdapter(new ThreeLevelAdapter());
      SecondLevelexplv2.setGroupIndicator(null);
      return SecondLevelexplv2;
    }
    @Override
    public int getChildrenCount(int groupPosition) {
      return 5;
    }
    @Override
    public Object getGroup(int groupPosition) {
      return groupPosition;
    }
    Override
    public int getGroupCount() {
      return 1;
    }
    @Override
    public long getGroupId(int groupPosition) {
      return groupPosition;
    }
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
      TextView tv = new TextView(MainActivity.this);
      tv.setText("-->Second Level");
      tv.setTextColor(Color.BLACK);
      tv.setTextSize(20);
      tv.setPadding(12, 7, 7, 7);
      tv.setBackgroundColor(Color.RED);
      return tv;
    }
    @Override
    public boolean hasStableIds() {
      return true;
    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
      return true;
    }
  }

  public class ThreeLevelAdapter extends BaseExpandableListAdapter {
    @Override
    public Object getChild(int arg0, int arg1) {
      return arg1;
    }
    @Override
    public long getChildId(int groupPosition, int childPosition) {
      return childPosition;
    }
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
      CustExpListview3 SecondLevelexplv3 = new CustExpListview3(MainActivity.this);
      SecondLevelexplv3.setAdapter(new FourLevelAdapter());
      SecondLevelexplv3.setGroupIndicator(null);
      return SecondLevelexplv3;
    }
    @Override
    public int getChildrenCount(int groupPosition) {
      return 3;
    }
    @Override
    public Object getGroup(int groupPosition) {
      return groupPosition;
    }
    @Override
    public int getGroupCount() {
      return 1;
    }
    @Override
    public long getGroupId(int groupPosition) {
      return groupPosition;
    }
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
      TextView tv = new TextView(MainActivity.this);
      tv.setText("Level --Three");
      tv.setTextColor(Color.BLACK);
      tv.setTextSize(20);
      tv.setPadding(15, 5, 5, 5);
      tv.setBackgroundColor(Color.YELLOW);
      return tv;
    }
    @Override
    public boolean hasStableIds() {
      return true;
    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
      return true;
    }
  }

  public class FourLevelAdapter extends BaseExpandableListAdapter {
    @Override
    public Object getChild(int arg0, int arg1) {
      return arg1;
    }
    @Override
    public long getChildId(int groupPosition, int childPosition) {
      return childPosition;
    }
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
      TextView tv = new TextView(MainActivity.this);
      tv.setText("->five level");
      tv.setTextColor(Color.BLACK);
      tv.setTextSize(20);
      tv.setBackgroundColor(Color.MAGENTA);
      tv.setPadding(24, 5, 5, 5);
      return tv;
    }
    @Override
    public int getChildrenCount(int groupPosition) {
      return 3;
    }
    @Override
    public Object getGroup(int groupPosition) {
      return groupPosition;
    }
    @Override
    public int getGroupCount() {
      return 1;
    }
    @Override
    public long getGroupId(int groupPosition) {
      return groupPosition;
    }
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
      TextView tv = new TextView(MainActivity.this);
      tv.setText("->four level");
      tv.setTextColor(Color.BLACK);
      tv.setTextSize(20);
      tv.setBackgroundColor(Color.CYAN);
      tv.setPadding(21, 5, 5, 5);
      return tv;
    }
    @Override
    public boolean hasStableIds() {
      return true;
    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
      return true;
    }
  }
}

要获取onCreateI的ID,请使用以下代码:

explvlist.setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View v, int groupPosition, long id) {
    // TODO Auto-generated method stub
    int itemType = ExpandableListView.getPackedPositionType(groupPosition);
    Log.i("item type", "" + itemType);
    if (itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
      childPosition = ExpandableListView.getPackedPositionChild(id);
      groupPosition = ExpandableListView.getPackedPositionGroup(id);
      Log.i("child", "" + childPosition);
      Log.i("child Group", "" + groupPosition);
    } else if (itemType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
      groupPosition = ExpandableListView.getPackedPositionGroup(id);
      Log.i("Group", "" + groupPosition);
    }
  }
});

以下代码:

explvlist.setOnChildClickListener(new OnChildClickListener() {
  @Override
  public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
    Toast.makeText(getBaseContext(), "Child clicked", Toast.LENGTH_SHORT).show();
    System.out.println(getBaseContext() + "Child clicked");
    return true;
  }
});

但它没有任何帮助 输出不显示任何内容! 请帮忙

2 个答案:

答案 0 :(得分:3)

试试这个

获取群组

   expandableListView.setOnGroupClickListener(new OnGroupClickListener() {
      public boolean onGroupClick(ExpandableListView arg0,View arg1, int groupPosition, long arg3) {

       return false;
       }
    );

让孩子点击

 expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
     public boolean onChildClick(ExpandableListView parent,View v, int groupPosition, int childPosition,
                                long id) {
      return false;
      }
    });

答案 1 :(得分:0)

expandableList.setOnGroupClickListener(new OnGroupClickListener()
        {
              public boolean onGroupClick(ExpandableListView arg0, View arg1, int groupPosition, long arg3) 
              {
                  Toast.makeText(getBaseContext(), "" + groupPosition, 1000).show();
                  return false;
               }
        });