【问题标题】:Adding dynamic ExpandableListView inside main.xml在 main.xml 中添加动态 ExpandableListView
【发布时间】:2011-04-28 17:14:12
【问题描述】:

有没有可以找到 BaseExpandableListAdapter 内部工作原理的网站?我阅读了 API,但我仍然不明白它是如何遍历提供视图的整个数组的。我自己的实现有问题。如果不使用 ExpandableListActivity,我无法创建完整的可扩展列表列表,即使两者相同。它应该从数据库中检索字符串并从每个字符串中创建一个可扩展列表视图,并将所有创建的可扩展列表添加到 main.xml 内的线性布局中。发生的情况是只显示第一个字符串的可扩展列表视图。这是sn-p

主类:

public class MainActivity extends Activity implements OnClickListener {
DBAdapter groupTable = new DBAdapter(this);
ExpandableListView groupLabel;
GroupAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

       groupTable.open();
    adapter = new GroupAdapter(groupTable.getAllGroups());
    retrieveExpandables(adapter);
       groupTable.close();
    Button addGroupButton = (Button)findViewById(R.id.addGroup);
    addGroupButton.setOnClickListener(this);
}

public void retrieveExpandables(GroupAdapter adapter) { 
    LinearLayout layout = (LinearLayout)findViewById(R.id.grouplist);
    LinearLayout groupLayout =    
             (LinearLayout)getLayoutInflater().inflate(R.layout.grouplistview, null);

    ExpandableListView groupExpandableList = 
             (ExpandableListView)groupLayout.findViewById(R.id.groupLabel);
    groupExpandableList.setAdapter(adapter);

    layout.addView(groupLayout);
}

BaseExpandableListAdapter 类:

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    if (convertView instanceof ViewGroup)
        return (ViewGroup) convertView;
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);
    ViewGroup item = (ViewGroup)inflater.inflate(R.layout.grouplisttext, null);
    TextView groupLabel = (TextView)item.findViewById(R.id.groupLabel);
    groupLabel.setText(groups[groupPosition].name);
    groupLabel.setVisibility(View.VISIBLE);
    Log.d("A1", "This part repeating");
    return item;
}

将作为可展开列表标题的 TextView 的 XML 文件

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:id="@+id/groups"
          android:layout_height="wrap_content">

    <TextView android:id="@+id/groupLabel"
              android:textSize="24sp"
              android:text="asdf"
              android:textStyle="bold"
              android:layout_width="fill_parent"
              android:layout_weight="1"
              android:layout_height="40dip"
              android:layout_marginLeft="45dip"
              android:gravity="center_vertical" />

 </LinearLayout>

ExpandableListView 的 XML 文件

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:id="@+id/groups"
          android:layout_height="wrap_content">

    <ExpandableListView android:id="@+id/groupLabel"
              android:textSize="24sp"
              android:textStyle="bold"
              android:layout_width="fill_parent"
              android:layout_weight="1"
              android:layout_height="40dip" />

 </LinearLayout>

抱歉,如果我发布了太多代码,我尝试尽可能多地删除不必要的信息。

【问题讨论】:

    标签: android expandablelistview expandablelistadapter


    【解决方案1】:

    我发现问题出在哪里了。显然我把main.xml中的LinearLayout和ScrollView互换了,getViewGroup类的方法是错误的。现在都好了

    【讨论】:

      猜你喜欢
      • 2011-11-12
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      • 2012-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多