Android ExpandableListView不工作
我试图在片段中使用可扩展列表视图,但由于某些原因,它没有显示出来。 现在,在输入正确的数据之前,我只使用createData 我想在fragment_home.xml上显示该列表Android ExpandableListView不工作,android,listview,android-fragments,expandablelistview,Android,Listview,Android Fragments,Expandablelistview,我试图在片段中使用可扩展列表视图,但由于某些原因,它没有显示出来。 现在,在输入正确的数据之前,我只使用createData 我想在fragment_home.xml上显示该列表 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".HomeFragment" >
<ExpandableListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ExpandableListView>
以下是ExpandableListView的相关文件
listrow_group.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_marginLeft="8dp"
android:drawableRight="@drawable/ic_launcher"
android:gravity="left"
android:paddingLeft="32dp"
android:paddingTop="8dp"
android:text="Test"
android:textSize="14sp"
android:textAlignment="textEnd"
android:textStyle="bold" />
Group.java
package com.smarte.smarteproducts;
import java.util.ArrayList;
import java.util.List;
public class Group {
public String string;
public final List<String> children = new ArrayList<String>();
public Group(String string) {
this.string = string;
}
}
package com.smarte.smarter产品;
导入java.util.ArrayList;
导入java.util.List;
公共课组{
公共字符串;
public final List children=new ArrayList();
公共组(字符串){
this.string=string;
}
}
listrow_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="40dp"
android:clickable="true"
android:orientation="vertical"
android:paddingLeft="40dp"
tools:context=".HomeFragment" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_launcher"
android:drawablePadding="5dp"
android:gravity="center_vertical"
android:text="@string/hello_world"
android:textSize="14sp"
android:textStyle="bold" >
</TextView>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black" />
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".HomeFragment" >
<ExpandableListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ExpandableListView>
HomeFragment.java
package com.smarte.smarteproducts;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Activity;
import android.util.SparseArray;
import android.widget.ExpandableListView;
public class HomeFragment extends Fragment {
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
public class HomeFragmentListExpand extends Activity {
// more efficient than HashMap for mapping integers to objects
SparseArray<Group> groups = new SparseArray<Group>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_home);
createData();
ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);
MyExpandableListAdapter adapter = new MyExpandableListAdapter(this,
groups);
listView.setAdapter(adapter);
}
public void createData() {
for (int j = 0; j < 5; j++) {
Group group = new Group("Test " + j);
for (int i = 0; i < 5; i++) {
group.children.add("Sub Item" + i);
}
groups.append(j, group);
}
}
}
}
package com.smarte.smarter产品;
导入android.app.Fragment;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.app.Activity;
导入android.util.SparseArray;
导入android.widget.ExpandableListView;
公共类HomeFragment扩展了片段{
公共HomeFragment(){}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.fragment\u home,container,false);
返回rootView;
}
公共类HomeFragmentListExpand扩展活动{
//在将整数映射到对象方面比HashMap更高效
SparseArray组=新SparseArray();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_home);
createData();
ExpandableListView listView=(ExpandableListView)findViewById(R.id.listView);
MyExpandableListAdapter=新的MyExpandableListAdapter(此,
组);
setAdapter(适配器);
}
public void createData(){
对于(int j=0;j<5;j++){
组=新组(“试验”+j);
对于(int i=0;i<5;i++){
添加(“分项”+i);
}
附加(j,组);
}
}
}
}
在HomeFragment类中,您正在调用HomeFragmentListExpand吗?因为我不能有两个类同时作为HomeFragmentList和HomeFragmentList,但您为什么这样调用?只需在HomeFragment Classic中执行所有操作它是这样做的,因为某些函数必须在activity类中完成,而第一个类是fragment。