android - 如何以编程方式创建 ExpandableListView?

标签 android

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

  1. 我想在代码上做所有事情,而不是 XML。到目前为止,我的整个 UI 的 XML 为零,我希望保持这种状态
  2. 如果可能,我想从 Activity 派生我的 Activity。我发现仅仅因为 ExpandableListView 会喜欢而更改类很愚蠢。如果其他控件也需要奇怪的 Activity 特殊类怎么办?希望可以在 Activity 上使用 ExpandableListView,对吗?

到目前为止,这是我的适配器代码:

// -------------------------------------------
// 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");

知道为什么它不起作用以及这里有什么问题吗???非常感谢 =)

最佳答案

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

关于android - 如何以编程方式创建 ExpandableListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16639347/

相关文章:

android - 在 Android 应用程序中使用 mlock()

android - Jacoco 和 android-gradle-plugin >= 1.1 的单元测试代码覆盖率

android - 更改选项卡背景颜色并删除选项卡之间的分隔线

Android onNewIntent() 在 Activity 不可见时未触发

android - 为什么堆栈跟踪行号与源代码行号不匹配?

Android数据库更新记录

android - 如何解决 Gradle 构建中的 FileCollectionSnapshotterRegistry 错误?

android - 水平 RecyclerView 顶部的滚动条

java - Android 日期时间操作库

java - getJSONObject 方法有问题