2

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

要获取 onCreate 的 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;
  }
});

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

4

2 回答 2

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;
      }
    });
于 2013-04-17T11:56:19.013 回答
0
expandableList.setOnGroupClickListener(new OnGroupClickListener()
        {
              public boolean onGroupClick(ExpandableListView arg0, View arg1, int groupPosition, long arg3) 
              {
                  Toast.makeText(getBaseContext(), "" + groupPosition, 1000).show();
                  return false;
               }
        });
于 2013-07-24T06:05:56.037 回答