如何以编程方式创建ExpandableListView?

时间:2013-05-19 20:49:14

标签: android

我正在尝试以编程方式创建一个ExpandableListView,但到目前为止它为它预留了空间,但空间是完全黑的!没有任何表现。 我在这里有一些特殊要求:

  1. 我想在代码上做所有事情,没有XML。到目前为止,我的整个UI都没有XML,我想保持这种方式
  2. 如果可能的话,我想从Activity中获取我的活动。我觉得改变类只是因为ExpandableListView会喜欢它,这很愚蠢。如果其他控件也需要奇怪的活动特殊类怎么办?希望在Activity上有一个ExpandableListView,对吧?
  3. 所以这是我的代码到目前为止的适配器:

    // -------------------------------------------
    // DoubleStringArrayExpandableListAdapter
    // -------------------------------------------
    public class DoubleStringArrayExpandableListAdapter extends BaseExpandableListAdapter
    {
    public ArrayList<String> Parents = new ArrayList<String>();
    public ArrayList<String> Children = new ArrayList<String>();
    public Activity mContext;
    
    public Object getChild(int groupPosition, int childPosition)
    {
      return Children.get(groupPosition);
    }
    
    public long getChildId(int groupPosition, int childPosition)
    {
      return childPosition;
    }
    
    public int getChildrenCount(int groupPosition)
    {
      return 1;
    }
    
    public TextView getGenericView()
    {
      AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
        LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    
      TextView textView = new TextView(mContext);
      textView.setLayoutParams(lp);
      // Center the text vertically
      textView.setGravity(Gravity.CENTER);
      // Set the text starting position
      textView.setPadding(55, 0, 0, 0);
      return textView;
    }
    
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent)
    {
      TextView textView = getGenericView();
      textView.setText(getChild(groupPosition, childPosition).toString());
      return textView;
    }
    
    public Object getGroup(int groupPosition)
    {
        return Parents.get(groupPosition);
    }
    
    public int getGroupCount()
    {
        return Parents.size();
    }
    
    public long getGroupId(int groupPosition)
    {
        return groupPosition;
    }
    
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent)
    {
      TextView textView = getGenericView();
      textView.setText(getGroup(groupPosition).toString());
      return textView;
    }
    
    public boolean isChildSelectable(int groupPosition, int childPosition)
    {
        return true;
    }
    
    public boolean hasStableIds()
    {
        return true;
    }
    } 
    

    这是我创建ExpandableListView的代码:

    listNews = new ExpandableListView(this);
    adapterNews = new DoubleStringArrayExpandableListAdapter();
    adapterNews.mContext = this;
    listNews.setAdapter(adapterNews);
    listNews.setVerticalScrollBarEnabled(true);
    listNews.setScrollbarFadingEnabled(false);
    //listNews.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    //listNews.setFocusableInTouchMode(true);  
    listNews.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 200/*doesnt workLayoutParams.WRAP_CONTENT*/));
    lLayout.addView(listNews);
    adapterNews.Parents.add("Parent");
    adapterNews.Children.add("Child");
    

    任何想法为什么它不起作用,这里有什么问题?非常感谢=)

1 个答案:

答案 0 :(得分:1)

您需要在适配器上调用notifyDataSetChanged(),因为您在最后两行代码中添加了数据,或者您可以在代码的最后一行之后调用setAdapter。